systemed / tilemaker

Make OpenStreetMap vector tiles without the stack
https://tilemaker.org/
Other
1.44k stars 229 forks source link

Avoid copying strings/tag maps #577

Closed cldellow closed 10 months ago

cldellow commented 10 months ago

OSMLuaProcessing's Find, setWay, setNode and setRelation functions are called a lot, so avoiding copies is helpful. Avoiding these copies results in a savings of ~1.5% of the time from startup to ready-to-write-output for my usage.

I believe the changes are safe: the tag map is only accessed for the duration of setWay, setNode and setRelation, so the pointer to the map in the caller's scope is fine.

I believe returning a reference from ::Find is also OK: for std::string, the relevant code in Kaguya is: https://github.com/satoren/kaguya/blob/9d77cad73846f5d8481f3f7e86e8c53c4891c2fb/include/kaguya/type.hpp#L654-L657, which uses lua_pushlstring to pass it to Lua code.

And according to https://www.lua.org/pil/24.2.1.html:

Lua never keeps pointers to external strings (or to any other object, except to C functions, which are always static). For any string that it has to keep, Lua either makes an internal copy or reuses one. Therefore, you can free or modify your buffer as soon as these functions return.

systemed commented 10 months ago

Another good one and seems to work flawlessly - thanks!