* Peter da Silva <pe...@taronga.com> [2007-08-02 21:45]:
> >  Please have the length of strings upfront.
> 
> Length encoding isn't self-syncing,

Synching the length is O(1).

Not doing it makes *everything* O(n) and leaves you to deal with
the semi-predicate problem, source of lots of 'sploits and other
fun for the whole family.

Pick your poison; I know which one I prefer.

> and it also limits the length of individual parameters by the
> size of the length.

So what? Just make sure the counter is wide enough for any string
you can keep in memory.

Wasteful? So use a variable-width encoding for the length. It can
be brutally stupid as it only needs to be competitive with NUL
termination on very short strings. Using a single byte for
strings < 128 bytes and a machine word for anything longer will
do just fine.

If you use the lowest bit as a flag to signify the type, then
massaging the counter takes under 2 cycles on average with
optimal machine code: ridiculously fast -- a common theme with
operations on length-prefixed strings.

That C and UNIX immortalised zero-terminated strings amounts to a
crime if you consider the amount of CPU cycles that have since
been wasted scanning for NULs.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Reply via email to