I'm writing Inline::C wrappers over some code that reads and writes LZW compressed data (/usr/bin/compress and /usr/bin/uncompress). That code is already very speedy and copies the data one character at a time into a buffer. I'm having perl manage the buffer as a plain string and I'm growing it whenever the pointer reaches the end.
I'm not experienced with C. Tell me if I'm doing something wrong.
Is there a good value for SZSIZE? I just picked 8192 out of the air because it seemed reasonable. I'd like to avoid fragmenting memory too much. I'd also like to prevent unnecessary copying. Are there any good strategies for serving these goals? Is there some magic value associated with modern (or not so modern) perl that I'd want to follow?
#define SZSIZE 8192 SV* LZW_zread( ... ) { SV* retval; u_char bp, end_bp; u_int offset; ... retval = NEWSV( ..., SZSIZE ); SvPOK_only( retval ); bp = (u_char*)SvPVX( retval ); end_bp = SZSIZE + bp; ... while ( ... ) { if ( bp == end_bp ) { /* When bp is == end_bp then the buffer must be grown. bp is cur +rently just off the end of the buffer and any operations on it is un +safe. */ offset = bp - PvPVX(retval); sv_grow(sv, offset + SZSIZE); bp = PvPVX(retval) + offset; end_bp = SZSIZE + bp; } ... *bp++ = ...; } ... SvCUR_set(sv, bp - SvPVX(retval)); return retval; }
In reply to Growing strings, avoiding copying and fragmentation? by diotalevi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |