Very frequently, I find myself needing to get the root turf of a particular movable atom. Previously I used a get_turf() proc that looked like this:
/proc/get_turf(atom/A)
if(!istype(A)) return
for(A, A && !isturf(A), A=A.loc);
return A
This is a high-traffic proc for us, it gets used a lot. Recently we noticed that locs[1] always seems to contain the root turf. I did a bit of testing where I compared the result of get_turf() to locs[1] and (provided get_turf() did not return null and locs was non-empty) they always seemed to match even for atoms placed inside other atoms while the containing atom got moved around to different turfs.
Since get_turf() is used pretty frequently I decided to replace it with this:
/proc/get_turf(atom/movable/A)
if(isturf(A)) return A
if(A && A.locs.len) return A.locs[1]
return null
Since there is no linear search just a type check and then a lookup into locs I figured it would be faster.
However, I had a closer look at the ref entry for locs and I spotted this:
"If loc is not a turf, it will be the only item in the locs list"
This contradicts the results of my testing. What is going on here? Is there a bug? If not is it safe to use locs in this manner? If so can we expect the observed behaviour to change in a future release?
In either case, what is the fastest correct way to get the root turf for a movable atom?
As for which one is faster, I'm really not sure but my guess would be the locs method. You could run some speed tests of your own though for confirmation.