From radford at cs.toronto.edu  Thu Feb 19 03:16:19 2015
From: radford at cs.toronto.edu (Radford Neal)
Date: Wed, 18 Feb 2015 21:16:19 -0500
Subject: [Rd] Recycling memory with a small free list
In-Reply-To: <CAFAN8vzYfOBKHq5b-H=-C_ZPYBK6E03Q5-jqPE3ASgqV15qq_g@mail.gmail.com>
References: <mailman.19.1424257206.2915.r-devel@r-project.org>
	<20150218151957.GA1174@cs.toronto.edu>
	<CAFAN8vzYfOBKHq5b-H=-C_ZPYBK6E03Q5-jqPE3ASgqV15qq_g@mail.gmail.com>
Message-ID: <20150219021619.GA18066@cs.toronto.edu>

Radford Neal:
> > there's a danger of getting carried away and essentially rewriting
> > malloc.  To avoid this, one might try just calling "free" on the
> > no-longer-needed object, letting "malloc" then figure out when it can
> > be re-used.

Nathan Kurz:
> Yes, I think that's what I was anticipating:  add a free() equivalent...

Radford Neal:
> > Unfortunately, that seems not to be safe, because it's
> > possible that there is a reference to the no-longer-needed object on
> > the PROTECT stack, even though no one should actually be looking at
> > it any more.

Nathan Kurz:
> Can you explain this case?   I don't think I understand it.


My comment about it not being safe was referring to actually calling
the "free" function in the standard C library, not a "free equivalent".  
Except for small objects, R calls "free" when it concludes that it no
longer needs an object, having allocated space for it earlier with
"malloc".  After "free" is called, R had better not do anything like try
to mark it in the garbage collection phase...

Keeping a free list apart from the one maintained by malloc/free would
I think have to be how it is done, hence my comment about ending up
rewriting malloc/free.  But it may not be too hard to restrain oneself
and only do the simplest things.

   Radford Neal



[Rd] Recycling memory with a small free list
Radford Neal