Artifact 58afce446532081e25413ce3fa29484bdff814889aa089315a65d9b72a0f50a7:
- File
src/runtime/category.m
— part of check-in
[84a724dd4b]
at
2013-12-06 00:52:26
on branch trunk
— Add a fast path for objc_classname_to_class().
This should improve the performance for the GCC ABI, as
objc_classname_to_class() is used for all sorts of class lookups, e.g.
objc_lookup_class().As this performance improvement needs RAM, it is only used after 128
calls into objc_classname_to_class(), so that if the ObjFW ABI is used
and the user does not call into objc_getClass() or similar in a loop, no
memory is wasted.Runtime internal usage of objc_classname_to_class() does not use the
fast path and does not count as a call into objc_classname_to_class().
The reason for this is that if the runtime calls
objc_classname_to_class(), it already has the lock and thus the
performance gain would be small, but it would waste memory. (user: js, size: 3209) [annotate] [blame] [check-ins using]
/* * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #import "runtime.h" #import "runtime-private.h" static struct objc_hashtable *categories = NULL; static void register_selectors(struct objc_abi_category *cat) { struct objc_abi_method_list *ml; unsigned int i; for (ml = cat->instance_methods; ml != NULL; ml = ml->next) for (i = 0; i < ml->count; i++) objc_register_selector( (struct objc_abi_selector*)&ml->methods[i]); for (ml = cat->class_methods; ml != NULL; ml = ml->next) for (i = 0; i < ml->count; i++) objc_register_selector( (struct objc_abi_selector*)&ml->methods[i]); } static void register_category(struct objc_abi_category *cat) { struct objc_abi_category **cats; Class cls = objc_classname_to_class(cat->class_name, false); if (categories == NULL) categories = objc_hashtable_new(2); cats = (struct objc_abi_category**)objc_hashtable_get(categories, cat->class_name); if (cats != NULL) { struct objc_abi_category **ncats; size_t i; for (i = 0; cats[i] != NULL; i++); if ((ncats = realloc(cats, (i + 2) * sizeof(struct objc_abi_category*))) == NULL) OBJC_ERROR("Not enough memory for category %s of " "class %s!", cat->category_name, cat->class_name); ncats[i] = cat; ncats[i + 1] = NULL; objc_hashtable_set(categories, cat->class_name, ncats); if (cls != Nil && cls->info & OBJC_CLASS_INFO_SETUP) { objc_update_dtable(cls); objc_update_dtable(cls->isa); } return; } if ((cats = malloc(2 * sizeof(struct objc_abi_category*))) == NULL) OBJC_ERROR("Not enough memory for category %s of class %s!\n", cat->category_name, cat->class_name); cats[0] = cat; cats[1] = NULL; objc_hashtable_set(categories, cat->class_name, cats); if (cls != Nil && cls->info & OBJC_CLASS_INFO_SETUP) { objc_update_dtable(cls); objc_update_dtable(cls->isa); } } void objc_register_all_categories(struct objc_abi_symtab *symtab) { struct objc_abi_category **cats; size_t i; cats = (struct objc_abi_category**)symtab->defs + symtab->cls_def_cnt; for (i = 0; i < symtab->cat_def_cnt; i++) { register_selectors(cats[i]); register_category(cats[i]); } } struct objc_category** objc_categories_for_class(Class cls) { if (categories == NULL) return NULL; return (struct objc_category**)objc_hashtable_get(categories, cls->name); } void objc_free_all_categories(void) { uint32_t i; if (categories == NULL) return; for (i = 0; i <= categories->last_idx; i++) if (categories->data[i] != NULL) free((void*)categories->data[i]->obj); objc_hashtable_free(categories); categories = NULL; }