<div dir="ltr">I removed the invariant by adding a new primop, addCFinalizerToWeak#. I opened a ticket for the issue.<br><br><a href="http://hackage.haskell.org/trac/ghc/ticket/7847">http://hackage.haskell.org/trac/ghc/ticket/7847</a><br>
<br>- Akio<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Mar 21, 2013 at 2:40 PM, Simon Marlow <span dir="ltr">&lt;<a href="mailto:marlowsd@gmail.com" target="_blank">marlowsd@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 11/03/13 03:17, Akio Takano wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I&#39;m working on implementing per-generation lists of weak pointers to<br>
speed up garbage collection in programs that allocate a lot of weak<br>
pointers. I have a patch [1] that validates and gives a 3x speed up on<br>
a benchmark. However I&#39;d like to ask for some advise before finishing<br>
and submitting the patch.<br>
<br>
[1] <a href="https://github.com/takano-akio/ghc/commit/c7345c68eaa1e7f9572e693b5e352e386df7d680" target="_blank">https://github.com/takano-<u></u>akio/ghc/commit/<u></u>c7345c68eaa1e7f9572e693b5e352e<u></u>386df7d680</a><br>

<br>
The problem is that since my patch splits the weak pointer list<br>
between generations, it no longer maintains the right order of weak<br>
pointers. This could cause finalizers added with<br>
addForeignPtrFinalizer to run in the wrong order.<br>
<br>
I can think of one way to fix it; to make sure that when a WEAK object<br>
gets promoted, it is always added to the front of the new list. So my<br>
questions are:<br>
<br>
- Would it be a correct fix?<br>
- If so, is it an acceptable fix? For example, is it too fragile a<br>
reasoning to rely on?<br>
</blockquote>
<br></div></div>
I don&#39;t like the way that we rely on the ordering of the weak pointer list right now.  I think this invariant arose accidentally when the support for C finalizers was added.  It was wrong for some time, see:<br>
<br>
<a href="http://hackage.haskell.org/trac/ghc/ticket/7160" target="_blank">http://hackage.haskell.org/<u></u>trac/ghc/ticket/7160</a><br>
<br>
and as per my comments in that commit log, I think we should do it differently.  I don&#39;t know how hard that would be though.<br>
<br>
Incidentally, I implemented per-generation weak pointers in the local-gc branch, but didn&#39;t get around to porting it back over into the mainline (I still have a ToDo for that).  My version probably has the ordering bug, but you could always look at the branch to see how my approach compares to yours.<br>

<br>
Cheers,<br>
        Simon<br>
<br>
</blockquote></div><br></div>