Dispatch via vtable is faster than a hash table, but would consume tremendous amounts of memory if used everywhere
up vote
1
down vote
favorite
I can't understand it "Dispatch via vtable is faster than a hash table, but would consume tremendous amounts of memory if used everywhere." which is in this blog objc_explain_objc_msgSend_vtable
ios objective-c objective-c-runtime
add a comment |
up vote
1
down vote
favorite
I can't understand it "Dispatch via vtable is faster than a hash table, but would consume tremendous amounts of memory if used everywhere." which is in this blog objc_explain_objc_msgSend_vtable
ios objective-c objective-c-runtime
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I can't understand it "Dispatch via vtable is faster than a hash table, but would consume tremendous amounts of memory if used everywhere." which is in this blog objc_explain_objc_msgSend_vtable
ios objective-c objective-c-runtime
I can't understand it "Dispatch via vtable is faster than a hash table, but would consume tremendous amounts of memory if used everywhere." which is in this blog objc_explain_objc_msgSend_vtable
ios objective-c objective-c-runtime
ios objective-c objective-c-runtime
asked Nov 11 at 14:34
dowZhang
298
298
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
Obj-C is not C++.
In C++, each class has a known (at compile time) set of virtual functions, which is typically significantly smaller than the complete set of functions defined for that class and all of its subclasses. (In C++ you must explicitly mark which functions are virtual, and by default they are not.)
In Obj-C every method is "virtual" (in C++ speak), the selector list is variable, methods can be redefined at runtime (added and replaced), and the list is indefinite (technically, you can send any object any message, even one it doesn't respond do it).
So in C++ it is pretty trivial to build a linear array of every virtual function an object will need to dispatch, assign an offset into that array for each function, and compile that into the code.
If you wanted to use virtual dispatch tables in Obj-C you'd have to create an array for every class that contained every possible selector. For even modest programs, each list would be huge, and it would grow exponentially with project complexity. I mean, every class would have its own array of (what?) 20,000 different selectors, and there are thousands of classes...
A hash table, on the other hand, contains a variable set of method function pointers defined for that class. The set is typically just the subset of the selectors that have actually been sent to those objects, so it is much (much) smaller than the complete set of selectors you could potentially send to an object of that class. This makes the hash table of methods efficient and self-optimizing, even if dispatching take a little longer.
Whether it means that the class would consume tremendous amounts of memory in swift. Your know the structure of Virtual table in swift.
– dowZhang
Nov 12 at 4:32
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Obj-C is not C++.
In C++, each class has a known (at compile time) set of virtual functions, which is typically significantly smaller than the complete set of functions defined for that class and all of its subclasses. (In C++ you must explicitly mark which functions are virtual, and by default they are not.)
In Obj-C every method is "virtual" (in C++ speak), the selector list is variable, methods can be redefined at runtime (added and replaced), and the list is indefinite (technically, you can send any object any message, even one it doesn't respond do it).
So in C++ it is pretty trivial to build a linear array of every virtual function an object will need to dispatch, assign an offset into that array for each function, and compile that into the code.
If you wanted to use virtual dispatch tables in Obj-C you'd have to create an array for every class that contained every possible selector. For even modest programs, each list would be huge, and it would grow exponentially with project complexity. I mean, every class would have its own array of (what?) 20,000 different selectors, and there are thousands of classes...
A hash table, on the other hand, contains a variable set of method function pointers defined for that class. The set is typically just the subset of the selectors that have actually been sent to those objects, so it is much (much) smaller than the complete set of selectors you could potentially send to an object of that class. This makes the hash table of methods efficient and self-optimizing, even if dispatching take a little longer.
Whether it means that the class would consume tremendous amounts of memory in swift. Your know the structure of Virtual table in swift.
– dowZhang
Nov 12 at 4:32
add a comment |
up vote
4
down vote
accepted
Obj-C is not C++.
In C++, each class has a known (at compile time) set of virtual functions, which is typically significantly smaller than the complete set of functions defined for that class and all of its subclasses. (In C++ you must explicitly mark which functions are virtual, and by default they are not.)
In Obj-C every method is "virtual" (in C++ speak), the selector list is variable, methods can be redefined at runtime (added and replaced), and the list is indefinite (technically, you can send any object any message, even one it doesn't respond do it).
So in C++ it is pretty trivial to build a linear array of every virtual function an object will need to dispatch, assign an offset into that array for each function, and compile that into the code.
If you wanted to use virtual dispatch tables in Obj-C you'd have to create an array for every class that contained every possible selector. For even modest programs, each list would be huge, and it would grow exponentially with project complexity. I mean, every class would have its own array of (what?) 20,000 different selectors, and there are thousands of classes...
A hash table, on the other hand, contains a variable set of method function pointers defined for that class. The set is typically just the subset of the selectors that have actually been sent to those objects, so it is much (much) smaller than the complete set of selectors you could potentially send to an object of that class. This makes the hash table of methods efficient and self-optimizing, even if dispatching take a little longer.
Whether it means that the class would consume tremendous amounts of memory in swift. Your know the structure of Virtual table in swift.
– dowZhang
Nov 12 at 4:32
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Obj-C is not C++.
In C++, each class has a known (at compile time) set of virtual functions, which is typically significantly smaller than the complete set of functions defined for that class and all of its subclasses. (In C++ you must explicitly mark which functions are virtual, and by default they are not.)
In Obj-C every method is "virtual" (in C++ speak), the selector list is variable, methods can be redefined at runtime (added and replaced), and the list is indefinite (technically, you can send any object any message, even one it doesn't respond do it).
So in C++ it is pretty trivial to build a linear array of every virtual function an object will need to dispatch, assign an offset into that array for each function, and compile that into the code.
If you wanted to use virtual dispatch tables in Obj-C you'd have to create an array for every class that contained every possible selector. For even modest programs, each list would be huge, and it would grow exponentially with project complexity. I mean, every class would have its own array of (what?) 20,000 different selectors, and there are thousands of classes...
A hash table, on the other hand, contains a variable set of method function pointers defined for that class. The set is typically just the subset of the selectors that have actually been sent to those objects, so it is much (much) smaller than the complete set of selectors you could potentially send to an object of that class. This makes the hash table of methods efficient and self-optimizing, even if dispatching take a little longer.
Obj-C is not C++.
In C++, each class has a known (at compile time) set of virtual functions, which is typically significantly smaller than the complete set of functions defined for that class and all of its subclasses. (In C++ you must explicitly mark which functions are virtual, and by default they are not.)
In Obj-C every method is "virtual" (in C++ speak), the selector list is variable, methods can be redefined at runtime (added and replaced), and the list is indefinite (technically, you can send any object any message, even one it doesn't respond do it).
So in C++ it is pretty trivial to build a linear array of every virtual function an object will need to dispatch, assign an offset into that array for each function, and compile that into the code.
If you wanted to use virtual dispatch tables in Obj-C you'd have to create an array for every class that contained every possible selector. For even modest programs, each list would be huge, and it would grow exponentially with project complexity. I mean, every class would have its own array of (what?) 20,000 different selectors, and there are thousands of classes...
A hash table, on the other hand, contains a variable set of method function pointers defined for that class. The set is typically just the subset of the selectors that have actually been sent to those objects, so it is much (much) smaller than the complete set of selectors you could potentially send to an object of that class. This makes the hash table of methods efficient and self-optimizing, even if dispatching take a little longer.
answered Nov 11 at 17:00
James Bucanek
1,4531617
1,4531617
Whether it means that the class would consume tremendous amounts of memory in swift. Your know the structure of Virtual table in swift.
– dowZhang
Nov 12 at 4:32
add a comment |
Whether it means that the class would consume tremendous amounts of memory in swift. Your know the structure of Virtual table in swift.
– dowZhang
Nov 12 at 4:32
Whether it means that the class would consume tremendous amounts of memory in swift. Your know the structure of Virtual table in swift.
– dowZhang
Nov 12 at 4:32
Whether it means that the class would consume tremendous amounts of memory in swift. Your know the structure of Virtual table in swift.
– dowZhang
Nov 12 at 4:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249744%2fdispatch-via-vtable-is-faster-than-a-hash-table-but-would-consume-tremendous-am%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown