[Glass] Cost of includesKey in RcKeyValueDictionary

BrunoBB smalltalk at adinet.com.uy
Wed Feb 26 13:50:26 PST 2020


A domain object (A) has a RcKeyValueDictionary to store large quantity of
(B) objects.

B objects has a random id generated with: 
id := Lag1MwcRandom new integer. "i think the max number is 4294967295"

The problem here is that with 100.000 (B) objects the probability of id
clash is pretty high.

It can be fixed with #includesKey: (inside a loop :( ) but which is the cost
of sending #includesKey: in RcKeyValueDictionary ? 

It all reduce to #_at: but is a primitive. As far as i remember in a Martin
McClure presentation about Hash functions the cost should NOT be to much.
But just want to confirm this :)

Maybe a better solution it will be to use persistent counters ?
Or maybe a UUID  ? (which has the same problem but a lower clash


At first it seems that the loop is NOT a good idea but the collections of
ids will be already generated and the #includesKey: does not take to long.

| numbers instances r int milli clashes|
numbers := Dictionary new.
r := Lag1MwcRandom new.
instances := 900000.
1 to: instances do: [:each | int := r integer. 
				numbers at: int put: int]. "already generated collection"

clashes := 0.
milli := Time millisecondsToRun: [ "new ids"
1 to: 10000 do: [:each | int := r integer. 
				[numbers includesKey: int] whileTrue: [clashes := clashes + 1. int :=
int +1]. 
				numbers at: int put: int].
Array with: clashes with: milli.

Sent from: http://forum.world.st/GLASS-f1460844.html

More information about the Glass mailing list