<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">2014/1/19 Levente Uzonyi <span dir="ltr">&lt;<a href="mailto:leves@elte.hu" target="_blank">leves@elte.hu</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Sun, 19 Jan 2014, Levente Uzonyi wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Sun, 19 Jan 2014, Levente Uzonyi wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
On Sun, 19 Jan 2014, David T. Lewis wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Sun, Jan 19, 2014 at 06:06:37PM +0100, Levente Uzonyi wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Sun, 19 Jan 2014, Nicolas Cellier wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
It&#39;s a bug. Let&#39;s remove the optimizatoin. If we really want it, this<br>
could be a implemented like<br>
<br>
WriteStream&gt;&gt;sharedContents? | sz | ^(sz := position max: readLimit) =<br>
collection size ifTrue: [collection] ifFalse: [collection copyFrom: 1 to:<br>
sz]<br>
</blockquote>
<br>
#new:streamContents: is about optimization, nothing else. I think it&#39;s a<br>
better solution to set the stream to the end before comparing the position<br>
with the size, because I don&#39;t see any other possible use for<br>
#sharedContents.<br>
</blockquote>
<br>
Do you mean something like this?<br>
<br>
 new: newSize streamContents: blockWithArg<br>
     | stream p endPos |<br>
     stream := WriteStream on: (self new: newSize).<br>
     blockWithArg value: stream.<br>
     p := stream position.<br>
</blockquote>
<br>
It doesn&#39;t matter what the current position of the stream is. If there&#39;s data after the current position, then #contents will return it. So I think this is<br>
</blockquote>
<br>
Um, it seems like #contents only cares about position, but not readLimit (even though it updates readLimit), which may or may not be a bug...<br>
</blockquote>
<br></div>
Well, it&#39;s not a bug, but me being a bit sleepy. All we need to do is to check if the stream is still using the original collection.<br>
<br>
new: newSize streamContents: blockWithArg<br>
<br>
        | stream collection |<br>
        collection := self new: newSize.<br>
        stream := WriteStream on: collection.<br>
        blockWithArg value: stream.<br>
        (stream originalContents == collection and: [ stream position = newSize ])<br>
                ifTrue: [ ^collection ]<br>
                ifFalse: [ ^stream contents ]<br>
<br>
Or another variant, which checks the size of #originalContents.<br>
<br>
new: newSize streamContents: blockWithArg<br>
<br>
        | stream originalContents |<div class="im"><br>
        stream := WriteStream on: (self new: newSize).<br>
        blockWithArg value: stream.<br></div>
        originalContents := stream originalContents.<br>
        (originalContents size = newSize and: [ stream position = newSize ])<br>
                ifTrue: [ ^originalContents ]<br>
                ifFalse: [ ^stream contents ]<span class="HOEnZb"><font color="#888888"><br>
<br>
<br></font></span></blockquote><div><br></div><div>Or to profit by luck: we pre-allocated 100, the originalContents grew to 200 but precisely fits stream position:<br><br></div><div>originalContents size = stream position ifTrue: [^originalContents]<br>
<br><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="HOEnZb"><font color="#888888">
Levente</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
Levente<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
how the method should be:<br>
<br>
new: newSize streamContents: blockWithArg<br>
<br>
        | stream |<br>
        stream := WriteStream on: (self new: newSize).<br>
        blockWithArg value: stream.<br>
        stream setToEnd position = newSize<br>
                ifTrue: [ ^stream originalContents ]<br>
                ifFalse: [ ^stream contents ]<br>
<br>
<br>
Levente<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
     endPos := stream setToEnd position.<br>
     p = endPos<br>
         ifTrue: [p = newSize<br>
                 ifTrue: [^ stream originalContents]]<br>
         ifFalse: [stream position: p].<br>
     ^ stream contents<br>
<br>
<br>
I&#39;m not sure what that would do to the performance gains.<br>
<br>
Dave<br>
<br>
<br>
<br>
</blockquote>
<br>
<br>
</blockquote>
<br>
<br>
</blockquote>
<br>
</div></div></blockquote></div><br></div></div>