From c3bb07acaa6c08067d0fa7b3c5a5b0753bd8c664 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Fri, 17 Jun 2022 13:53:35 -0400 Subject: [PATCH] ~33% speed improvement on vis by using already-allocated memory for the vis stack --- include/vis/vis.hh | 5 +++-- vis/flow.cc | 2 +- vis/vis.cc | 23 ++++++++++++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/include/vis/vis.hh b/include/vis/vis.hh index 5f30253b..676ee195 100644 --- a/include/vis/vis.hh +++ b/include/vis/vis.hh @@ -150,14 +150,15 @@ struct pstack_t leaf_t *leaf; portal_t *portal; // portal exiting std::shared_ptr source, pass; - std::shared_ptr windings[STACK_WINDINGS]; // Fixed size windings + winding_t windings[STACK_WINDINGS]; // Fixed size windings + bool windings_used[STACK_WINDINGS]; // whether the winding is used currently qplane3d portalplane; leafbits_t *mightsee; // bit string qplane3d separators[2][MAX_SEPARATORS]; /* Separator cache */ int numseparators[2]; }; -std::shared_ptr &AllocStackWinding(pstack_t *stack); +std::shared_ptr AllocStackWinding(pstack_t *stack); void FreeStackWinding(std::shared_ptr &w, pstack_t *stack); std::shared_ptr ClipStackWinding(std::shared_ptr &in, pstack_t *stack, qplane3d *split); diff --git a/vis/flow.cc b/vis/flow.cc index 9d699782..071e00dc 100644 --- a/vis/flow.cc +++ b/vis/flow.cc @@ -179,7 +179,7 @@ static void RecursiveLeafFlow(int leafnum, threaddata_t *thread, pstack_t *prevs stack.numseparators[1] = 0; for (i = 0; i < STACK_WINDINGS; i++) - stack.windings[i].reset(); + stack.windings_used[i] = false; leafbits_t local(portalleafs); stack.mightsee = &local; diff --git a/vis/vis.cc b/vis/vis.cc index 2e5c9fba..1391b30f 100644 --- a/vis/vis.cc +++ b/vis/vis.cc @@ -63,6 +63,10 @@ void vis_settings::initialize(int argc, const char **argv) settings::vis_settings options; fs::path portalfile, statefile, statetmpfile; + +struct noop_delete { + void operator()(winding_t *p) const { } +}; /* ================== @@ -71,11 +75,14 @@ fs::path portalfile, statefile, statetmpfile; Return a pointer to a free fixed winding on the stack ================== */ -std::shared_ptr &AllocStackWinding(pstack_t *stack) +std::shared_ptr AllocStackWinding(pstack_t *stack) { - for (auto &winding : stack->windings) { - if (!winding) { - return (winding = std::make_shared()); + for (size_t i = 0; i < STACK_WINDINGS; i++) { + if (!stack->windings_used[i]) { + stack->windings_used[i] = true; + return std::shared_ptr(&stack->windings[i], [stack, i]([[maybe_unused]] winding_t *) { + stack->windings_used[i] = false; + }); } } @@ -89,14 +96,16 @@ std::shared_ptr &AllocStackWinding(pstack_t *stack) As long as the winding passed in is local to the stack, free it. Otherwise, do nothing (the winding either belongs to a portal or another stack structure further up the call chain). + + FIXME: is there some way we can refactor this out entirely? the deleter + for stack windings is safe ================== */ void FreeStackWinding(std::shared_ptr &w, pstack_t *stack) { - for (auto &winding : stack->windings) { - if (winding == w) { + for (size_t i = 0; i < STACK_WINDINGS; i++) { + if (stack->windings_used[i] && &stack->windings[i] == w.get()) { w.reset(); - winding.reset(); return; } }