Optimize stack trace capture #138
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With these changes, the current test suite runs O(0.2s-0.3s) faster on a Mid 2015 Macbook Pro with a 2.5GHz Intel Core i7 with 16GB RAM. Not all of the optimizations have the same degree of impact, but all make the code a little less complex and a bit more readable.
Most notably, there is now only the
BATS_STACK_TRACE
variable, rather thanBATS_CURRENT_STACK_TRACE
,BATS_PREVIOUS_STACK_TRACE
, andBATS_ERROR_STACK_TRACE
. The only difference between the two previous stacks was the line number of the frame at the top of each. The stack trace is captured both before a command runs and after it triggers theERR
orEXIT
trap. Depending on the Bash version or type of error, theBASH_LINENO
would change when capturing the second stack trace. We now manage that difference viaBATS_LINENO
andBATS_CURRENT_LINENO
.Note that these timings are a little different than the ones in the commit messages, since I rebooted the machine before collecting these in order to ensure there was as little interference as possible from other processes and memory pressure.
Bash 3.2.57(1)-release before:
Bash 3.2.57(1)-release after:
Bash 4.2.23(1)-release before:
Bash 4.2.23(1)-release after: