Do getters and setters impact performance in C++/D/Java?

后端 未结 10 2382
无人及你
无人及你 2020-12-06 09:26

This is a rather old topic: Are setters and getters good or evil?

My question here is: do compilers in C++ / D / Java inline the getters and setter?

To which

10条回答
  •  被撕碎了的回忆
    2020-12-06 09:39

    Quoting from here Regarding the D Programming Langiage

    I think DMD is currently unable to de-virtualize virtual getters and setters. Virtual calls are a bit slower by itself, but they also don't allow inlining, so successive standard optimizations can't be done. So if such accesses to the attribute is a virtual call and this happens in a "hot" part of the code, then it may slow down your code significantly. (if it happens in non-hot parts of the code it has usually no effects. That's why Java Hot Spot doesn't need optimize all your code to produce a very fast program anyway).

    I have encouraged Frits van Bommel to improve the devirtualization capabilities of LDC:

    Now LDC is able to do that in few very simple situations, but most times the situation is unchanged compared to DMD. Eventually LLVM will improve, so this situation can improve by itself. But the front-end too may do something about this.

    Here is some documentation about this topic, some older, some more modern:

提交回复
热议问题