<div dir="auto">Thx Eliot for that detailed description. What might be the impact of a default-pin every object passed to FFI?<div dir="auto"><br></div><div dir="auto">Cheers -ben</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 30 Aug 2017 11:46 PM, "Eliot Miranda" <<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>> wrote:<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Torsten, Hi All,<br>
<br>
   re pinDuring:<br>
<div class="quoted-text"><br>
> On Aug 28, 2017, at 9:17 AM, Torsten Bergmann <<a href="mailto:astares@gmx.de">astares@gmx.de</a>> wrote:<br>
><br>
</div><div class="quoted-text">> As some of you are aware of Spur added memory pinning and in Pharo 6.1 (#60510) as well as Pharo 7<br>
> we currently have<br>
><br>
> Object>>pin<br>
> Object>>unpin<br>
> Object>>isPinned<br>
><br>
> and also Object>>setPinned:.<br>
><br>
> Object pinning is explained by Eliot here [1].<br>
><br>
> I would propose two changes (at least for Pharo but ideally also for Squeak):<br>
><br>
> 1. IMHO #setPinned: should be moved to a private protocol<br>
> 2. Provide a #pinDuring: aBlock method like:<br>
><br>
>      pinDuring: aBlock<br>
>          [ self pin.<br>
>            aBlock value ] ensure: [ self unpin ]<br>
><br>
>    so we can ensure that a pinned object is unpinned after an operation.<br>
<br>
</div>From an implementation perspective, I suspect this is unnecessary.  There are two reasons, one to do with collecting pinned objects and the other to do with compaction.<br>
<br>
Pinning an object forces it into oldSpace if it was not there already, but does not prevent it being garbage collected. So any un references pinned object in oldSpace will be collected at the next full GC.<br>
<br>
It is possible (but in implemented) to move not large objects back to newSpace but the forwarder left in oldSpace will prevent its collection until the next full GC so trying to move an object back into newSpace is pointless.  Conclusion, there is no point unpinning for garbage collectability.<br>
<br>
The compactor does a good job of moving unpinned objects around pinned objects, typically leaving very small gaps before pinned objects.  [One can observe how much free space exists in an image file to get an idea of how much free space this causes.  It's low]. The number of uncollectable pinned objects in an application, which will be objects that are long-lived that have been passed through the FFI, is likely to be small.  Conclusion there is little to be gained in avoiding fragmentation by unpinning long-lived objects pinned on an FFI call.<br>
<br>
<br>
In summary, collectible ones objects will be collected anyway, and the few long-lived pinned objects should not cause significant fragmentation.  So pinDuring: is unlikely to provide any benefit.  Simply pin and leave it to the GC and its compactor to clean up afterwards.<br>
<div class="elided-text"><br>
> What do you think?<br>
><br>
> Also I wonder why FFICallbackTests>>#<wbr>testCqsortWithByteArray is sending #pin but not<br>
> #unpin afterwards. Maybe that is the reason why there is a flag in this method that it<br>
> is crashing on Windows.<br>
><br>
> Bye<br>
> T.<br>
><br>
> [1] <a href="http://lists.squeakfoundation.org/pipermail/squeak-dev/2017-August/195129.html" rel="noreferrer" target="_blank">http://lists.squeakfoundation.<wbr>org/pipermail/squeak-dev/2017-<wbr>August/195129.html</a><br>
<br>
</div></blockquote></div><br></div>