[Pharo-project] Preallocation behavior

Henrik Sperre Johansen henrik.s.johansen at veloxit.no
Thu Apr 28 12:25:54 CEST 2011

On 28.04.2011 12:12, Toon Verwaest wrote:
> nextPut: anObject
>     "Primitive. Insert the argument at the next position in the Stream
>     represented by the receiver. Fail if the collection of this stream 
> is not an
>     Array or a String. Fail if the stream is positioned at its end, or 
> if the
>     position is out of bounds in the collection. Fail if the argument 
> is not
>     of the right type for the collection. Optional. See Object 
> documentation
>     whatIsAPrimitive."
> <primitive: 66>
>     position >= writeLimit
>         ifTrue: [^ self pastEndPut: anObject]
>         ifFalse:
>             [position := position + 1.
>             ^collection at: position put: anObject]
> The primitive will only fail if you are trying to write past the end. 
> This check is very fast since it's primitive... and we'll only go in 
> Smalltalk code when it fails. The first version on the other hand is 
> always first some smalltalk code (quite a lot for your basic example) 
> before it goes into a slightly more heavy primitive 
> (replaceFrom:to:with:startingAt:).
To be exact, the primitive always fails on Cog (not implemented, jitted 
version is just as fast), and only works for non-ReadWriteable streams 
on newish (last year or so) VM's :)

The constant overhead is still lower than nextPutAll: of course.


More information about the Pharo-project mailing list