Fastest way to detect changed property slot

105 views
Skip to first unread message

argeden

unread,
Dec 23, 2011, 10:28:22 AM12/23/11
to Sedona Framework
Hi,

Let's say, we have a component that have 4 or more properties :

property int in1
propery float in2
property bool in3
property float in4
....
....

If anyone of these changes, "changed(Slot s)" method runs. This is OK.
We want to detect the changed slot by the fastest way.
The possibilities that we know are :

a) If (s.name=="in2") ....
b) If (s.id==2) .......

Is there a better(faster) way or any suggestions?

thanks

Dan Giorgis

unread,
Jan 2, 2012, 10:20:49 AM1/2/12
to sedo...@googlegroups.com
I'd recommend using == to compare slots.  There are several examples in the control kit source.

if (s == in1)
...
else if (s == in2)
...
else if (s == in3)
...


I'm not an expert on the compiler side, but I suspect the that slot == in2 is converted to (s.id == 2) at compile time.  Using slot names is certainly more readable and easier to maintain than referencing slot ids directly.





--
You received this message because you are subscribed to the Google Groups "Sedona Framework" group.
To post to this group, send email to sedo...@googlegroups.com.
To unsubscribe from this group, send email to sedonadev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sedonadev?hl=en.


argeden

unread,
Jan 3, 2012, 1:15:59 AM1/3/12
to Sedona Framework
Thanks for the recommendation.

As an information to everybody:
I have checked the ".ir" files. The compiler does not generate same
code for "slot==in2" and "s.id==2". And also, "s.id==2" works
approximately %4 faster than "slot==in2"(tested on our Sedona device).

I totally agree that using of "slot==in2" is more readable and
understandable. But sometimes the faster one may be preferred
according to the need.

Regards

Matthew

unread,
Jan 3, 2012, 9:06:46 AM1/3/12
to sedo...@googlegroups.com
I think using "s.id == 2" is a ticking time bomb for bugs.  If you ever modify your component to add new properties or actions, the slot ids could get re-assigned and then that test condition will no longer be valid.

Perhaps a good compiler optimization would be to do that under-the-covers at compile time (this would require some investigation). But I think you are setting yourself up for some nasty bugs down the road by explicitly testing against the slot id.

argeden

unread,
Jan 3, 2012, 10:24:47 AM1/3/12
to Sedona Framework
I know exactly what you mean. It is our risk to get some more speed.

And of course, we will appreciate if the compiler make it
automatically.

Regards

McKenney, Elizabeth (Tridium)

unread,
Mar 6, 2012, 11:28:56 AM3/6/12
to sedo...@googlegroups.com

Question: Do you get the same 4% performance boost if you use the syntax

if (s.id==MyClass.in2.id)

?

argeden

unread,
Mar 7, 2012, 9:47:50 AM3/7/12
to Sedona Framework
Answer: No, "s.id==2" is still fastest. Because, if you take a look
at:

class deneme1 extends Component
{
@readonly property bool out

virtual override void changed(Slot s)
{
if (s.id==1) out=false
}
}

deneme1's ir :

public virtual override void changed(sys::Slot)
{
0: LoadParam1
1: Load8BitFieldU1 sys::Slot.id
2: LoadI1
3: JumpIntNotEq 8
4: LoadParam0
5: LoadSlot ontrolArge::deneme1.out
6: LoadI0
7: Call sys::Component.setBool
8: ReturnVoid
}

class deneme2 extends Component
{
@readonly property bool out

virtual override void changed(Slot s)
{
if (s.id==deneme2.out.id) out=false
}
}

deneme2's ir :

public virtual override void changed(sys::Slot)
{
0: LoadParam1
1: Load8BitFieldU1 sys::Slot.id
2: LoadSlot ontrolArge::deneme2.out
3: Load8BitFieldU1 sys::Slot.id
4: JumpIntNotEq 9
5: LoadParam0
6: LoadSlot ontrolArge::deneme2.out
7: LoadI0
8: Call sys::Component.setBool
9: ReturnVoid
}

As you can see, the second one has 9 commands instead of 8. So,
"s.id==2" is faster.
Reply all
Reply to author
Forward
0 new messages