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.nerga...@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-boun...@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>
> 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

Reply via email to