Applies to:DM reference
Status: Open

Issue hasn't been assigned a status value.
So basically doing
for(var/datum/foo/bar as() in list)

is faster than the usual typecheckless
for(var/i in list)
var/datum/foo/bar = i

And it'd be nice to have some documentation in the reference ( pertaining to what exactly it does
I've honestly never encountered as() as a syntax. I didn't think parentheses could be used in an as clause at all. But it doesn't seem like it should do anything here, since it's not actually supplying any kind of filter. I think if you removed it you'd have identical performance.

From what I can see, the only difference between your two versions is that the second one is doing an extra assignment and the first is calling istype() internally. When I was going through the logic of both, it occurred to me that you have two incref and decref calls either way, so I'm not at all sure why the istype() version would be faster.
What we have observed is that using as() suppresses the istype() check. It's faster for that reason. Compare the compiler's output for these two procs:


for(var/datum/foo/bar in list());

for(var/datum/foo/bar as() in list());

It's weird, but it's useful.
I documented some of the actual internals that are happening with this in this bug report:
For the record these are the bitflags if anyone cares to know
world.log << as()   //0
world.log << as(mob)//1
world.log << as(obj)//2
world.log << as(text)//4
world.log << as(num)//8
world.log << as(file)//16
world.log << as(turf)//32
world.log << as(key)//64
world.log << as(null)//128
world.log << as(area)//256
world.log << as(icon)//512
world.log << as(sound)//1024
world.log << as(message)//2048
world.log << as(anything)//4096
world.log << "missing/undocumented"//8192
world.log << "missing/undocumented"//16384
world.log << as(password)//32768
world.log << as(command_text)//65536
world.log << as(color)//131072
In response to TiviPlus
The undocumented ones are used only internally on the client.

Login to reply.