| 1 | /* GNU Objective C Runtime class related functions
|
|---|
| 2 | Copyright (C) 1993, 1995, 1996, 1997, 2001 Free Software Foundation, Inc.
|
|---|
| 3 | Contributed by Kresten Krab Thorup and Dennis Glatting.
|
|---|
| 4 |
|
|---|
| 5 | Lock-free class table code designed and written from scratch by
|
|---|
| 6 | Nicola Pero, 2001.
|
|---|
| 7 |
|
|---|
| 8 | This file is part of GNU CC.
|
|---|
| 9 |
|
|---|
| 10 | GNU CC is free software; you can redistribute it and/or modify it under the
|
|---|
| 11 | terms of the GNU General Public License as published by the Free Software
|
|---|
| 12 | Foundation; either version 2, or (at your option) any later version.
|
|---|
| 13 |
|
|---|
| 14 | GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
|
|---|
| 15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|---|
| 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|---|
| 17 | details.
|
|---|
| 18 |
|
|---|
| 19 | You should have received a copy of the GNU General Public License along with
|
|---|
| 20 | GNU CC; see the file COPYING. If not, write to the Free Software
|
|---|
| 21 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|---|
| 22 |
|
|---|
| 23 | /* As a special exception, if you link this library with files compiled with
|
|---|
| 24 | GCC to produce an executable, this does not cause the resulting executable
|
|---|
| 25 | to be covered by the GNU General Public License. This exception does not
|
|---|
| 26 | however invalidate any other reasons why the executable file might be
|
|---|
| 27 | covered by the GNU General Public License. */
|
|---|
| 28 |
|
|---|
| 29 | /*
|
|---|
| 30 | The code in this file critically affects class method invocation
|
|---|
| 31 | speed. This long preamble comment explains why, and the issues
|
|---|
| 32 | involved.
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | One of the traditional weaknesses of the GNU Objective-C runtime is
|
|---|
| 36 | that class method invocations are slow. The reason is that when you
|
|---|
| 37 | write
|
|---|
| 38 |
|
|---|
| 39 | array = [NSArray new];
|
|---|
| 40 |
|
|---|
| 41 | this gets basically compiled into the equivalent of
|
|---|
| 42 |
|
|---|
| 43 | array = [(objc_get_class ("NSArray")) new];
|
|---|
| 44 |
|
|---|
| 45 | objc_get_class returns the class pointer corresponding to the string
|
|---|
| 46 | `NSArray'; and because of the lookup, the operation is more
|
|---|
| 47 | complicated and slow than a simple instance method invocation.
|
|---|
| 48 |
|
|---|
| 49 | Most high performance Objective-C code (using the GNU Objc runtime)
|
|---|
| 50 | I had the opportunity to read (or write) work around this problem by
|
|---|
| 51 | caching the class pointer:
|
|---|
| 52 |
|
|---|
| 53 | Class arrayClass = [NSArray class];
|
|---|
| 54 |
|
|---|
| 55 | ... later on ...
|
|---|
| 56 |
|
|---|
| 57 | array = [arrayClass new];
|
|---|
| 58 | array = [arrayClass new];
|
|---|
| 59 | array = [arrayClass new];
|
|---|
| 60 |
|
|---|
| 61 | In this case, you always perform a class lookup (the first one), but
|
|---|
| 62 | then all the [arrayClass new] methods run exactly as fast as an
|
|---|
| 63 | instance method invocation. It helps if you have many class method
|
|---|
| 64 | invocations to the same class.
|
|---|
| 65 |
|
|---|
| 66 | The long-term solution to this problem would be to modify the
|
|---|
| 67 | compiler to output tables of class pointers corresponding to all the
|
|---|
| 68 | class method invocations, and to add code to the runtime to update
|
|---|
| 69 | these tables - that should in the end allow class method invocations
|
|---|
| 70 | to perform precisely as fast as instance method invocations, because
|
|---|
| 71 | no class lookup would be involved. I think the Apple Objective-C
|
|---|
| 72 | runtime uses this technique. Doing this involves synchronized
|
|---|
| 73 | modifications in the runtime and in the compiler.
|
|---|
| 74 |
|
|---|
| 75 | As a first medicine to the problem, I [NP] have redesigned and
|
|---|
| 76 | rewritten the way the runtime is performing class lookup. This
|
|---|
| 77 | doesn't give as much speed as the other (definitive) approach, but
|
|---|
|
|---|