ID:1992635
 
Applies to:DM Language
Status: Open

Issue hasn't been assigned a status value.
Doing set waitfor = 0 in the target proc is massively faster than using spawn()

Noticeable if you do a loop that calls something that sleeps, and you want to make it happen simultaneously rather then one after the other, calling a lot of spawn()s adds up quickly.

Proof:
                             Profile results (total time)
Proc Name Self CPU Total CPU Real Time Calls
---------------------------------- --------- --------- --------- ---------
/mob/proc/testwaitforspawn3 2.473 2.474 28438.984 20000
/mob/proc/testwaitfor3 2.018 2.019 29180.518 20000
/mob/proc/testwaitforspawn2 1.002 3.479 28439.971 20000
/mob/proc/testwaitfor2 0.031 2.049 29180.541 20000
/mob/verb/testwaitforspawn 0.023 0.976 0.976 1
/mob/verb/testwaitfor 0.021 2.059 2.059 1
/mob/verb/testwaitforprocoverhead 0.020 0.035 0.035 1
/mob/proc/testwaitforprocoverhead2 0.015 0.015 0.020 20000
/mob/proc/testwaitforprocoverhead3 0.000 0.002 0.003 20000

mob
verb/testwaitfor()
for (var/i = 0, i < 20000, i++)
testwaitfor2(i)
proc/testwaitfor2(i)
set waitfor=0
testwaitfor3()
proc/testwaitfor3()
sleep 1

verb/testwaitforspawn()
for (var/i = 0, i < 20000, i++)
testwaitforspawn2(i)
proc/testwaitforspawn2(i)
spawn
testwaitforspawn3()
proc/testwaitforspawn3()
sleep 1


verb/testwaitforprocoverhead()
for (var/i = 0, i < 20000, i++)
testwaitforprocoverhead2(i)
proc/testwaitforprocoverhead2(i)
testwaitforprocoverhead3()
proc/testwaitforprocoverhead3();



The reason this is faster, seems to be because it doesn't have to copy the vars to the spawn()'ed section.

Related: https://github.com/tgstation/-tg-station/pull/13443
http://web.archive.org/web/20060722121013/http:// developer.byond.com/docs/notes/225.html
The reason it's faster is because the procedure returns when it hits a sleeping procedure or function and then continues to execute.
I mean it has less overhead then spawn does...
the profiles speak for themselves...
+1 on this-- it probably also helps that you're not explicitly forcing a context switch anymore.
In response to MrStonedOne
MrStonedOne wrote:
the profiles speak for themselves...

Yeah, I'm not arguing, but they're not doing the exact same thing either way
In response to Super Saiyan X
I think that's the confusing part. He wasn't wishing spawn() to go away, or wanting anyone to necessarily advocate using waitfor instead. He just wants it added back to the documentation so it doesn't just get silently deprecated while he's using it.
functionally speaking, in almost every case you'd want to use spawn without a time argument, waitfor = 0 does the same thing..

Ya, internally they are very different, syntax wise they are very different, but spawn is and has always been, primarily used to prevent a sleep from hanging the stack.

set waitfor at the proper place in the stack does the exact same thing, with the bonus of having significantly less overhead.
spawn is very expensive, so avoiding using it is a massive +
You can set waitfor for procs and the like---waitfor functions similarly to spawn(), but has considerably less overhead if it encounters a sleep and none at all if it doesn't---yet it's completely and totally undocumented.

It would be great if this was fully documented on the DM reference.
In response to Fox P McCloud
Bump.