Search code examples
dencapsulationmicro-optimization

What is the overhead of properties in D if any?


I should expose some of my class data, but it is expected to work in very productivity demanding area, so naturally I wonder, what is the best way to do so: keep to design principles and make this exposition via properties, or to forget about it and just make my data public?


Solution

  • A property is a function call, but it is generally a trivial one that can be inlined.... at least as long as it is final and you use the right compiler setup (gdc -O or ldc's optimization. a quick test is showing me that dmd -inline does not inline it though!).

    So right now, if you are using gdc or ldc, a final property is free because it will be inlined and optimized. A virtual property function I'm not sure about. If you are using dmd, there's a small cost of a function call, so if it is a tight loop, you'll probably get a speedup with a public field.