diff options
author | sapier <Sapier at GMX dot net> | 2013-04-21 15:54:29 +0200 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2013-04-24 21:59:09 -0400 |
commit | e9a4e98cb9608e26e7dafc575856aad9da5c27da (patch) | |
tree | 5f4b42d36464d838e87912ff8698e82a3684ee47 | |
parent | 625a4c2e662f6b69b73a2a828d1b08d72e53ff73 (diff) | |
download | minetest-e9a4e98cb9608e26e7dafc575856aad9da5c27da.tar.gz minetest-e9a4e98cb9608e26e7dafc575856aad9da5c27da.tar.bz2 minetest-e9a4e98cb9608e26e7dafc575856aad9da5c27da.zip |
Improve getFree*ActiveObjectId to reduce common case cpu usage drasticaly
-rw-r--r-- | src/environment.cpp | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/src/environment.cpp b/src/environment.cpp index fc7972b2c..438c9ef4f 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -1295,16 +1295,17 @@ bool isFreeServerActiveObjectId(u16 id, u16 getFreeServerActiveObjectId( std::map<u16, ServerActiveObject*> &objects) { - u16 new_id = 1; + //try to reuse id's as late as possible + static u16 last_used_id = 0; + u16 startid = last_used_id; for(;;) { - if(isFreeServerActiveObjectId(new_id, objects)) - return new_id; + last_used_id ++; + if(isFreeServerActiveObjectId(last_used_id, objects)) + return last_used_id; - if(new_id == 65535) + if(last_used_id == startid) return 0; - - new_id++; } } @@ -2296,16 +2297,17 @@ bool isFreeClientActiveObjectId(u16 id, u16 getFreeClientActiveObjectId( std::map<u16, ClientActiveObject*> &objects) { - u16 new_id = 1; + //try to reuse id's as late as possible + static u16 last_used_id = 0; + u16 startid = last_used_id; for(;;) { - if(isFreeClientActiveObjectId(new_id, objects)) - return new_id; + last_used_id ++; + if(isFreeClientActiveObjectId(last_used_id, objects)) + return last_used_id; - if(new_id == 65535) + if(last_used_id == startid) return 0; - - new_id++; } } |