| 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
|
|---|
| 78 | at least a class method invocation now takes approximately 4.5 times
|
|---|
| 79 | an instance method invocation on my machine (it would take approx 12
|
|---|
| 80 | times before the rewriting), which is a lot better.
|
|---|
| 81 |
|
|---|
| 82 | One of the main reason the new class lookup is so faster is because
|
|---|
| 83 | I implemented it in a way that can safely run multithreaded without
|
|---|
| 84 | using locks - a so-called `lock-free' data structure. The atomic
|
|---|
| 85 | operation is pointer assignment. The reason why in this problem
|
|---|
| 86 | lock-free data structures work so well is that you never remove
|
|---|
| 87 | classes from the table - and the difficult thing with lock-free data
|
|---|
| 88 | structures is freeing data when is removed from the structures. */
|
|---|
| 89 |
|
|---|
| 90 | #include "runtime.h" /* the kitchen sink */
|
|---|
| 91 | #include "sarray.h"
|
|---|
| 92 |
|
|---|
| 93 | #include <objc/objc.h>
|
|---|
| 94 | #include <objc/objc-api.h>
|
|---|
| 95 | #include <objc/thr.h>
|
|---|
| 96 |
|
|---|
| 97 | /* We use a table which maps a class name to the corresponding class
|
|---|
| 98 | * pointer. The first part of this file defines this table, and
|
|---|
| 99 | * functions to do basic operations on the table. The second part of
|
|---|
| 100 | * the file implements some higher level Objective-C functionality for
|
|---|
| 101 | * classes by using the functions provided in the first part to manage
|
|---|
| 102 | * the table. */
|
|---|
| 103 |
|
|---|
| 104 | /**
|
|---|
| 105 | ** Class Table Internals
|
|---|
| 106 | **/
|
|---|
| 107 |
|
|---|
| 108 | /* A node holding a class */
|
|---|
| 109 | typedef struct class_node
|
|---|
| 110 | {
|
|---|
| 111 | struct class_node *next; /* Pointer to next entry on the list.
|
|---|
| 112 | NULL indicates end of list. */
|
|---|
| 113 |
|
|---|
| 114 | const char *name; /* The class name string */
|
|---|
| 115 | int length; /* The class name string length */
|
|---|
| 116 | Class pointer; /* The Class pointer */
|
|---|
| 117 |
|
|---|
| 118 | } *class_node_ptr;
|
|---|
| 119 |
|
|---|
| 120 | /* A table containing classes is a class_node_ptr (pointing to the
|
|---|
| 121 | first entry in the table - if it is NULL, then the table is
|
|---|
| 122 | empty). */
|
|---|
| 123 |
|
|---|
| 124 | /* We have 1024 tables. Each table contains all class names which
|
|---|
| 125 | have the same hash (which is a number between 0 and 1023). To look
|
|---|
| 126 | up a class_name, we compute its hash, and get the corresponding
|
|---|
| 127 | table. Once we have the table, we simply compare strings directly
|
|---|
| 128 | till we find the one which we want (using the length first). The
|
|---|
| 129 | number of tables is quite big on purpose (a normal big application
|
|---|
| 130 | has less than 1000 classes), so that you shouldn't normally get any
|
|---|
| 131 | collisions, and get away with a single comparison (which we can't
|
|---|
| 132 | avoid since we need to know that you have got the right thing). */
|
|---|
| 133 | #define CLASS_TABLE_SIZE 1024
|
|---|
| 134 | #define CLASS_TABLE_MASK 1023
|
|---|
| 135 |
|
|---|
| 136 | static class_node_ptr class_table_array[CLASS_TABLE_SIZE];
|
|---|
| 137 |
|
|---|
| 138 | /* The table writing mutex - we lock on writing to avoid conflicts
|
|---|
| 139 | between different writers, but we read without locks. That is
|
|---|
| 140 | possible because we assume pointer assignment to be an atomic
|
|---|
| 141 | operation. */
|
|---|
| 142 | static objc_mutex_t __class_table_lock = NULL;
|
|---|
| 143 |
|
|---|
| 144 | /* CLASS_TABLE_HASH is how we compute the hash of a class name. It is
|
|---|
| 145 | a macro - *not* a function - arguments *are* modified directly.
|
|---|
| 146 |
|
|---|
| 147 | INDEX should be a variable holding an int;
|
|---|
| 148 | HASH should be a variable holding an int;
|
|---|
| 149 | CLASS_NAME should be a variable holding a (char *) to the class_name.
|
|---|
| 150 |
|
|---|
| 151 | After the macro is executed, INDEX contains the length of the
|
|---|
| 152 | string, and HASH the computed hash of the string; CLASS_NAME is
|
|---|
| 153 | untouched. */
|
|---|
| 154 |
|
|---|
| 155 | #define CLASS_TABLE_HASH(INDEX, HASH, CLASS_NAME) \
|
|---|
| 156 | HASH = 0; \
|
|---|
| 157 | for (INDEX = 0; CLASS_NAME[INDEX] != '\0'; INDEX++) \
|
|---|
| 158 | { \
|
|---|
| 159 | HASH = (HASH << 4) ^ (HASH >> 28) ^ CLASS_NAME[INDEX]; \
|
|---|
| 160 | } \
|
|---|
| 161 | \
|
|---|
| 162 | HASH = (HASH ^ (HASH >> 10) ^ (HASH >> 20)) & CLASS_TABLE_MASK;
|
|---|
| 163 |
|
|---|
| 164 | /* Setup the table. */
|
|---|
| 165 | static void
|
|---|
|
|---|