Repository navigation
decode index entries and objects without a heap allocation - #19
Conversation
…cation
Idx_Handle (and Handle, which the block iterators use for objects) decoded
the entry under the iterator on first access with new Object(ptr), and
deleted it when the iterator moved on. Every index() and object() call on a
fresh entry thus cost a heap allocation and a free, even for 4-byte types
such as Uint32_Index and for fixed-size skeletons.
Idx_Handle now decodes into an alignas(Object) buffer inside the handle
with placement new and runs the destructor in clear(). Copying and
assigning a handle are now deleted: nothing does either, a copied obj
would point into the other handle's storage, and the copy assignment
operator had no return statement.
Nova Scotia extract with history, warm cache, 4-core VM: median CPU of
25 runs of each build, alternating. Output is identical:
[out:json][timeout:300]; (node["telecom"="data_center"]; way["telecom"="data_center"]; ); out center tags;
25.1 ms -> 19.5 ms (-22%)
[out:csv(::count)]; node(44.60,-63.66,44.70,-63.52); out count;
36.9 ms -> 30.8 ms (-17%)
(rel['seamark:type']; >; way['seamark:type']; >; node['seamark:type']; ); out meta;
206 ms -> 173 ms (-16%)
[out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta;
335 ms -> 299 ms (-11%)
[out:json]; way[highway](44.60,-63.66,44.70,-63.52); out geom;
461 ms -> 422 ms (-8%)
[out:csv(::count)]; nwr[amenity=restaurant]; out count;
148 ms -> 148 ms (0%)
Co-Authored-By: Colin Dellow <cldellow@gmail.com>
|
Addressing this part of the code is spot on. You can even get rid of the very first new and enable maximum optimization potential by keeping data in registers only: https://github.com/mmd-osm/Overpass-API/blob/test7591/src/template_db/block_backend.h That change is quite large and touches everything, so not a good option for now. in general there’s more in this file to avoid useless work, but again not a small change. |
|
Just want to make sure I'm following - are you saying that the other changes in your fork are not a good option for now (they provide a big performance improvement but they change the interface and have a big ripple effect throughout the code), or that this particular PR is not a good option for now? I'm trying to shoot for a good compromise between maximum performance boost and minimum disruption in the code -- I was hoping this PR threaded the needle, but feedback from people who actually know the codebase is very helpful to me in calibrating how risky things are. |
|
It was more of an outlook. To unlock bigger savings you’d have to do bigger changes that touch more places all over the codebase. Getting rid of repeated shortlived heap allocations is definitely a step in the right direction. new with placement feels a bit hacky. We need to be extra careful in the future that nobody is keeping references to that object for too long. |
|
I think I'm following you, yeah, there's a bound on how much performance can be extracted by nibbling around the edges like this. Still, I'm fairly optimistic that we can at least 2x throughput with enough of these sorts of improvements.
Agreed, I like that your approach removes the lifetime semantics (at the cost of updating call sites and being a bigger review). For now, I think it might be the lesser of two evils.
I think this PR at least doesn't make the semantics worse - they're unchanged, so the same level of care is needed as before. With the appropriate macros and enabling ASan in CI, we'd also start catching violations seen by the test suites, at least. |
|
I agree that the allocation semantics aren't really changed with this PR. So looks good to me. |
This speeds things up by avoiding heap allocations in a hot path.
Idx_Handle and Handle allow lazy decoding of objects from blocks.
That previously did a heap allocation on every first access as an iterator swept across the blocks.
Instead, this PR allocates the memory once, and uses placement new to avoid doing a heap allocation.
The previous implementation of the assignment operator had undefined behaviour (it returned nothing). This was fine, because nothing called it. This PR makes that explicit by deleting it, and deleting the copy constructor while we're here, too.
This PR doesn't change the semantics of the handles - a caller holding on to a pointer after its iterator has moved on is invalid in the current code, and invalid in the new code. The current code has the benefit that ASan would detect such invalid use. ASan would not detect such invalid use in the new code. The new code could be extended to use
ASAN_POISON_MEMORY_REGION(...)andASAN_UNPOISON_MEMORY_REGION(...)macros to make such incorrect use also detectable by ASan. I've left that out for now - maybe once tests are in the CI, we can have them run an ASan build, and enable those flags.The boost varies by query, across the top-20 queries from Kai's analysis on gall/lambert, on a bbox that covered Boston, the median speedup was 7%.
Some examples from Nova Scotia: