Re: George's Buffers and bits

From: George (
Date: 03/02/99

On Tue, 2 Mar 1999, Chris Jacobson wrote:

>I was reviewing George's Buffer code (I run a mix of 1.9 and 2.0 and my
>own mods), and realized the magnitude/bitcounting/rounding code was a bit
>... superfluous.
>George, did you compare the current code for finding the next largest
>power of 2, to the simple loop:
>for (i = 0; i < 32; i++)
>     if ((1 << i) >= size)
>          break;

You mean round_to_pow2?  It would be this without shortcuts:

static size_t round_to_pow2(size_t y)
  size_t x = (1 << SIZE_T_HIGHBIT);     /* Set the highest bit possible... */

  while ((y & x) == 0)  /* Scan for the highest bit in target. */
    x >>= 1;            /* Bit not found in target, shift mask right one bit. */

  if (x == y)   /* If the numbers are equal, it's already rounded. */
    return x;
  return (x << 1);      /* Numbers don't match, round up to compensate. */

>Normally I would presume you did, but the current code seems like such
>overkill that I am left to wonder if someone had spiked your coffee that
>day <g>, if you were just feeling creative, if your routines had other
>purposes that have yet to reach the light of day in Pre-2.0 code, or if
>they are actually faster.

I was bored and benchmark happy.  If you're interested, take a look at

It goes from 3,600 usec to 8,800 usec without the shortcuts for an even
distribution from 32,767 to 0.  I expected those to be the standard values
requested in the buffer allocations.

As given, yours took ~19,000 usec for the same loop.  There's a reason I
shift the value first and then shift it down one every time.  Making it
shift the whole thing every time is slow.

If I change yours to:

static size_t chris_round(unsigned int size)
  unsigned int i = (1 << 0);

  for (i = (1 << 0); i < (1 << 31); i <<= 1)
    if (i >= size)

  return i;

And remove my shorcuts, then it compares as:

#1 (my) : 8882 usec
#2 (cr) : 7325 usec

Otherwise, with shortcuts:

#1 (my) : 3576 usec
#2 (stk): 6520 usec

But I suppose they could be added to yours somehow, I'll look into it

George Greer

     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  |

This archive was generated by hypermail 2b30 : 12/15/00 PST