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










share|improve this question

























    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










    share|improve this question























      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










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 14:34









      dowZhang

      298




      298






















          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.






          share|improve this answer




















          • 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










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          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

























          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.






          share|improve this answer




















          • 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














          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.






          share|improve this answer




















          • 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












          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto