Page 1 of 1

Dictionary Locking

Posted: Mon Jun 24, 2019 10:14 am
by advaro
Hi,
I'm looking at sorting out a few lock issues I'm having with some of our bigger/busiest classes.

Just wanting to confirm, I thought Jade made a change many moons ago, that if the key of a dictionary isn't changed, even if it's set again, the dictionary wasn't locked. But that doesn't look to be the case.

I made a silly little method that sets the key again (to itself) and one that sets the reference that adds it to the dictionary again (also to itself).
begin
aDeviceToken := aDeviceToken;
app.doWindowEvents(5000);
end;

and setting the reference:
begin
rRootObject := rRootObject;
app.doWindowEvents(5000);
end;

Note - the delay is just so I have time to click refresh on my monitor to view the locks.

Running both these methods, even though it is setting the value to itself, creates exclusive locks on the dictionaries involved. Screenshot is attached.

So it appears for any key or reference I should be doing something like:
if aDeviceToken <> deviceToken then
aDeviceToken := deviceToken;
endif;

Is this correct? - as mentioned I thought we no longer had to do that basic check but it appears we do.

Many thanks
Stephen

Re: Dictionary Locking

Posted: Mon Jun 24, 2019 2:30 pm
by Kevin
Hi Stephen,

You're correct, before setting a reference or key value, you should check whether it's actually changing to avoid locking & updating a collection.

I also use this same rule of thumb when updating a variable used as part of a condition to avoid collections being locked when the condition is being re-evaluated.

There may be a good reason why it works this way, but it would be nice if this could be optimized so we didn't have to write the code needed to check if a value is actually changing before setting. For which, you could submit this as an idea here: https://jedi.ideas.jadeworld.com/.

Cheers,
Kevin