Trying to understand Stream>>nextPutAll:

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Trying to understand Stream>>nextPutAll:

Damien Cassou-3-2
Hi,

browsing through the source code, i found the following methods with an
implementation I don't understand:


Stream>>nextPutAll: aCollection
  aCollection isSequenceable
    ifTrue: [self next: aCollection size putAll: aCollection startingAt: 1]
    ifFalse: [aCollection do: [:v | self nextPut: v]].
  ^ aCollection

and:

Stream>>next: anInteger putAll: aSequenceableCollection startingAt:
startIndex
  startIndex to: startIndex + anInteger - 1 do:
    [:index | self nextPut: (aSequenceableCollection at: index)].
  ^aSequenceableCollection


I don't understand the test within #nextPutAll:. Why is it necessary ?
Why not simply:

Stream>>nextPutAll: aCollection
  aCollection do: [:v | self nextPut: v].
  ^ aCollection

In fact, Squeak implement it like this.


Thank you


--
Damien

Reply | Threaded
Open this post in threaded view
|

Re: Trying to understand Stream>>nextPutAll:

kobetic
Probably speed. If you look at implementers of #next:putAll:startingAt:
you'll see that there are much faster ways of dumping 20K of bytes into
a file than going byte by byte. But commonly the assumption for the fast
alternatives is that the source collection is sequenceable.

Damien Cassou wrote:

> Hi,
>
> browsing through the source code, i found the following methods with an
> implementation I don't understand:
>
>
> Stream>>nextPutAll: aCollection
>  aCollection isSequenceable
>    ifTrue: [self next: aCollection size putAll: aCollection startingAt: 1]
>    ifFalse: [aCollection do: [:v | self nextPut: v]].
>  ^ aCollection
>
> and:
>
> Stream>>next: anInteger putAll: aSequenceableCollection startingAt:
> startIndex
>  startIndex to: startIndex + anInteger - 1 do:
>    [:index | self nextPut: (aSequenceableCollection at: index)].
>  ^aSequenceableCollection
>
>
> I don't understand the test within #nextPutAll:. Why is it necessary ?
> Why not simply:
>
> Stream>>nextPutAll: aCollection
>  aCollection do: [:v | self nextPut: v].
>  ^ aCollection
>
> In fact, Squeak implement it like this.
>
>
> Thank you
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Trying to understand Stream>>nextPutAll:

Damien Cassou-3-2
Martin Kobetic wrote:
> Probably speed. If you look at implementers of
> #next:putAll:startingAt: you'll see that there are much faster ways of
> dumping 20K of bytes into a file than going byte by byte. But commonly
> the assumption for the fast alternatives is that the source collection
> is sequenceable.

This was so easy that I missed it. Thank you. In fact, I forgot to
search for all implementors of the second message, I just looked at the
first one.


Thank you