Skip to content

Commit

Permalink
Fix introspection with type-dependent dispatch enabled. Looking up a …
Browse files Browse the repository at this point in the history
…method with an untyped selector was failing because the UID for the selector was no longer the same for all type variants.

As a side-effect of this change, method lookups for introspection are now much faster.  They use the slot lookup mechanism to find which class has the method declared and then only need to do the linear search on that class, rather than doing the linear search on the entire hierarchy (slow!).  If the method is not present, then they can give up after two memory accesses, rather than after searching a few hundred list entries, but that's a less important case.

I also noticed while tracking down this bug that the implementation of methodSignatureForSelector in GNUstep is very inefficient.  I'll tweak that next.
  • Loading branch information
theraven committed Sep 2, 2010
1 parent 0284155 commit fe566cb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
27 changes: 13 additions & 14 deletions runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <assert.h>

Class objc_next_class(void*);
struct objc_slot *objc_get_slot(Class cls, SEL selector);

/**
* Looks up the instance method in a specific class, without recursing into
Expand Down Expand Up @@ -257,18 +258,15 @@ id class_createInstance(Class cls, size_t extraBytes)

Method class_getInstanceMethod(Class aClass, SEL aSelector)
{
Method method = class_getInstanceMethodNonrecursive(aClass, aSelector);
if (method == NULL)
{
// TODO: Check if this should be NULL or aClass
Class superclass = class_getSuperclass(aClass);
if (superclass == NULL)
{
return NULL;
}
return class_getInstanceMethod(superclass, aSelector);
}
return method;
// Do a dtable lookup to find out which class the method comes from.
struct objc_slot *slot = objc_get_slot(aClass, aSelector);
if (NULL == slot) { return NULL; }

// Now find the typed variant of the selector, with the correct types.
aSelector = sel_registerTypedName_np(sel_getName(aSelector), slot->types);

// Then do the slow lookup to find the method.
return class_getInstanceMethodNonrecursive(slot->owner, aSelector);
}

Method class_getClassMethod(Class aClass, SEL aSelector)
Expand Down Expand Up @@ -359,10 +357,11 @@ BOOL class_isMetaClass(Class cls)

IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
{
Method method = class_getInstanceMethodNonrecursive(cls, name);
SEL sel = sel_registerTypedName_np(sel_getName(name), types);
Method method = class_getInstanceMethodNonrecursive(cls, sel);
if (method == NULL)
{
class_addMethod(cls, name, imp, types);
class_addMethod(cls, sel, imp, types);
return NULL;
}
IMP old = (IMP)method->imp;
Expand Down
31 changes: 30 additions & 1 deletion selector_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ const char *sel_getType_np(SEL aSel)
}


unsigned sel_copyTypes(const char *selName, const char **types, unsigned count)
unsigned sel_copyTypes_np(const char *selName, const char **types, unsigned count)
{
SEL untyped = selector_lookup(selName, 0);
if (untyped == NULL) { return 0; }
Expand Down Expand Up @@ -373,6 +373,35 @@ unsigned sel_copyTypes(const char *selName, const char **types, unsigned count)
return found;
}

unsigned sel_copyTypedSelectors_np(const char *selName, SEL *const sels, unsigned count)
{
SEL untyped = selector_lookup(selName, 0);
if (untyped == NULL) { return 0; }

struct sel_type_list *l =
SparseArrayLookup(selector_list, (uint32_t)(uintptr_t)untyped->name);
// Skip the head, which just contains the name, not the types.
l = l->next;

if (count == 0)
{
while (NULL != l)
{
count++;
l = l->next;
}
return count;
}

unsigned found = 0;
while (NULL != l && found<count)
{
sels[found++] = selector_lookup(selName, l->value);
l = l->next;
}
return found;
}

void objc_register_selectors_from_list(struct objc_method_list *l)
{
for (int i=0 ; i<l->count ; i++)
Expand Down

0 comments on commit fe566cb

Please sign in to comment.