This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: GNU ObjC runtime class table rewritten
- To: Stan Shebs <shebs at apple dot com>
- Subject: Re: GNU ObjC runtime class table rewritten
- From: Nicola Pero <nicola at brainstorm dot co dot uk>
- Date: Wed, 30 May 2001 12:31:49 +0100 (BST)
- cc: Nicola Pero <n dot pero at mi dot flashnet dot it>, Ovidiu Predescu <ovidiu at cup dot hp dot com>, gcc-patches at gcc dot gnu dot org
> So what I ask is that you take the details in your message,
> cast them into an appropriate form for a comment block at
> the top of class.c, and resubmit - I'll take that version and
> install it as the new class.c in the trunk.
Ok - sounds like a good idea - here in plain is the comment - in attach
the new class.c file.
/*
The code in this file critically affects class method invocation
speed. This long preamble comment explains why, and the issues
involved.
One of the traditional weaknesses of the GNU Objective-C runtime is
that class method invocations are slow. The reason is that when you
write
array = [NSArray new];
this gets basically compiled into the equivalent of
array = [(objc_get_class ("NSArray")) new];
objc_get_class returns the class pointer corresponding to the string
`NSArray'; and because of the lookup, the operation is more
complicated and slow than a simple instance method invocation.
Most high performance Objective-C code (using the GNU Objc runtime)
I had the opportunity to read (or write) work around this problem by
caching the class pointer:
Class arrayClass = [NSArray class];
... later on ...
array = [arrayClass new];
array = [arrayClass new];
array = [arrayClass new];
In this case, you always perform a class lookup (the first one), but
then all the [arrayClass new] methods run exactly as fast as an
instance method invocation. It helps if you have many class method
invocations to the same class.
The long-term solution to this problem would be to modify the
compiler to output tables of class pointers corresponding to all the
class method invocations, and to add code to the runtime to update
these tables - that should in the end allow class method invocations
to perform precisely as fast as instance method invocations, because
no class lookup would be involved. I think the Apple Objective-C
runtime uses this technique. Doing this involves synchronized
modifications in the runtime and in the compiler.
As a first medicine to the problem, I [NP] have redesigned and
rewritten the way the runtime is performing class lookup. This
doesn't give as much speed as the other (definitive) approach, but
at least a class method invocation now takes approximately 4.5 times
an instance method invocation on my machine (it would take approx 12
times before the rewriting), which is a lot better.
One of the main reason the new class lookup is so faster is because
I implemented it in a way that can safely run multithreaded without
using locks - a so-called `lock-free' data structure. The atomic
operation is pointer assignment. The reason why in this problem
lock-free data structures work so well is that you never remove
classes from the table - and the difficult thing with lock-free data
structures is freeing data when is removed from the structures. */
class.c.gz