The idea that a set is always created with a minimal size is because we always add at least some elements to them. We at some point increased that size (from 3 to 5?). The difference comes from Set sizeFor: 1 ==> 7 HashedCollection>>#sizeFor: has a special case for small (<4) elements, while the one of Set does not. Which is odd. Marcus
On 1 Dec 2016, at 14:39, Christophe Demarey <christophe.demarey@inria.fr> wrote:
Thanks for this great explanation Henrik. Nevertheless, my question is still valid. Why Set new will give me an internal array of 5 elements while Set new: 1 will give me an internal array of 7 elements? Shouldnât Set new give an internal of 7 elements as it looks like the minimal array size for a set? Also I would expect Set new: 0 to give me an empty internal array. It explicitly ask an empty Set. A further call to #add: will increase its size as expected.
Le 1 déc. 2016 à 13:25, Henrik Nergaard <henrik.nergaard@uia.no <mailto:henrik.nergaard@uia.no>> a écrit :
Hashed collections keep a portion of the available memory empty (usually 25%) as a partition mechanism to increase lookup speed.
âNormal arrayâ | col |
col := Smalltalk allClassesAndTraits asArray.
[ col includes: 8; includes: Morph ] bench. "'366.853 per second'"
âNormal set ~75% loadâ | set |
set := Smalltalk allClassesAndTraits asSet.
[ set includes: 8; includes: Morph ] bench. "'4,700,967 per second'"
âSet forced to have ~100% loadâ | array fullLoadSet |
array := Smalltalk allClassesAndTraits asSet asArray , #(nil). fullLoadSet:= Set basicNew. fullLoadSet instVarNamed: #array put: array; instVarNamed: #tally put: array size.
[ fullLoadSet includes: 8; includes: Morph ] bench. "'215.157 per second'"
Best regards, Henrik
From: Pharo-dev [mailto:pharo-dev-bounces@lists.pharo.org <mailto:pharo-dev-bounces@lists.pharo.org>] On Behalf Of Christophe Demarey Sent: Thursday, December 1, 2016 11:49 AM To: Pharo Development List <pharo-dev@lists.pharo.org <mailto:pharo-dev@lists.pharo.org>> Subject: [Pharo-dev] Set internal array size
Hi,
I just discovered a funny thing. If you create a Set with Set new, you will get a Set with an internal array of 5 elements (hardcoded in Set class>>#new). If you ask explicitly a Set for one or no element: Set new: 0 (or a number between 0 and 5), you will get an internal array of 7 elements. This behavior is a bit strange. Either we always need at least 7 elements and we need to update Set class>>#new, either the method Set class>>#sizeFor: should be updated.
WDYT?
Christophe