Can you set custom properties by referenced value?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Monox18
Posts: 118
Joined: Tue Nov 25, 2014 9:48 pm

Can you set custom properties by referenced value?

Post by Monox18 » Fri Jul 05, 2019 9:24 pm

Hello all,

There's is some code I'm testing in a Line Graph widget. I use this to make the widget "live" and update it's graphData every x ms. When the data contains 145 lines, it takes 112ms average, per iteration. If I raise the number of data to 300 lines (I want to be able to display longer trends) it raises to 280ms average per iteration and naturally the GUI becomes a bit slower.

So, I was wondering if the line "set the graphData of me to sGraphData" could be changed somehow so that the value is passed by reference instead of copying the value, and thus anytime sGraphData changes, the widget would update itself without me having to tell him when to do it.

the LoopPaint is every 50ms, but the uFlag updates about every 400ms.

Code: Select all

command LoopPaint
   local tTime
   if the uFlag of me then
      set the uFlag of me to false
      delete line 1 of sGraphData
      put "," & the uSP of me & "," & the uPV of me & "," & the uCV of me &\
            cr & ",100,50,-5" into line - 1 of sGraphData
      
      put the milliseconds into tTime
      set the graphData of me to sGraphData //Line being tested
      put the milliseconds - tTime into tTime
      
      add 1 to sCounter
      add tTime to sSum 
      put round( sSum/sCounter ,0) into tTime
      writeLog tTime 
   end if
   send "LoopPaint" to me in 50 milliseconds 
end LoopPaint


Thanks!
Monox
Developing a Cyber Physical System.
https://www.monoxware.com/

Xero
Posts: 157
Joined: Sat Jun 23, 2018 2:22 pm

Re: Can you set custom properties by referenced value?

Post by Xero » Tue Jul 23, 2019 8:11 am

I'm not sure if I get exactly what you want (honestly, I am not familiar with graph widgets), but, if you are trying to find code to track whether the data has changed, you might consider trying something like this:
Assign a variable to hold the current line data that you are checking (tCheck- this may need to be a global to ensure it's always tracked and retained. Be sure to call it at the start with 'Global tCheck')
Then, just before you set the graphData, use a line like this:

Code: Select all

if line 1 of sGraphData = tCheck then
pass loopPaint
Which would effectively check to see if the data is different, and end the process before the updating of the data.
Not sure if the syntax will work, you may need to play with it, but the concept should work. You could also do it in the negative with if line 1 of sGraphData is not tCheck then... and continue with the rest of your process.
There is one drawback to this: if the data you are looking at is unchanging, or repetitive, it will be skipped. i.e. if you were tracking interest rates, and they were 3% on time check 1, and 3% on time check 2, it would put nothing into time check 2, and your data would go out of whack.
Hope that helps, if not, maybe you could post a little more detail about what you are doing?
XdM

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Can you set custom properties by referenced value?

Post by dunbarx » Tue Jul 23, 2019 1:44 pm

Hi.

Setting a custom property does not take a lot of time. Have you tried reducing the 50 milliseconds?

Craig

Monox18
Posts: 118
Joined: Tue Nov 25, 2014 9:48 pm

Re: Can you set custom properties by referenced value?

Post by Monox18 » Thu Aug 22, 2019 8:33 pm

Xero thank you for your suggestion. I'm already doing something similar. the uFlag of me is a property that is changed in another handler that gets set to true when there's actually a change on the data.

dunbarx, it's true setting a property is fast but in this case setting the graphData of the widget is a LC command that varies on the amount of points. The more data points the more graphics it has to create and thus slower.

For now it works very well, was just wondering if it could be faster. I'm just limited to keep the data to draw to a few lines to make it faster. I left that part of the code as it is, and moved to the next parts.
Monox
Developing a Cyber Physical System.
https://www.monoxware.com/

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Can you set custom properties by referenced value?

Post by FourthWorld » Thu Aug 22, 2019 8:48 pm

Monox18 wrote:
Fri Jul 05, 2019 9:24 pm
So, I was wondering if the line "set the graphData of me to sGraphData" could be changed somehow so that the value is passed by reference instead of copying the value, and thus anytime sGraphData changes, the widget would update itself without me having to tell him when to do it.
Pass by copy vs by reference is about memory management, and since v7 LC now does copy-on-write for pretty much all operations, so unless the data is being changed the original passed to a routine is probably what's being used. In recent versions we rarely find cases where explicit by-reference handler definitions buy us much we're not already getting for free with that engine enhancement.

Here, the issue seems more about messaging than memory, triggers specifically.

While we do have getProp and setProp handlers that can trigger any actions you like when the value of a custom property is read or written, what you're looking for seems a bit more specific than that, more akin to a publish-and-subscribe mechanism.

There is no generalized publish-and-subscribe built into the LC engine, so in lieu of that your polling mechanism with a timer seems pretty good.

Alternatively, you might consider building in a trigger for the chart update into the routine which obtains the data, but without seeing that end of things I'm unable to advise if it would be any simpler, efficient, or more performant.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Monox18
Posts: 118
Joined: Tue Nov 25, 2014 9:48 pm

Re: Can you set custom properties by referenced value?

Post by Monox18 » Tue Aug 27, 2019 7:18 pm

Hey FourthWorld thank you very much for your comprenshensive answer. You understood very well the kind of mechanism I'm looking forward and you explanation made the situation clear for me. Since that is not built into LC, I will leave it with the current timed polling mechanism. I will reconsider this approach later if widgets recieve some updates.
Monox
Developing a Cyber Physical System.
https://www.monoxware.com/

Post Reply