Cache callstack results to avoid expensive lookup This works well as our use-case is to lookup the same addresses multiple times (loops, helper functions, etc.). With a simple timing of ReactorUnitTests.exe, with this optimization enabled, total time dropped from 16.72 seconds to 2.24 seconds (~7.5X faster). This optimization is especially important for Mac where resolving stack addresses takes much longer than on other platforms. With this optimization, running a specific gles-unittests test, it was able to output the callstack trace in about 30 seconds; without, it was still emitting jit code after 10 minutes at which point I gave up. Bug: b/131425026 Change-Id: I97e76256220f73fba0a1fc4099532e833f561551 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/32369 Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Tested-by: Antonio Maiorano <amaiorano@google.com> Reviewed-by: Nicolas Capens <nicolascapens@google.com> Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/Reactor/ReactorDebugInfo.cpp b/src/Reactor/ReactorDebugInfo.cpp index 246574d..cd7cc44 100644 --- a/src/Reactor/ReactorDebugInfo.cpp +++ b/src/Reactor/ReactorDebugInfo.cpp
@@ -20,6 +20,7 @@ # include "boost/stacktrace.hpp" # include <algorithm> +# include <unordered_map> namespace rr { @@ -58,21 +59,35 @@ std::vector<Location> locations; - // Note that bs::stacktrace() effectively returns a vector of addresses; bs::frame construction is where - // the heavy lifting is done: resolving the function name, file and line number. namespace bs = boost::stacktrace; + + // Cache to avoid expensive stacktrace lookups, especially since our use-case results in looking up the + // same call stack addresses many times. + static std::unordered_map<bs::frame::native_frame_ptr_t, Location> cache; + for(bs::frame frame : bs::stacktrace()) { - if(shouldSkipFile(frame.source_file())) + Location location; + + auto iter = cache.find(frame.address()); + if(iter == cache.end()) + { + location.function.file = frame.source_file(); + location.function.name = frame.name(); + location.line = frame.source_line(); + cache[frame.address()] = location; + } + else + { + location = iter->second; + } + + if(shouldSkipFile(location.function.file)) { continue; } - Location location; - location.function.file = frame.source_file(); - location.function.name = frame.name(); - location.line = frame.source_line(); - locations.push_back(location); + locations.push_back(std::move(location)); if(limit > 0 && locations.size() >= limit) {