pop quiz, which one is fastest? `a = [].set(1, 100...
# lucee
z
pop quiz, which one is fastest?
a = [].set(1, 10000, "zac");
c = 0;
timer type="outline" {
loop array=a index="b" {
c++;
}
}
dump(c);
d = 0;
timer type="outline" {
a.each(function(e){
d++;
});
}
dump(d);
f = 0;
timer type="outline" {
a.each(function(g){
f++;
}, true);
}
dump(f);
a
I'd expect the first one. I'd also expect to never be looking at performance gains by checking what iteration construct I was using.
t
I think you are probably demonstrating a weakness in cftimer for parallel execution
a
Oh yeh, just noticed the
true
on the last one.
z
nothing to do with cftimer
a
#spoilers Running the code... the last one was the slowest to come up with an incorrect answer. "kewl"
z
yeah, coz it's about thread safety, which causes the overhead too
a
So looks like
each
doesn't actually block until all threads have completed before returning? That's a bug innit?
z
for the last one? nah, you need a lock around f++
a
I get a result of [not 10000] when I run that code. I get it's running each iteration in its own thread (from some pool of available threads), but it should wait until all of them are done before continuing, shouldn't it? Or is it the non-atomic nature of
++
that's causing a race condition?
z
the later
a
OK cool.
a
(reading from 2013 in case it's not clear to anyone what we're on about: https://blog.adamcameron.me/2013/03/no-operators-are-not-thread-safe.html, and maybe the article it links to)
a
Shouldn't that "not thread safe, see below" thing be on ALL the compound operators?
z
good catch
there's actually two race conditions with the thread handling, fixed in the latest RC https://luceeserver.atlassian.net/browse/LDEV-4135 https://luceeserver.atlassian.net/browse/LDEV-4204
f
curious though — what kind of “_thread safety”_ is going on in the parallel one that causes all the overhead then? is it locking when reading the array
a
to fetch the value of
g
for each iteration for example? or is it like pagecontext stuff? or is it overhead of creating the threads and checking the status?
z
pageContexts and os threads
a
And that perf issue is largely mitigated by those two tickets you cited?
z
nope they are concurrency bugs. for your mental model, parallel is for doing heavy tasks, it has an overhead due the the isolation required, it's simply not suited for doing little tasks
a
Right to the overhead is "per iteration", and when the loop is not some contrived amplification to demonstrate the point, the innate overhead disappears into the background noise, for all intents and purposes.
z
yeah. would be nice to figure out a way to optionally allow using a super lightweight page context, at the moment we pack the entire house up (request) just to pop round the corner to grab some milk
a
I would say that this is the problem with second-guessing implementation requirements @ language level.
But equally as long as it's caveated in the docs... job done.
z
it's also a case of if you are doing something for performance reasons, you need to profile it