Re: Spinlock backoff algorithm

From: Greg Smith <gsmith(at)gregsmith(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Spinlock backoff algorithm
Date: 2007-11-14 18:37:46
Message-ID: Pine.GSO.4.64.0711141309480.5503@westnet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, 14 Nov 2007, Mark Mielke wrote:

>> The other problem with using modulo is that it makes the result depend
>> mostly on the low-order bits of the random() result, rather than mostly
>> on the high-order bits; with lower-grade implementations of random(),
>> the lower bits are materially less random than the higher.

> If this was a serious problem, there is the >> operator. I see it as a poor
> coding practice to make assumptions about which bits are most "random" in a
> call to random().

There are many types of pseudo-random number generators where the
low-order bits are not so random, and the assumption Tom has described is
pretty likely to be true. See http://www.fourmilab.ch/random/ as one
comment about the badness of the standard UNIX random generator for
example.

There is an interesting discussion of this issue along with code showing a
way to improve things while only using integer math (which in some cases
uses >> as you suggest) as part of the Java standard library:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html#nextInt(int)

--
* Greg Smith gsmith(at)gregsmith(dot)com http://www.gregsmith.com Baltimore, MD

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2007-11-14 18:39:55 Re: a tsearch2 (8.2.4) dictionary that only filters out stopwords
Previous Message Tom Lane 2007-11-14 17:37:16 Re: a tsearch2 (8.2.4) dictionary that only filters out stopwords