Re: calculating CRC

  • From: Mike Pall <mike-1205@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 30 May 2012 15:15:31 +0200

William Adams wrote:
> 1) Would I be better off doing a cast of 's' in the beginning,
> and then using simple array access after that?  Any great
> advantage over just doing the getbytes?  My assumption is it
> would be faster, but I don't know of other tradeoffs.

It should be slightly faster. Not all range checks can be
eliminated for string.byte().

> 2) Should I switch the Digest.consts table to being an array of
> in32_t instead of a Lua table?  Will it be more efficient in
> some way, or some other benefit?

It avoids bounds checks, type checks and plenty of double -> int
conversions (in single-number mode, default on x86/x64). So yes,
it would make a difference.

BTW: string.len(s) is compatibility from Lua 5.0. Better use #s.

BTW#2: Try -jdump on a sample snippet only testing this code. Even
if you don't know much about assembler, you can still judge the
length of the machine code inside the 'LOOP'. Shorter is better,
less branches and less loads/stores are better. Beware: it's
addictive to stare at compiler output. :-)

--Mike

Other related posts: