ID:1323052
 
So I'm wrapping up optimizing Stranded's generation and discovered a nifty trick.

Note: This is only useful and can only deal with TURFS - nothing else.

proc/trange(var/Range=0,var/atom/Center=null)
if(Center==null||Range==0) return
var/_x1y1 = locate(min(Center.x-Range,0),min(Center.y-Range,0),Center.z)
var/_x2y2 = locate(max(Center.x+Range,world.maxx),max(Center.y+Range,world.maxx),Center.z)
return block(_x1y1,_x2y2)


Using block() instead of range(), since block only deals with turfs - uses considerably less cpu.

The weird thing is at the time of using trange() there are ONLY turfs on the map at the time but the cpu ussage between it and the normal range() is a margin of 10%.
My best explanation for that behaviour is VM internals. I suspect there's a whole different kind of algorithm going on for block(), compared to range(), view() and company, given the lack of need for centering, and only returning turfs. It can probably just chop up the map using a different data structure, such as ... a 2D range tree or segment tree perhaps.
Its probably because range() does basicaly block() but has to go through all contents lists
Good to know. I've never used block() before, figured other people may not have used it.
Suggestion: Use max() and min() to limit your coordinates. Otherwise you'll end up with null locations if you get too close to the map edge.
Ad:
Has your game ever went over 100% CPU? Want to stop it? Use this code by Jittai and stop your Turf from using 10% CPU.

My game use to go over 90% before i used this code it now uses 0% CPU.

Any errors talk to Jittai.
I am not responsible for any seizures or random knock outs(Talk to Jittai)
In response to Lummox JR
Yeah, I've ran into that issue and applied a precautionary check, thanks for the min/max tip though.
Suggestion: More reasonable default argument for the center- when I see a proc with default arguments, I generally expect it to 'just sort of work' (to some extent) with the supplied defaults. That being said, all of the related built-in procs default the center to 'usr' and that's usually pretty reasonable.

Another suggestion: Sanity check the arguments