ŁukaszDe 38 Posted May 16, 2019 (edited) Hi, I always add properties for private parameters in class with Get and Set methods: TSomething = class private pX: Integer; pY: Integer; function GetX: Integer; procedure SetX(AValue: Integer); function GetY: Integer; procedure SetY(AValue: Integer); public property X: Integer read GetX write SetX; property Y: Integer read GetY write SetY; end; function TSomething.GetX: Integer; begin Result := Self.pX; end; procedure TSomething.SetX(AValue: Integer); begin Self.pX := AValue; end; function TSomething.GetY: Integer; begin Result := Self.pY; end; procedure TSomething.SetY(AValue: Integer); begin Self.pY := AValue; end; But I found a problem with this in Delphi. I attach an example application. Access to class parameter by property with Get is almost 6 times longer than directly access. Why? If you do this in loop for 50000000 objects, than the loop is very sloow... AccessToClassParameters.zip Edited May 16, 2019 by ŁukaszDe Share this post Link to post
Alexander Elagin 143 Posted May 16, 2019 Add inline directive to the property accessors and check if it helps. In your example adding this directive completely eliminates function calls in the generated code. function GetX: Integer; inline; procedure SetX(AValue: Integer); inline; function GetY: Integer; inline; procedure SetY(AValue: Integer); inline; Share this post Link to post
ŁukaszDe 38 Posted May 16, 2019 Yes, this is the solution. So If I want to use class properties in loops, I need add 'inline' to Get and Set methods? Share this post Link to post
Ugochukwu Mmaduekwe 42 Posted May 16, 2019 15 minutes ago, ŁukaszDe said: Yes, this is the solution. So If I want to use class properties in loops, I need add 'inline' to Get and Set methods? Inline is a way to hint the compiler to avoid calling a method and instead take the contents of that method and place it at the call site. Share this post Link to post
Dalija Prasnikar 1396 Posted May 16, 2019 First, this sounds like premature optimization. Yes it is 6 times slower, but you are talking about milliseconds for huge number of objects. Next, if you really need to optimize for speed, you can completely remove getter and setter methods. That is the beauty of Delphi - it does not need them at all and you can freely add getter and setter methods later on (if needed) without breaking the public API. Inline approach can solve the immediate problem, but keep in mind that inline directive is just recommendation for the compiler and under some circumstances such methods will not get inlined (though compiler will give you hint in those cases). Essentially you are writing a whole a lot of code for nothing. 2 Share this post Link to post
Stefan Glienke 2002 Posted May 16, 2019 Measure again with optimization on and stackframe off (release config) - I guess the overhead for the getter/setter calls will shrink. Share this post Link to post
David Heffernan 2345 Posted May 16, 2019 Kinda pointless to measure a getter that just returns or sets the field. Try measuring one that does some work. 1 Share this post Link to post
Lars Fosdal 1792 Posted May 31, 2019 As David says - if the setter and getter don't do anything but assignments, you can eliminate them. property X: Integer read pX write pX; property Y: Integer read pY write pY; Share this post Link to post