Traversing a binary

Peter-Henry Mander erlang@REDACTED
Wed Sep 22 10:36:23 CEST 2004


Thanks everyone,

Method four is a worthy runner-up, and seems more generic way of parsing
binaries too. Thanks for the debate, it's been an eye-opener.

Pete.

P.s. for the record:

10> c(tc,[native]).   
{ok,tc}
11> tc:t().           
M1: {534026,5355}
M2: {502536,5355}
M3: {120267,5355}
M4: {174551,5355}
ok


On Wed, 22 Sep 2004 00:11:40 +0200
Erik Stenman <erik.stenman@REDACTED> wrote:

> 
> >I assume that a bignum is an integer bigger than the word size of the
> >machine (Correct me if i'm wrong). I don't think i can have a checksum
> >bigger than 4Gb. The biggest packet i may have is 64Kb (Because of ip).
> >So, in the worst case there would be 2^15 two byte pairs. If all are
> >16#ffff that would add to 2^31, which fits in the 32 bits machine word.
> >
> >  
> >
> Unfortunately the tag-scheme used for Erlang takes some bits from the 
> integers (i think it is 4
> bits at the moment, but it is at least 2).
> With a limited packet size it might be that bignums are created very seldom.
> 
> >>   You could also traverse the binary without creating new sub-binaries, by
> >>   keeping
> >>   track of how many byte-pairs you have seen (N):
> >>    
> >>
> >That's something i hadn't think about. Seems a good idea.
> >  
> >
> 
> >I'll try it then. Maybe it's fast enough.
> >
> >  
> >
> If you compile to native code with hipe, then method 3 (in my small 
> benchmark file) seems to be the fastest.
> 
> /Erik


-- 
"The Tao of Programming
 flows far away 
 and returns 
 on the wind of morning."




More information about the erlang-questions mailing list