Re: Instance Variable access
Re: Instance Variable access
- Subject: Re: Instance Variable access
- From: Andy Lee <email@hidden>
- Date: Sat, 8 May 2004 13:16:21 -0400
On May 8, 2004, at 12:16 PM, Allan Odgaard wrote:
On 8. May 2004, at 7:14, Andy Lee wrote:
(Note: I haven't done C++ in many moons, so the following comments
might be based on an outdated understanding of the language...)
[...] the messaging overhead of Objective-C is much closer to C++
than to, say, Java.
For the records: C++ has two kinds of methods (called member
functions), by default a member function is just a regular function
which take an object pointer as a hidden first argument -- these can
easily be made inline by the compiler.
The other type is virtual member functions (there is no final keyword
in C++, this is from Java!)
D'oh! I can only plead brain-cell deterioration over the years.
So if I understand correctly, it's the *absence* of a keyword that
indicates a C++ method cannot be overridden. Furthermore, and
hopefully I'm not screwing up again, this is not quite like final
methods in the Java sense, because in C++ a subclass can have a
non-virtual member function with the same signature. Which means if
you want an instance of the subclass to invoke its superclass's version
of the member function, you have to cast it to the superclass. Which
means non-virtual member functions are not exactly like methods in
*any* other object-oriented language -- which I'm guessing is why C++
programmers prefer the "member function" terminology? (I'm probably
wrong again, but if I'm going to stand corrected, I might as well stand
fully corrected. :))
If my new understanding is correct, I don't think it's fair to compare
performance of non-virtual member functions because they're essentially
syntactic sugar around plain old functions.
ObjectiveC uses the actual name of the method as signature/identifier.
When it needs to call a method it computes a hash from the name,
looks into a hash-table to see if a match is found, and if not, it
finds the method using the isa pointer (traversing linked lists of
method names), and then updates the hash.
I doubt the hash is computed at run time every time a message is sent.
In fact, I'm pretty sure the hash is cached, perhaps inside the
structure of a SEL. The only time I can think of that a hash
definitely has to be computed at run time is if I call
NSSelectorFromString() with a variable as its argument.
Although the C++ standard does not require the implementor to use a
jump-table (often referred to as the vtable), the implementation
shouldn't come close to the complexity of ObjectiveC's obj_msgSend
(since C++ does not have open classes, categories, dynamic typing and
similar).
Depends on what we mean by "close." I don't know if the difference is
an order of magnitude or 2x or less, but it seems to me Objective-C
*could* use a vtable-type implementation where vtables happen to be
changeable at run time, in which case the difference should be pretty
small. In any case, neither of the C-derivative languages does all the
runtime checking of message-sends that Java does.
--Andy (cc'ing Ondra at the last second because I see you posted a
reply...)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.