public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Faster Reference Counting [was faster compilation speed]
@ 2002-09-02 14:41 Eliot Miranda
  0 siblings, 0 replies; only message in thread
From: Eliot Miranda @ 2002-09-02 14:41 UTC (permalink / raw)
  To: gcc


Tim Hollebeek <tim at hollebeek dot com> wrote:

[deletia]

> A simpler strategy is to just make every object with RC >= 2^n
> immortal.  Something like:

> add: if (rc) rc++;

> subtract: if (rc) if (!--rc) delete;

> For a program like gcc that doesn't have to absolutely guarantee it
> leaks no memory, this can be an acceptable tradeoff.

> (for belt and suspenders people, run a infrequent lazy gc to clean up
> the scraps)

> -Tim

I chanced upon the gcc compilation speed thread through gclist (a
GC-specific mailing list).  Peter Deutsch invented a nice trick
for cheaper immortality using byte arithmetic.  The idea is for a
count operation to add or subtract two and mark an overflowing count
odd so that it can never go to zero.  This saves a test in decrement.

e.g. If add/subtract 1 with sign bit for immortality you get

incrc(obj)
{       if (obj->rc >= 0)
                ++obj->rc;
}
decrc(obj)
{       if (obj->rc >= 0)
                if (--obj->rc == 0)
                        reclain(obj);
}

but with add/subtract 2 you get
incrc(obj)
{       if ((obj->rc += 2) == 0)
                obj->rc = 1;
}
decrc(obj)
{       if ((obj->rc -= 2) == 0)
                reclaim(obj);
}

which saves a test on decrement, or about 25% of your ref counting cost
(and note that recursive freeing in reclaim is all decrements).

Also, recursive freeing can be made tail-recursive and hence written
as a loop (this from Glenn Krasner).

HTH...

-- 
_______________,,,^..^,,,____________________________
Eliot Miranda              Smalltalk - Scene not herd

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-09-02 21:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-02 14:41 Faster Reference Counting [was faster compilation speed] Eliot Miranda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).