Implementation of language features

Discussion in 'C++' started by W Karas, Mar 11, 2013.

  1. W Karas

    W Karas Guest

    Is there a reference (besides the ARM and hopefully consisting of WWW page(s).) that outlines possible implementations (that is, generated object code) of C++ features? Something to point an experience C programmer to in response to comments like "I think calling a virtual function must involve a hash table lookup" or "I don't see how exception handling could not have execution time overhead even when no exceptions are thrown".
    W Karas, Mar 11, 2013
    #1
    1. Advertising

  2. W Karas

    Ian Collins Guest

    W Karas wrote:

    Please do something about your line length!

    > Is there a reference (besides the ARM and hopefully consisting of WWW
    > page(s).) that outlines possible implementations (that is, generated
    > object code) of C++ features? Something to point an experience C
    > programmer to in response to comments like "I think calling a virtual
    > function must involve a hash table lookup" or "I don't see how
    > exception handling could not have execution time overhead even when
    > no exceptions are thrown".


    There isn't any definitive implementation standard.

    There are a limited number of possible solutions for exceptions, but
    each compiler has it's own implementation. Optimisation can also
    obscure or remove details like virtual function calls.

    --
    Ian Collins
    Ian Collins, Mar 11, 2013
    #2
    1. Advertising

  3. W Karas

    W Karas Guest

    On Monday, March 11, 2013 6:45:25 PM UTC-4, Ian Collins wrote:
    > W Karas wrote:
    >
    >
    >
    > Please do something about your line length!
    >
    >
    >
    > > Is there a reference (besides the ARM and hopefully consisting of WWW

    >
    > > page(s).) that outlines possible implementations (that is, generated

    >
    > > object code) of C++ features? Something to point an experience C

    >
    > > programmer to in response to comments like "I think calling a virtual

    >
    > > function must involve a hash table lookup" or "I don't see how

    >
    > > exception handling could not have execution time overhead even when

    >
    > > no exceptions are thrown".

    >
    >
    >
    > There isn't any definitive implementation standard.
    >
    >
    >
    > There are a limited number of possible solutions for exceptions, but
    >
    > each compiler has it's own implementation. Optimisation can also
    >
    > obscure or remove details like virtual function calls.
    >
    >
    >
    > --
    >
    > Ian Collins


    I believe there is a "model" approach for exception handling with no time overhead if no exception is thrown. It is based on generating a lookup table where the key is the return address found on the stack during stack unwind. The result of the table lookup is the address of a function that will properly destroy the known set of objects that will be active when that return address is present in the call stack.
    W Karas, Mar 11, 2013
    #3
  4. W Karas

    Öö Tiib Guest

    On Tuesday, 12 March 2013 10:10:18 UTC+2, Christian Gollwitzer wrote:
    > Am 11.03.13 23:51, schrieb Stefan Ram:
    > > W Karas <> writes:
    > >> Is there a reference (besides the ARM and hopefully
    > >> consisting of WWW page(s).) that outlines possible
    > >> implementations (that is, generated object code) of C++
    > >> features?

    > >
    > > Maybe I may follow that with my similar question:
    > >
    > > GCC is open source, so its source code should
    > > be available. A part of it should be the source for
    > > GCC's C++ standard library written in C++ itself
    > > (well, maybe in the GCC dialect of it!). Does anyone
    > > already know where to find this, before I start to
    > > search it myself? Or/And possibly for Clang, too?

    >
    >
    > Gcc's standard library is called libstdc++ and has it's own page:
    >
    > http://gcc.gnu.org/libstdc /
    >
    > It's written there that libstdc++ is now part of gcc. So simply download
    > a copy of the source of gcc.
    >
    > clang++ uses by default libstdc++, too (on OSX), but they have
    > developped there own version:
    >
    > http://libcxx.llvm.org/


    Thanks, Christian, you were very helpful. I suspect however that Stefan
    was being sarcastic with his counter-question. Those sites are among first
    hits when attempting to search for them.
    Öö Tiib, Mar 12, 2013
    #4
  5. Stefan Ramæ–¼ 2013å¹´3月12日星期二UTC+8上åˆ6時51分39秒寫é“:
    > W Karas <> writes:
    >
    > >Is there a reference (besides the ARM and hopefully

    >
    > >consisting of WWW page(s).) that outlines possible

    >
    > >implementations (that is, generated object code) of C++

    >
    > >features?

    >
    >
    >
    > Maybe I may follow that with my similar question:
    >
    >
    >
    > GCC is open source, so its source code should
    >
    > be available. A part of it should be the source for
    >
    > GCC's C++ standard library written in C++ itself
    >
    > (well, maybe in the GCC dialect of it!). Does anyone
    >
    > already know where to find this, before I start to
    >
    > search it myself? Or/And possibly for Clang, too?

    As far as I know, an assembler and a C compiler are implemented first
    for some new CPU instruction set, then one can get the CPP compiler
    in the BSD or LINUX platforms.
    88888 Dihedral, Mar 12, 2013
    #5
  6. W Karas

    W Karas Guest

    On Tuesday, March 12, 2013 7:14:26 AM UTC-4, Öö Tiib wrote:
    > On Tuesday, 12 March 2013 10:10:18 UTC+2, Christian Gollwitzer wrote:
    >
    > > Am 11.03.13 23:51, schrieb Stefan Ram:

    >
    > > > W Karas <> writes:

    >
    > > >> Is there a reference (besides the ARM and hopefully

    >
    > > >> consisting of WWW page(s).) that outlines possible

    >
    > > >> implementations (that is, generated object code) of C++

    >
    > > >> features?

    >
    > > >

    >
    > > > Maybe I may follow that with my similar question:

    >
    > > >

    >
    > > > GCC is open source, so its source code should

    >
    > > > be available. A part of it should be the source for

    >
    > > > GCC's C++ standard library written in C++ itself

    >
    > > > (well, maybe in the GCC dialect of it!). Does anyone

    >
    > > > already know where to find this, before I start to

    >
    > > > search it myself? Or/And possibly for Clang, too?

    >
    > >

    >
    > >

    >
    > > Gcc's standard library is called libstdc++ and has it's own page:

    >
    > >

    >
    > > http://gcc.gnu.org/libstdc /

    >
    > >

    >
    > > It's written there that libstdc++ is now part of gcc. So simply download

    >
    > > a copy of the source of gcc.

    >
    > >

    >
    > > clang++ uses by default libstdc++, too (on OSX), but they have

    >
    > > developped there own version:

    >
    > >

    >
    > > http://libcxx.llvm.org/

    >
    >
    >
    > Thanks, Christian, you were very helpful. I suspect however that Stefan
    >
    > was being sarcastic with his counter-question. Those sites are among first
    >
    > hits when attempting to search for them.


    I guess I was not clear, I'm looking for summary descriptions in English accompanied by figures and/or bits of code. I think most people would find this preferable to reading compiler code and reverse-engineering the key strategies involved.
    W Karas, Mar 12, 2013
    #6
  7. W Karas

    Stefan Ram Guest

    Stefan Ram, Mar 12, 2013
    #7
  8. W Karas

    Jorgen Grahn Guest

    On Tue, 2013-03-12, Scott Lurndal wrote:
    > W Karas <> writes:
    >>Is there a reference (besides the ARM and hopefully consisting of WWW page(=
    >>s).) that outlines possible implementations (that is, generated object code=
    >>) of C++ features? Something to point an experience C programmer to in res=
    >>ponse to comments like "I think calling a virtual function must involve a h=
    >>ash table lookup" or "I don't see how exception handling could not have exe=
    >>cution time overhead even when no exceptions are thrown".

    >
    > The C++ ABI defines several areas that are relevent to interoperability on
    > Unix systems - including the vtable layout and format.
    >
    > http://mentorembedded.github.com/cxx-abi/
    > http://mentorembedded.github.com/cxx-abi/abi-eh.html
    >
    > this applies to both itanium and x86_64 systems.
    >
    > A good C programmer shouldn't feel uncomfortable disassembling code and
    > poking around in the guts of a program to see what the the compiler is
    > generating.


    I suspect that W Karas' C programmers are of the kind who think it's
    up to *him* to disprove their home-made theories about how C++ must
    work ...

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Mar 12, 2013
    #8
  9. W Karas

    Dombo Guest

    Op 11-Mar-13 23:39, W Karas schreef:
    > Is there a reference (besides the ARM and hopefully consisting of WWW
    > page(s).) that outlines possible implementations (that is, generated
    > object code) of C++ features? Something to point an experience C
    > programmer to in response to comments like "I think calling a virtual
    > function must involve a hash table lookup" or "I don't see how
    > exception handling could not have execution time overhead even when
    > no exceptions are thrown".


    If the performance really matters and I really want to understand what
    is going on I usually inspect the assembly output of the compiler and/or
    step though the code with the debugger in disassembly mode.

    Inspecting the assembly output can lead to sometimes surprising
    insights. I see too often that people resort to tricks that make the
    code hard to understand and/or brittle because they _believe_ it will
    run more efficiently without bothering to actually measure it or
    actually checking how it affects the code being generated. More often
    than not the hack is no more efficient and quite often even less
    efficient than the straightforward implementation.

    There are many variables, such as the compiler, (optimization) settings
    and the code being compiled, that affect what kind of code will be
    generated. For example for virtual functions a vtable is typically used
    (I have never seen a C++ compiler that used hash tables for virtual
    functions, nor do I see any benefit with that approach), but sometimes
    the compiler can skip the vtable lookup if knows at compile time exactly
    which function is going to be called, it might even inline that function
    in that case.

    That being said the following articles should give some insight how
    virtual functions are typically done by C++ compilers:
    http://en.wikipedia.org/wiki/Virtual_method_table
    http://tinydrblog.appspot.com/?p=89001

    For exception handling the articles below may give you some insight how
    C++ compilers may implement this feature:
    http://preshing.com/20110807/the-cost-of-enabling-exception-handling
    http://www.systemcall.org/blog/2010/10/zero-cost-exception-handling-in-cpp/
    Dombo, Mar 12, 2013
    #9
  10. W Karas

    Guest

    On Tuesday, March 12, 2013 8:10:18 AM UTC, Christian Gollwitzer wrote:
    > clang++ uses by default libstdc++, too (on OSX), but they have
    >
    > developped there own version:
    >
    >
    >
    > http://libcxx.llvm.org/
    >


    I'm building clang ...
    The getting started page says that make "builds both
    LLVM and Clang for debug mode." Why do I want debug
    mode? How to build it without debug stuff? Tia.

    Brian
    Ebenezer Enterprises - Proverbs 3:5,6.
    http://webEbenezer.net
    , Mar 12, 2013
    #10
  11. W Karas

    W Karas Guest

    On Tuesday, March 12, 2013 4:08:34 PM UTC-4, Jorgen Grahn wrote:
    > On Tue, 2013-03-12, Scott Lurndal wrote:
    >
    > > W Karas <> writes:

    >
    > >>Is there a reference (besides the ARM and hopefully consisting of WWW page(=

    >
    > >>s).) that outlines possible implementations (that is, generated object code=

    >
    > >>) of C++ features? Something to point an experience C programmer to inres=

    >
    > >>ponse to comments like "I think calling a virtual function must involvea h=

    >
    > >>ash table lookup" or "I don't see how exception handling could not haveexe=

    >
    > >>cution time overhead even when no exceptions are thrown".

    >
    > >

    >
    > > The C++ ABI defines several areas that are relevent to interoperabilityon

    >
    > > Unix systems - including the vtable layout and format.

    >
    > >

    >
    > > http://mentorembedded.github.com/cxx-abi/

    >
    > > http://mentorembedded.github.com/cxx-abi/abi-eh.html

    >
    > >

    >
    > > this applies to both itanium and x86_64 systems.

    >
    > >

    >
    > > A good C programmer shouldn't feel uncomfortable disassembling code and

    >
    > > poking around in the guts of a program to see what the the compiler is

    >
    > > generating.

    >
    >
    >
    > I suspect that W Karas' C programmers are of the kind who think it's
    >
    > up to *him* to disprove their home-made theories about how C++ must
    >
    > work ...


    Not necessarily. Unfortunately, even in scientific and technical matters, pride or greed or both can cause people to market rather than explain. They emphasize advantages and gloss over trade-offs. Skepticism is an unavoidable consequence of that. I'd find it highly believable if someone told meit would be hard for them to find the time to figure out how C++ worked bylooking at uncommented, disassembled code.
    W Karas, Mar 12, 2013
    #11
  12. On Mar 12, 12:04 pm, W Karas <> wrote:
    > > > > W Karas <> writes:

    >
    > > > >> Is there a reference (besides the ARM and hopefully

    >
    > > > >> consisting of WWW page(s).) that outlines possible

    >
    > > > >> implementations (that is, generated object code) of C++

    >
    > > > >> features?

    >

    (...)
    >
    > I guess I was not clear, I'm looking for summary descriptions in English accompanied by figures and/or bits of code.  I think most people would find this preferable to reading compiler code and reverse-engineering the keystrategies involved.



    Not what you are looking for in terms of "outlining generated
    object
    code from compiling C++ code", but methinks Lippman's "Inside C+
    +
    object model" could provide sufficiently technical explanations
    in
    English to refute people's common misconceptions about C+
    +.

    - Anand
    Anand Hariharan, Mar 12, 2013
    #12
  13. W Karas

    W Karas Guest

    On Tuesday, March 12, 2013 4:54:18 PM UTC-4, Dombo wrote:
    > Op 11-Mar-13 23:39, W Karas schreef:
    >
    > > Is there a reference (besides the ARM and hopefully consisting of WWW

    >
    > > page(s).) that outlines possible implementations (that is, generated

    >
    > > object code) of C++ features? Something to point an experience C

    >
    > > programmer to in response to comments like "I think calling a virtual

    >
    > > function must involve a hash table lookup" or "I don't see how

    >
    > > exception handling could not have execution time overhead even when

    >
    > > no exceptions are thrown".

    >
    >
    >
    > If the performance really matters and I really want to understand what
    >
    > is going on I usually inspect the assembly output of the compiler and/or
    >
    > step though the code with the debugger in disassembly mode.
    >
    >
    >
    > Inspecting the assembly output can lead to sometimes surprising
    >
    > insights. I see too often that people resort to tricks that make the
    >
    > code hard to understand and/or brittle because they _believe_ it will
    >
    > run more efficiently without bothering to actually measure it or
    >
    > actually checking how it affects the code being generated. More often
    >
    > than not the hack is no more efficient and quite often even less
    >
    > efficient than the straightforward implementation.
    >
    >
    >
    > There are many variables, such as the compiler, (optimization) settings
    >
    > and the code being compiled, that affect what kind of code will be
    >
    > generated. For example for virtual functions a vtable is typically used
    >
    > (I have never seen a C++ compiler that used hash tables for virtual
    >
    > functions, nor do I see any benefit with that approach), but sometimes
    >
    > the compiler can skip the vtable lookup if knows at compile time exactly
    >
    > which function is going to be called, it might even inline that function
    >
    > in that case.
    >
    >
    >
    > That being said the following articles should give some insight how
    >
    > virtual functions are typically done by C++ compilers:
    >
    > http://en.wikipedia.org/wiki/Virtual_method_table
    >
    > http://tinydrblog.appspot.com/?p=89001
    >
    >
    >
    > For exception handling the articles below may give you some insight how
    >
    > C++ compilers may implement this feature:
    >
    > http://preshing.com/20110807/the-cost-of-enabling-exception-handling
    >
    > http://www.systemcall.org/blog/2010/10/zero-cost-exception-handling-in-cpp/


    I think SmallTalk uses a hash table to bind function calls to functions at runtime. This implementation stems from the fact that formal parameters toSmallTalk methods (member functions) are only bound to duck types at compile time, not concrete types. The advantage is that the distinction betweena class and a templeted class is eliminated.
    W Karas, Mar 12, 2013
    #13
  14. W Karas

    Öö Tiib Guest

    On Wednesday, 13 March 2013 00:31:10 UTC+2, W Karas wrote:
    > On Tuesday, March 12, 2013 4:08:34 PM UTC-4, Jorgen Grahn wrote:
    > > I suspect that W Karas' C programmers are of the kind who think it's
    > > up to *him* to disprove their home-made theories about how C++ must
    > > work ...

    >
    > Not necessarily. Unfortunately, even in scientific and technical
    > matters, pride or greed or both can cause people to market rather
    > than explain. They emphasize advantages and gloss over
    > trade-offs. Skepticism is an unavoidable consequence of that. I'd
    > find it highly believable if someone told me it would be hard for them
    > to find the time to figure out how C++ worked by looking at
    > uncommented, disassembled code.


    The usual reason for such discussions is that some software developers
    (you?) try to make other developers (them?) to use or consider tools that
    they do not like.

    Nothing good can come out of it. Ever. Only the nonsense fan sites like
    http://keithlea.com/javabench/ that contain some Python vs. Assembler
    performance contest where Python wins by large margin.
    Öö Tiib, Mar 12, 2013
    #14
  15. W Karas

    W Karas Guest

    On Tuesday, March 12, 2013 7:06:09 PM UTC-4, Öö Tiib wrote:
    > On Wednesday, 13 March 2013 00:31:10 UTC+2, W Karas wrote:
    >
    > > On Tuesday, March 12, 2013 4:08:34 PM UTC-4, Jorgen Grahn wrote:

    >
    > > > I suspect that W Karas' C programmers are of the kind who think it's

    >
    > > > up to *him* to disprove their home-made theories about how C++ must

    >
    > > > work ...

    >
    > >

    >
    > > Not necessarily. Unfortunately, even in scientific and technical

    >
    > > matters, pride or greed or both can cause people to market rather

    >
    > > than explain. They emphasize advantages and gloss over

    >
    > > trade-offs. Skepticism is an unavoidable consequence of that. I'd

    >
    > > find it highly believable if someone told me it would be hard for them

    >
    > > to find the time to figure out how C++ worked by looking at

    >
    > > uncommented, disassembled code.

    >
    >
    >
    > The usual reason for such discussions is that some software developers
    >
    > (you?) try to make other developers (them?) to use or consider tools that
    >
    > they do not like.
    >
    >
    >
    > Nothing good can come out of it. Ever. Only the nonsense fan sites like
    >
    > http://keithlea.com/javabench/ that contain some Python vs. Assembler
    >
    > performance contest where Python wins by large margin.


    Just as God would have saved Sodom and Gomorrah for the benefit of a handful of righteous men, I think what I'm asking for would be nice to have, evenif only for a handful of open-minded C programmers.
    W Karas, Mar 13, 2013
    #15
  16. W Karas

    Jorgen Grahn Guest

    On Wed, 2013-03-13, W Karas wrote:
    ....
    > Just as God would have saved Sodom and Gomorrah for the benefit of a
    > handful of righteous men, I think what I'm asking for would be nice to
    > have, even if only for a handful of open-minded C programmers.


    And just for the record, I agree. But unfortunately I have never seen
    that information collected in one place.

    For the "exceptions are for free unless thrown" idea, I have seen
    people mention that such designs are feasible[1], but it's not clear
    to me /how/ it's done, or if my favorite compiler does it. This
    bothers me a bit.

    /Jorgen

    [1] It might even be mentioned in Stroustrup's /Design and Evolution
    of C++/. That book probably answers some of your questions, but it's
    now almost 20 years old.

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Mar 13, 2013
    #16
  17. W Karas

    Öö Tiib Guest

    On Wednesday, 13 March 2013 02:12:07 UTC+2, W Karas wrote:
    > On Tuesday, March 12, 2013 7:06:09 PM UTC-4, Öö Tiib wrote:
    > > The usual reason for such discussions is that some software developers
    > > (you?) try to make other developers (them?) to use or consider tools that
    > > they do not like.
    > >
    > > Nothing good can come out of it. Ever. Only the nonsense fan sites like
    > > http://keithlea.com/javabench/ that contain some Python vs. Assembler
    > > performance contest where Python wins by large margin.

    >
    > Just as God would have saved Sodom and Gomorrah for the benefit of a
    > handful of righteous men, I think what I'm asking for would be nice to
    > have, even if only for a handful of open-minded C programmers.


    It was angel. You are not angel of God. :) You have no message.

    I suggest to measure such things and look at them. World changes but you
    stay efficient and elegant. The modern implementations of exceptions
    virtuals etc. are extremely efficient. Someone who is objecting against
    those tools is still right, every tool can be used inefficiently. Examples ....

    Virtual is bad: I have measured serious performance drop because a
    pointless virtual did block vital optimizations. Do not ever have
    pointless virtuals.

    Virtual is good: I have measured large performance gain of replacing
    sized switch-case with virtuals. Avoid huge switch case blocks, use
    virtuals.

    Exception is bad: I recently discovered that returning a value is only 300 times quicker than throwing it. It was way larger old times. 300 is still
    a lot.

    Exception is good: I have measured that passing and handling fallible,
    nullable or optional in every cycle (that never falls, is null or missing)
    is twice slower than returning a value. So, if the chance to fail is 1/600
    or less then using exception is twice cheaper than using fallible, nullable
    or optional.

    Try-catch is bad: The try-catch block is simply *ugly*. I never put them
    into every cycle despite it costs nothing (entering and leaving 10 millions
    of those takes one millisecond).

    Try-catch is good: Thanks to its ugliness I use RAII that makes it possible
    to move the ugly try-catch up the call chain around the whole operation.
    RAII looks extremely elegant as contrast. Same effect is with
    'static_cast<Foo>(poo)'. It is so ugly that I have reduced casts to
    minimum.
    Öö Tiib, Mar 14, 2013
    #17
  18. W Karas

    Guest

    On Tuesday, March 12, 2013 9:49:15 PM UTC, wrote:
    >
    > I'm building clang ...
    >
    > The getting started page says that make "builds both
    >
    > LLVM and Clang for debug mode." Why do I want debug
    >
    > mode? How to build it without debug stuff? Tia.
    >


    I managed to build Clang without debug stuff and it
    helped me find a problem that neither G++ or MSVC
    were pointing out.

    Now I'm wondering about a warning clang gives. Here's
    an example:

    clang++ -std=c++0x -O3 -s -Wreorder -W -Wall -I. -DCMW_MAKEFILES -DSYSLOG_AVAILABLE -c -o remote_messages_middle.cg.o remote_messages_middle.cg.cc

    clang: warning: argument unused during compilation: '-s'

    I got the impression I could remove the -s and nothing
    would change. But my executable is 64,816 bytes when
    everything is built with -s and 79,648 when I take it out.
    G++ doesn't give any warning when the -s is in there.
    Any ideas? Thanks.

    Brian
    Ebenezer Enterprises - John 3:16.
    http://webEbenezer.net
    , Mar 22, 2013
    #18
  19. W Karas

    Guest

    Nevermind. My mistake. I think the -s isn't needed
    for some fo the build, but is in the linking.
    , Mar 22, 2013
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Omnicore Software
    Replies:
    1
    Views:
    433
    Tim Tyler
    Sep 29, 2003
  2. Language features

    , Nov 2, 2005, in forum: C++
    Replies:
    18
    Views:
    566
  3. Jonathan Mcdougall
    Replies:
    2
    Views:
    481
    Kaz Kylheku
    Nov 3, 2005
  4. Sean

    language features!

    Sean, Mar 24, 2006, in forum: C++
    Replies:
    6
    Views:
    377
    forkazoo
    Mar 24, 2006
  5. BillJosephson
    Replies:
    148
    Views:
    2,587
    peter koch
    Jan 27, 2007
Loading...

Share This Page