argumentParsers: precompile argument and string maps #130
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.
The Fix
Many String() and Parse functions had inner constant helper maps.
These maps were reallocated on each call, which could overtime cause signifacnt GC overhead.
This fix initializes the maps before the functions, making sure they are reused and not repeatedly reallocated.
Performance analysis
This fix was found while benchmarking tracee with a specific workload - a script running
strace ps -ef
repeatedly.Running pprof on this case gave the following flame graph:
Note the runtime.mapassign_fast64 that is a significant repeating code path.
With this fix we get this flame graph:
Simply preallocating the map one time on the module init avoids the constant repeating calls to runtime.mapassign_fast64 while constructing the map on each function call.
Of course, this workload brought out this function on the extreme due to it being relevant to the workload running in a loop, however it isn't impossible that these helpers would cause overhead overtime for different type of workloads (There are many more maps there were optimized in this PR).
This is a quick and easy change which is similar in nature to precompiling a regex.