Hi Dojo,
Was looking through a code review and had a question purely based off curiosity: when setting a parameter via SLScripting, you see both protocol.SetParameter(PID, value) and setting the property directly e.g:
protocol.<parametername_PID> = value;
Are there performance differences? Why does it seem to be more common to see protocol.SetParameter vs setting the property directly?
Thanks in advance!
Hi,
The protocol.<parametername> = value; becomes accessible when using SLProtocolExt instead of SLProtocol. This helper was added to have a more natural coding style when getting or setting a parameter value.
In the background this is creating an extra interface SLProtocolExt that defines object <parametername> {get; set;}, which then has the class implementation ConcreteSLProtocolExt public object <parametername> { get {return GetParameter(<pid>);} set {SetParameter(<pid>, value);}}
This means that it is calling the same SetParameter method in the background.
-Are there performance differences?
Yes:
-As soon as Run(SLProtocolExt protocol) is being defined in a QAction then the ConcreteSLProtocolExt will be constructed and all the table parameter object types will be added to the heap. This makes it more difficult for the one that needs to debug an SLScripting memory dump as all these types are present on the heap. Thumb rule: if protocol.<parametername> is not being used by the code then please do no define Run(SLProtocolExt protocol), but try to use Run(SLProtocol protocol) as much as possible as the compiler does not need to create all the parameter objects and they will not be added to the heap.
-When protocol.<parametername> = value; is being used then in the background it needs to call the setter, which in turn calls SetParameter(); so this means an extra method being added to the callstack. This means that calling protocol.SetParameter(pid, value) will be more performant compared to protocol.<parametername> = value;
Also note that protocol.SetParameter has more flexibility, e.g. DateTime can be specified to be able to do history sets. protocol.SetParameters also exists to be able to perform multiple sets in one call, which will be more performant compared to calling multiple protocol.<parametername> = value calls one by one.
All this makes that my personal preference is to use protocol.SetParameter (or protocol.SetParameters) instead of protocol.<parametername> and not to use SLProtocolExt if not needed when defining the Run method.
Regards,