print.cpp improvements?

in print.cpp there are many functions that use int base

void Print::print(char c, int base)
{
  print((long) c, base);
}

think uint8_t base should be enough as bases above 255 are never used or

don't know the effect on sketches yet, => to investigate

Yep, strips of 14 bytes of a sketch that uses print (2006 =>1992 bytes)

also changed #digits to uint8_t for printing floats/double

Should also strip some bytes from all derived classes :slight_smile:

Posted as improvement - memory improvement of the print.cpp lib · Issue #193 · arduino/ArduinoCore-avr · GitHub -

Other things found:

replace the indexer for printnumber from unsigned long to a byte ==> 60 bytes less !
removed the expensive modulo operator ==> 10 bytes less! ==> update did fail some tests

void Print::printNumber(unsigned long n, uint8_t base)
{
  unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. 
  // unsigned long i = 0;
  uint8_t i = 0;

  if (n == 0) {
    print('0');
    return;
  } 

  while (n > 0) 
  {
    buf[i++] = n % base;  
    n /= base;
  }

  for (; i > 0; i--)
    print((char) (buf[i - 1] < 10 ?
      '0' + buf[i - 1] :
      'A' + buf[i - 1] - 10));
}

replace 2 calls with one ==> 12 bytes less !

void Print::println(void)
{
  write("\r\n");
  // print('\r');
  // print('\n');  
}

replace float division with float multiplication in printFloat ==> 208 bytes less

  • replaced an int by an uint8_t ==> 6 bytes less
void Print::printFloat(double number, uint8_t digits) 
{ 
  // Handle negative numbers
  if (number < 0.0)
  {
     print('-');
     number = -number;
  }

  // Round correctly so that print(1.999, 2) prints as "2.00"
  double rounding = 0.5;
  for (uint8_t i=0; i<digits; ++i)
    rounding *= 0.1;
    // rounding /= 10.0;  // <<<<<<<<<<<<<< more expensive than multiplication * 0.1
  
  number += rounding;

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)number;
  double remainder = number - (double)int_part;
  print(int_part);

  // Print the decimal point, but only if there are digits beyond
  if (digits > 0)
    print("."); 

  // Extract digits from the remainder one at a time
  while (digits-- > 0)
  {
    remainder *= 10.0;
    uint8_t toPrint = uint8_t (remainder);   // <<<<<<<<<<<<<<<< for 1 digit a byte is enough...
    print(toPrint, DEC);
    remainder -= toPrint; 
  } 
}

Sofar, in total 310 300 bytes less by looking carefully to the datatypes and operations in the print.cpp file

updated some numbers after a failing test.