Page 1 of 1

Speed up for WriteS3

Posted: Tue Jul 21, 2009 1:41 am
by mrchadwickusa
Hi All,

Been messing with FTP to allow download of the current app and updating. Using Firefox with the Download Statusbar Extension, I noticed that the download speed from the Netburner was pretty punky, about 53KBytes per second average for a large app (3.36MB S3 file size). So looking over the code in StreamUpdate.cpp, at the bottom of the loop is WriteS3, which was using siprintf and strlen to add each byte to the output buffer.

I present here a modest change which results in more than a factor of 5 speed up, to 330KBytes per second.

mrc

Code: Select all

const char BinToHex[]={"0123456789ABCDEF"};

void WriteS3( int fd, unsigned long &cur_addr, unsigned char *&cp, unsigned long mklen )
{
   char *ufb = ( char * ) user_flash_buffer;
   int pos=0;
   char c;

   ufb[pos]= 'S';
   ufb[++pos]= '3';
   ufb[++pos]= BinToHex[((mklen + 5) >> 4)& 0x0f];
   ufb[++pos]= BinToHex[((mklen + 5) & 0x0f)];
   ufb[++pos]= BinToHex[(cur_addr >> 28)& 0x0f];
   ufb[++pos]= BinToHex[(cur_addr >> 24)& 0x0f];
   ufb[++pos]= BinToHex[(cur_addr >> 20)& 0x0f];
   ufb[++pos]= BinToHex[(cur_addr >> 16)& 0x0f];
   ufb[++pos]= BinToHex[(cur_addr >> 12)& 0x0f];
   ufb[++pos]= BinToHex[(cur_addr >>  8)& 0x0f];
   ufb[++pos]= BinToHex[(cur_addr >>  4)& 0x0f];
   ufb[++pos]= BinToHex[(cur_addr) & 0x0f];

//   siprintf( ufb, "S3%02X%08X", mklen + 5, cur_addr );
   unsigned char * cpsum = ( unsigned char * ) &cur_addr;
   unsigned char csum;
   csum = ( mklen + 5 ) + cpsum[0] + cpsum[1] + cpsum[2] + cpsum[3];

   while ( mklen )
   {
      c = *cp;
      ufb[++pos]= BinToHex[(c >> 4)&0x0f];
      ufb[++pos]= BinToHex[(c & 0x0f)];
//      siprintf( ( char * ) ufb + strlen( ufb ), "%02X", ( int ) * cp );
      csum += c;
      ++cp;
      ++cur_addr;
      --mklen;
   }

   csum = ( ~csum );
   ufb[++pos]= BinToHex[(csum >> 4)&0x0f];
   ufb[++pos]= BinToHex[(csum & 0x0f)];
   ufb[++pos]= '\r';
   ufb[++pos]= '\n';
   ufb[++pos]= NULL;
//   siprintf( ( char * ) ufb + strlen( ufb ), "%02X\r\n", csum );
   writestring( fd, ufb );
}


Re: Speed up for WriteS3

Posted: Tue Jul 21, 2009 11:35 am
by lgitlitz
Hi,
This does look like it should be much more efficient. If you do not mind we will update the StreamUpdate code for future builds with your code. I did tests on the FTPD_AppUpdate using Firezilla as my client. I downloaded the application at about 50KB/s with the original code. Then when I switched to your new code I am in the 500-600KB/s range with a max of about 750KB/s, I had to make an HTML folder with some binary files to increase the application size so I could actually see the speed.
-Larry

Re: Speed up for WriteS3

Posted: Tue Jul 21, 2009 1:47 pm
by Ridgeglider
Larry: While you're at it, I find the ftp calls work much more efficiently, particularly with large directories if the function in ftp_f.cpp called gettimedate() is edited to remove iprintf statements. This file has some other iprintfs that could stand commenting depending on the level of debug req'd. These comments go out stdio so it can be a bit surprising if you're using the port for something else. It might be nice to have a compile directive to allow them to be turned on/off? I tensd to like the ones showing a problem, but comment the others.

Code: Select all

static void gettimedate( F_FIND *f )
{
   unsigned short t, d;

   int nret = f_gettimedate( f->filename, &t, &d );
   if ( nret == F_NO_ERROR )
   {
    //////      iprintf( "%15s   |", f->filename );
    //////      iprintf( "%2.2d:%2.2d:%2.2d   |", ( ( t & 0xF800 ) >> 11 ), ( ( t & 0x07E0 ) >> 5 ), 2 * ( t & 0x001F ) );
    //////      iprintf( "%2.2d/%2.2d/%4d   |", ( ( d & 0x01E0 ) >> 5 ), ( d & 0x001F ), ( 1980 + ( ( d & 0xFE00 ) >> 9 ) ) );
    //////      iprintf( "%9ld Bytes\r\n", f->filesize );
   }
   else
   {
      iprintf( "Time stamp retrieval failed: %d\r\n", nret );
   }
}

Re: Speed up for WriteS3

Posted: Wed Jul 22, 2009 12:22 am
by mrchadwickusa
Hi Larry,

You are welcome to use my version. I'm pleased I can give something back to the Netburner community.

mrc