delphi - Does the compiler optimize (close) identical FieldByName calls? -


in code i'm maintaining, see 2 different methods used in tclientdataset.oncalcfields event handlers:

 dataset  begin    // 1. call fieldbyname twice    if amindate > fieldbyname(splanallocatiefromdate).asdatetime       amindate := fieldbyname(splanallocatiefromdate).asdatetime;    // 2. put retrieved fieldbyname value in temp var    lempid := fieldbyname(splanallocatieempid).asinteger;    if lempid <> 0 ltsallocatedempids.add(inttostr(lempid));  end; 

will compiler (delphi xe2, win32 app) optimize method 2 use temp var? 2 fieldbynames quite close, nested.

if not, should rewrite 1. because oncalcfields executes often.

btw. know fields[] versus fieldbyname(), or using temp tfield var when running eof loop, not issue here.

no version of delphi compiler this.

such optimizations require compiler able prove 2 calls fieldbyname give same result, , there no provision flagging method being deterministic.

note quite possible in theory (if unlikely in reality) 2 calls not give same result, in case e.g. if different thread deletes field out of collection between first , second call. generally, compiler not know or care @ call site particular method call does.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -