I’ve rewritten Instance.new a bit using that “wait until thread yields” idea.
It’s almost as fast as setting .Parent as last field, much faster than the regular
(I said “until thread yields”, but it’s more like “after all pending threads yielded”, can’t do better)
Here’s the (test) code, along with the output of running it 5x: http://pastebin.com/VwirmUsW
Here’s a test run:
0.00053739547729492 -- Instance.new(a) setFields() setParent()
0.075802326202393 -- Instance.new(a,b) setFields()
0.00056886672973633 -- custom_new(a,b) setFields()
nil -- parent of custom_new(a,b) right after setting the fields
Folder 0.0021722316741943 -- parent + passed time after obj.Changed:wait()
Basically, add the end of the tick, it (only then) sets the parent of all
The “mutex function” only runs once, at the end of the tick that
mutex:Fire() got called.
coroutine.yield(), the parent is guaranteed to be set to what you wanted.
Seems like it’s almost as fast as setting the Parent afterwards, but Parent is only set after a semi-tick.
You can also do
mutex.Event:wait() if you need to wait until everything (of the current tick) is parented.
Instance.new(a,b), but Parent is only set somewhere before the next tick
(it’s still better to just manually replace all
Instance.new(a,b) with a variant that sets .Parent afterwards)
One thing I noticed:
Instance.new("Part",workspace) takes longer depending on how many parts are already present.