Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

reduce allocations when printing stack traces (#149) #150

Merged
merged 1 commit into from
Jan 5, 2019

Conversation

cstockton
Copy link

This implements proposal in #149 by adding a format method to Frame that accepts an io.Writer, allowing stack and StackTrace to grow a bytes.Buffer ahead of time. The benefits are illustrated in the benchmark comparison below, in summary allocations are reduced by 90-97% resulting in a 40-55% performance boost.

This comes at a slight cost in bytes used, which is around 5% higher on average with a single case reaching 36% (small stack, %s) which is probably not worth special casing. This trade off can be affected by adjusting stackMinLen with a more generous multiplier, lowering allocations at the expense of unused buffer.

benchmark                                         old ns/op     new ns/op     delta
BenchmarkStackFormatting/%s-stack-10-24           411           402           -2.19%
BenchmarkStackFormatting/%v-stack-10-24           411           416           +1.22%
BenchmarkStackFormatting/%+v-stack-10-24          29551         17147         -41.97%
BenchmarkStackFormatting/%s-stack-30-24           377           410           +8.75%
BenchmarkStackFormatting/%v-stack-30-24           411           385           -6.33%
BenchmarkStackFormatting/%+v-stack-30-24          65337         29997         -54.09%
BenchmarkStackFormatting/%s-stack-60-24           444           396           -10.81%
BenchmarkStackFormatting/%v-stack-60-24           441           385           -12.70%
BenchmarkStackFormatting/%+v-stack-60-24          67739         31652         -53.27%
BenchmarkStackFormatting/%s-stacktrace-10-24      12894         7409          -42.54%
BenchmarkStackFormatting/%v-stacktrace-10-24      20893         11611         -44.43%
BenchmarkStackFormatting/%+v-stacktrace-10-24     31120         17046         -45.22%
BenchmarkStackFormatting/%s-stacktrace-30-24      24363         13724         -43.67%
BenchmarkStackFormatting/%v-stacktrace-30-24      48716         23995         -50.75%
BenchmarkStackFormatting/%+v-stacktrace-30-24     68169         31939         -53.15%
BenchmarkStackFormatting/%s-stacktrace-60-24      25448         14208         -44.17%
BenchmarkStackFormatting/%v-stacktrace-60-24      46780         24099         -48.48%
BenchmarkStackFormatting/%+v-stacktrace-60-24     68103         34016         -50.05%

benchmark                                         old allocs     new allocs     delta
BenchmarkStackFormatting/%s-stack-10-24           2              2              +0.00%
BenchmarkStackFormatting/%v-stack-10-24           2              2              +0.00%
BenchmarkStackFormatting/%+v-stack-10-24          77             7              -90.91%
BenchmarkStackFormatting/%s-stack-30-24           2              2              +0.00%
BenchmarkStackFormatting/%v-stack-30-24           2              2              +0.00%
BenchmarkStackFormatting/%+v-stack-30-24          162            4              -97.53%
BenchmarkStackFormatting/%s-stack-60-24           2              2              +0.00%
BenchmarkStackFormatting/%v-stack-60-24           2              2              +0.00%
BenchmarkStackFormatting/%+v-stack-60-24          162            4              -97.53%
BenchmarkStackFormatting/%s-stacktrace-10-24      33             4              -87.88%
BenchmarkStackFormatting/%v-stacktrace-10-24      63             7              -88.89%
BenchmarkStackFormatting/%+v-stacktrace-10-24     77             7              -90.91%
BenchmarkStackFormatting/%s-stacktrace-30-24      67             4              -94.03%
BenchmarkStackFormatting/%v-stacktrace-30-24      131            4              -96.95%
BenchmarkStackFormatting/%+v-stacktrace-30-24     162            4              -97.53%
BenchmarkStackFormatting/%s-stacktrace-60-24      67             4              -94.03%
BenchmarkStackFormatting/%v-stacktrace-60-24      131            4              -96.95%
BenchmarkStackFormatting/%+v-stacktrace-60-24     162            4              -97.53%

benchmark                                         old bytes     new bytes     delta
BenchmarkStackFormatting/%s-stack-10-24           16            16            +0.00%
BenchmarkStackFormatting/%v-stack-10-24           16            16            +0.00%
BenchmarkStackFormatting/%+v-stack-10-24          2647          2954          +11.60%
BenchmarkStackFormatting/%s-stack-30-24           16            16            +0.00%
BenchmarkStackFormatting/%v-stack-30-24           16            16            +0.00%
BenchmarkStackFormatting/%+v-stack-30-24          5923          6269          +5.84%
BenchmarkStackFormatting/%s-stack-60-24           16            16            +0.00%
BenchmarkStackFormatting/%v-stack-60-24           16            16            +0.00%
BenchmarkStackFormatting/%+v-stack-60-24          5923          6269          +5.84%
BenchmarkStackFormatting/%s-stacktrace-10-24      632           864           +36.71%
BenchmarkStackFormatting/%v-stacktrace-10-24      920           924           +0.43%
BenchmarkStackFormatting/%+v-stacktrace-10-24     2671          2975          +11.38%
BenchmarkStackFormatting/%s-stacktrace-30-24      1313          1521          +15.84%
BenchmarkStackFormatting/%v-stacktrace-30-24      1922          1617          -15.87%
BenchmarkStackFormatting/%+v-stacktrace-30-24     5949          6295          +5.82%
BenchmarkStackFormatting/%s-stacktrace-60-24      1313          1521          +15.84%
BenchmarkStackFormatting/%v-stacktrace-60-24      1922          1617          -15.87%
BenchmarkStackFormatting/%+v-stacktrace-60-24     5949          5910          -0.66%

@cstockton
Copy link
Author

Would you like any additional work done for this pull req @davecheney ?

@gregwebs
Copy link

@lysu do you want to pull this into our fork?

@davecheney davecheney added this to the 0.9 milestone Oct 8, 2018
@davecheney davecheney merged commit e1ac100 into pkg:master Jan 5, 2019
davecheney added a commit that referenced this pull request Jan 9, 2019
davecheney added a commit that referenced this pull request Jan 9, 2019
Updates #150

Signed-off-by: Dave Cheney <dave@cheney.net>
davecheney added a commit that referenced this pull request Jan 9, 2019
Updates #150

Signed-off-by: Dave Cheney <dave@cheney.net>
davecheney added a commit that referenced this pull request Jan 9, 2019
Updates #150

Signed-off-by: Dave Cheney <dave@cheney.net>
davecheney added a commit that referenced this pull request Jan 9, 2019
Updates #150

Signed-off-by: Dave Cheney <dave@cheney.net>
@cstockton
Copy link
Author

@davecheney I'm glad to see performance improvements get in, was there any improvements I could have made to the patch that could have allowed me to maintain authorship?

I was trying to follow the commits above to see if I made any mistakes, from the diff it appears you didn't want to Grow a buffer? This has a pretty big impact on performance of the patch in the common %+v scenario:

BenchmarkStackFormatting/%+v-stack-10-24          15221         21093         +38.58%
BenchmarkStackFormatting/%+v-stack-30-24          28797         43941         +52.59%
BenchmarkStackFormatting/%+v-stack-60-24          28479         42015         +47.53%

BenchmarkStackFormatting/%+v-stack-10-24          6              19             +216.67%
BenchmarkStackFormatting/%+v-stack-30-24          3              33             +1000.00%
BenchmarkStackFormatting/%+v-stack-60-24          3              33             +1000.00%

That said I would have been happy to remove it to maintain authorship for the work I put into fixing this and patiently waiting for it to be reviewed and merged, just something to consider for your future contributors. Thanks.

@davecheney
Copy link
Member

davecheney commented Jan 27, 2019 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants