Closed Bug 562809 Opened 14 years ago Closed 14 years ago

js::HashTable::init(n) should guarantee success of next 'n' add()s

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: dwitte, Assigned: dwitte)

References

Details

(Whiteboard: fixed-in-tracemonkey)

Attachments

(1 file)

http://mxr.mozilla.org/mozilla-central/source/js/src/jshashtable.h#290

Currently it rounds 'n' up to the nearest power of 2, and sizes the hash thusly. But when add()ing an element, alpha gets involved, which may result in an appropriately-presized hash being expanded.

It would be nice if init() took alpha into account, such that those 'n' add()s could be guaranteed.
I was wrong on IRC, this is the same as JS_DHashTableInit. The parameter name is capacity, so rounding up to power of two is the right thing given the meaning of the parameter. Callers use JS_DHASH_DEFAULT_CAPACITY(n) where n is the number of initial entries.

Looking back, this seems silly to foist off on all callers. I may have been thinking that Init would be called with a guess at capacity rather than an exact (or simpler better guess at) number of initial or near-term entries. Anyway, if jshashtable.h takes the more usable route, please document the difference.

We should try to retire jsdhash/pldhash at some point, in favor of jshashtable. I remember Luke worrying about template bloat, but let's find out how bad of a problem that is.

/be
Attached patch patchSplinter Review
Uses a statically defined sInvMaxAlpha. I went with a << 7 shift, rather than 8, to keep it fitting in a uint8. (Which is useful so that the max table size of 2^24 is still achievable.)
Assignee: general → dwitte
Status: NEW → ASSIGNED
Attachment #443691 - Flags: review?(lw)
Comment on attachment 443691 [details] [diff] [review]
patch

Looks good, thanks!

>+        /* Correct for sMaxAlphaFrac such that the table will not resize
>+           when adding 'length' entries. */

Nit: SM does:

 /*
  * comments
  */
Attachment #443691 - Flags: review?(lw) → review+
Whiteboard: fixed-in-tracemonkey
http://hg.mozilla.org/mozilla-central/rev/2cc1ad9809b2
Status: ASSIGNED → RESOLVED
Closed: 14 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: