Skip to content

Conversation

@asergunov
Copy link

What this PR does / why we need it:

Spotted calls to strconv.Quote which allocates the new string. Replaced with strconv.AppendQuote.

In most of the places I'm using the pattern:

b.Write(strconv.AppendQuote(b.AvailableBuffer(), str))

So we reusing the already allocated buffer if it's available.

The only exception is stack-allocated fixed size buffers we have introduced in #413. As you can see from the benchmark_out_reduce-allocations-fix-alloc.txt column it's faster in benchmarks as it is. The benchmark_out_reduce-allocations.txt is a results with these places also replaced and you can see it performs worse in some cases. So I kept that code untouched.

goos: linux
goarch: amd64
pkg: k8s.io/klog/v2/textlogger
cpu: 13th Gen Intel(R) Core(TM) i5-1345U
                                                                               │ benchmark_out_main.txt │ benchmark_out_reduce-allocations-fix-alloc.txt │ benchmark_out_reduce-allocations.txt  │
                                                                               │         sec/op         │         sec/op           vs base               │    sec/op      vs base                │
TextloggerOutput/handle_odd-numbers_of_KVs_in_both_log_values_and_Info_args-12             4.363µ ± 19%             1.429µ ±   4%  -67.25% (p=0.001 n=7)    1.981µ ± 35%   -54.60% (p=0.001 n=7)
TextloggerOutput/KObjs-12                                                                  2.855µ ± 47%             1.285µ ±  12%  -54.99% (p=0.001 n=7)    3.992µ ±  7%   +39.82% (p=0.017 n=7)
TextloggerOutput/KObjSlice_int_arg-12                                                      3.095µ ± 36%             1.157µ ±   8%  -62.62% (p=0.001 n=7)    1.519µ ± 13%   -50.92% (p=0.001 n=7)
TextloggerOutput/String()_that_panics-12                                                   3.339µ ± 19%             1.686µ ±  28%  -49.51% (p=0.001 n=7)    4.261µ ± 13%   +27.61% (p=0.002 n=7)
TextloggerOutput/mixed_duplicates-12                                                      19.123µ ±  8%             7.769µ ±   3%  -59.37% (p=0.001 n=7)   23.587µ ± 30%         ~ (p=0.165 n=7)
TextloggerOutput/vmodule-12                                                                3.586µ ± 21%             2.029µ ±  13%  -43.42% (p=0.001 n=7)    3.397µ ± 25%         ~ (p=0.318 n=7)
TextloggerOutput/call_depth-12                                                             3.193µ ± 21%             1.108µ ±   6%  -65.30% (p=0.001 n=7)    1.630µ ± 54%   -48.95% (p=0.001 n=7)
TextloggerOutput/Error()_for_nil-12                                                       13.133µ ± 14%             4.342µ ±  75%  -66.94% (p=0.001 n=7)   13.181µ ±  9%         ~ (p=0.902 n=7)
TextloggerOutput/Error()_that_panics-12                                                    4.799µ ± 16%             1.784µ ±  19%  -62.83% (p=0.001 n=7)    4.000µ ± 18%   -16.65% (p=0.004 n=7)
TextloggerOutput/MarshalLog()_that_panics-12                                               4.676µ ±  7%             1.645µ ±  14%  -64.82% (p=0.001 n=7)    3.293µ ± 33%   -29.58% (p=0.001 n=7)
TextloggerOutput/other_vmodule-12                                                          839.6n ± 10%             305.7n ±   6%  -63.59% (p=0.001 n=7)    689.1n ± 34%   -17.93% (p=0.026 n=7)
TextloggerOutput/KObjSlice_nil_entry-12                                                    2.959µ ±  5%             1.097µ ±  16%  -62.93% (p=0.001 n=7)    2.809µ ± 15%    -5.07% (p=0.011 n=7)
TextloggerOutput/ignore_MarshalJSON-12                                                     3.054µ ±  7%             1.037µ ±  52%  -66.04% (p=0.001 n=7)    2.669µ ± 19%         ~ (p=0.259 n=7)
TextloggerOutput/struct_values-12                                                          3.424µ ±  9%             1.306µ ±   6%  -61.86% (p=0.001 n=7)    2.957µ ± 21%   -13.64% (p=0.004 n=7)
TextloggerOutput/log_with_name_and_values-12                                               3.426µ ± 16%             1.243µ ±  28%  -63.72% (p=0.001 n=7)    3.177µ ± 21%         ~ (p=0.318 n=7)
TextloggerOutput/handle_odd-numbers_of_KVs-12                                              3.065µ ±  5%             1.167µ ±  14%  -61.92% (p=0.001 n=7)    3.471µ ± 14%   +13.25% (p=0.001 n=7)
TextloggerOutput/regular_error_types_as_value-12                                           2.539µ ± 18%             1.005µ ± 122%  -60.42% (p=0.001 n=7)    2.679µ ± 15%         ~ (p=0.383 n=7)
TextloggerOutput/KObjSlice_nil_arg-12                                                     2213.0n ± 16%             977.2n ±  29%  -55.84% (p=0.001 n=7)   1318.0n ± 15%   -40.44% (p=0.001 n=7)
TextloggerOutput/odd_WithValues-12                                                         9.966µ ± 22%             3.563µ ±  14%  -64.25% (p=0.001 n=7)   10.910µ ± 27%         ~ (p=0.710 n=7)
TextloggerOutput/multiple_WithValues-12                                                   10.511µ ±  7%             5.030µ ±  11%  -52.15% (p=0.001 n=7)   13.295µ ±  8%   +26.49% (p=0.001 n=7)
TextloggerOutput/quotation-12                                                              2.925µ ± 19%             1.025µ ±   7%  -64.96% (p=0.001 n=7)    2.426µ ± 46%         ~ (p=0.053 n=7)
TextloggerOutput/preserve_order_of_key/value_pairs-12                                      4.650µ ± 15%             1.359µ ±  24%  -70.77% (p=0.001 n=7)    3.970µ ± 17%   -14.62% (p=0.017 n=7)
TextloggerOutput/KObjSlice_okay-12                                                         2.732µ ± 20%             1.175µ ±  12%  -56.99% (p=0.001 n=7)    1.724µ ± 32%   -36.90% (p=0.001 n=7)
TextloggerOutput/KObjSlice_ints-12                                                         3.198µ ± 23%             1.052µ ±  14%  -67.10% (p=0.001 n=7)    1.810µ ± 29%   -43.40% (p=0.001 n=7)
TextloggerOutput/String()_for_nil-12                                                      13.027µ ± 15%             4.376µ ±   8%  -66.41% (p=0.001 n=7)   12.669µ ± 14%         ~ (p=0.805 n=7)
TextloggerOutput/MarshalLog()_that_returns_itself-12                                       3.234µ ± 22%             1.144µ ±  54%  -64.63% (p=0.001 n=7)    1.881µ ± 36%   -41.84% (p=0.001 n=7)
TextloggerOutput/log_with_values-12                                                        2.682µ ± 19%             1.003µ ±  12%  -62.60% (p=0.001 n=7)    1.358µ ± 35%   -49.37% (p=0.001 n=7)
TextloggerOutput/override_single_value-12                                                  3.411µ ± 20%             1.258µ ±  51%  -63.12% (p=0.001 n=7)    3.768µ ± 24%         ~ (p=0.128 n=7)
TextloggerOutput/override_WithValues-12                                                    4.605µ ± 19%             1.479µ ±  14%  -67.88% (p=0.001 n=7)    2.610µ ± 26%   -43.32% (p=0.001 n=7)
TextloggerOutput/handle_integer_keys-12                                                    4.969µ ± 23%             1.637µ ±  12%  -67.06% (p=0.001 n=7)    4.724µ ± 43%         ~ (p=0.259 n=7)
TextloggerOutput/duplicates-12                                                            20.498µ ± 15%             7.045µ ±  13%  -65.63% (p=0.001 n=7)   21.466µ ± 24%         ~ (p=0.805 n=7)
TextloggerOutput/struct_keys-12                                                            4.376µ ± 23%             1.360µ ±  10%  -68.92% (p=0.001 n=7)    4.143µ ± 10%         ~ (p=0.383 n=7)
TextloggerOutput/slice_values-12                                                           3.882µ ± 13%             1.374µ ±  48%  -64.61% (p=0.001 n=7)    3.665µ ±  7%         ~ (p=0.259 n=7)
TextloggerOutput/verbosity_disabled-12                                                    166.40n ± 26%             76.25n ±  11%  -54.18% (p=0.001 n=7)   175.60n ± 14%         ~ (p=0.805 n=7)
TextloggerOutput/log_with_multiple_names_and_values-12                                     4.339µ ± 20%             1.337µ ±   9%  -69.19% (p=0.001 n=7)    3.575µ ± 38%         ~ (p=0.209 n=7)
TextloggerOutput/empty_WithValues-12                                                      2580.0n ± 15%             968.0n ±  30%  -62.48% (p=0.001 n=7)   2478.0n ± 14%         ~ (p=0.318 n=7)
TextloggerOutput/MarshalLog()_for_nil-12                                                   4.626µ ± 25%             1.817µ ±  18%  -60.72% (p=0.001 n=7)    4.765µ ±  6%         ~ (p=0.128 n=7)
TextloggerOutput/klog.Format-12                                                            6.059µ ± 25%             2.130µ ±   8%  -64.85% (p=0.001 n=7)    4.186µ ± 31%   -30.91% (p=0.004 n=7)
TextloggerOutput/verbosity_enabled-12                                                     2263.0n ± 59%             814.8n ±  11%  -63.99% (p=0.001 n=7)   2482.0n ± 22%         ~ (p=0.165 n=7)
TextloggerOutput/KObj-12                                                                   978.3n ±  8%             993.6n ±   9%        ~ (p=0.805 n=7)   1439.0n ± 16%   +47.09% (p=0.001 n=7)
TextloggerOutput/regular_error_types_when_using_logr.Error-12                              1.064µ ± 11%             1.065µ ±  12%        ~ (p=1.000 n=7)    2.894µ ±  8%  +171.99% (p=0.001 n=7)
TextloggerOutput/map_values-12                                                             2.217µ ± 20%             2.443µ ±  17%        ~ (p=0.805 n=7)    3.204µ ± 37%   +44.52% (p=0.001 n=7)
TextloggerOutput/cyclic_list-12                                                            189.2µ ± 44%             147.6µ ±  28%        ~ (p=0.053 n=7)    218.1µ ± 35%   +15.28% (p=0.026 n=7)
TextloggerOutput/print_duplicate_keys_in_arguments-12                                      1.517µ ± 26%             1.253µ ±  28%  -17.40% (p=0.024 n=7)    3.463µ ± 21%  +128.28% (p=0.001 n=7)
TextloggerOutput/html_characters-12                                                        1.089µ ± 80%             1.020µ ±  13%        ~ (p=0.128 n=7)    1.235µ ± 20%         ~ (p=0.209 n=7)
TextloggerOutput/map_keys-12                                                               1.796µ ± 26%             1.739µ ±  16%        ~ (p=0.209 n=7)    2.616µ ± 24%   +45.66% (p=0.007 n=7)
geomean                                                                                    3.651µ                   1.563µ         -57.18%                  3.420µ          -6.32%

                                                                               │ benchmark_out_main.txt │ benchmark_out_reduce-allocations-fix-alloc.txt │ benchmark_out_reduce-allocations.txt  │
                                                                               │          B/op          │         B/op           vs base                 │     B/op      vs base                 │
TextloggerOutput/handle_odd-numbers_of_KVs_in_both_log_values_and_Info_args-12               424.0 ± 0%              360.0 ± 0%  -15.09% (p=0.001 n=7)       360.0 ± 0%  -15.09% (p=0.001 n=7)
TextloggerOutput/KObjs-12                                                                    296.0 ± 0%              296.0 ± 0%        ~ (p=1.000 n=7) ¹     296.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/KObjSlice_int_arg-12                                                        296.0 ± 0%              296.0 ± 0%        ~ (p=1.000 n=7) ¹     296.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/String()_that_panics-12                                                     376.0 ± 0%              328.0 ± 0%  -12.77% (p=0.001 n=7)       328.0 ± 0%  -12.77% (p=0.001 n=7)
TextloggerOutput/mixed_duplicates-12                                                       3.081Ki ± 0%            2.377Ki ± 0%  -22.85% (p=0.001 n=7)     2.378Ki ± 0%  -22.82% (p=0.001 n=7)
TextloggerOutput/vmodule-12                                                                  296.0 ± 0%              296.0 ± 0%        ~ (p=1.000 n=7) ¹     296.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/call_depth-12                                                               376.0 ± 0%              360.0 ± 0%   -4.26% (p=0.001 n=7)       360.0 ± 0%   -4.26% (p=0.001 n=7)
TextloggerOutput/Error()_for_nil-12                                                          712.0 ± 0%              520.0 ± 0%  -26.97% (p=0.001 n=7)       520.0 ± 0%  -26.97% (p=0.001 n=7)
TextloggerOutput/Error()_that_panics-12                                                      376.0 ± 0%              328.0 ± 0%  -12.77% (p=0.001 n=7)       328.0 ± 0%  -12.77% (p=0.001 n=7)
TextloggerOutput/MarshalLog()_that_panics-12                                                 392.0 ± 0%              344.0 ± 0%  -12.24% (p=0.001 n=7)       344.0 ± 0%  -12.24% (p=0.001 n=7)
TextloggerOutput/other_vmodule-12                                                            64.00 ± 0%              64.00 ± 0%        ~ (p=1.000 n=7) ¹     64.00 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/KObjSlice_nil_entry-12                                                      296.0 ± 0%              296.0 ± 0%        ~ (p=1.000 n=7) ¹     296.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/ignore_MarshalJSON-12                                                       312.0 ± 0%              296.0 ± 0%   -5.13% (p=0.001 n=7)       296.0 ± 0%   -5.13% (p=0.001 n=7)
TextloggerOutput/struct_values-12                                                            296.0 ± 0%              296.0 ± 0%        ~ (p=1.000 n=7) ¹     296.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/log_with_name_and_values-12                                                 440.0 ± 0%              408.0 ± 0%   -7.27% (p=0.001 n=7)       408.0 ± 0%   -7.27% (p=0.001 n=7)
TextloggerOutput/handle_odd-numbers_of_KVs-12                                                328.0 ± 0%              296.0 ± 0%   -9.76% (p=0.001 n=7)       296.0 ± 0%   -9.76% (p=0.001 n=7)
TextloggerOutput/regular_error_types_as_value-12                                             312.0 ± 0%              296.0 ± 0%   -5.13% (p=0.001 n=7)       296.0 ± 0%   -5.13% (p=0.001 n=7)
TextloggerOutput/KObjSlice_nil_arg-12                                                        296.0 ± 0%              296.0 ± 0%        ~ (p=1.000 n=7) ¹     296.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/odd_WithValues-12                                                          1064.0 ± 0%             1000.0 ± 0%   -6.02% (p=0.001 n=7)      1000.0 ± 0%   -6.02% (p=0.001 n=7)
TextloggerOutput/multiple_WithValues-12                                                    1.470Ki ± 0%            1.470Ki ± 0%        ~ (p=1.000 n=7) ¹   1.470Ki ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/quotation-12                                                                320.0 ± 0%              296.0 ± 0%   -7.50% (p=0.001 n=7)       296.0 ± 0%   -7.50% (p=0.001 n=7)
TextloggerOutput/preserve_order_of_key/value_pairs-12                                        472.0 ± 0%              392.0 ± 0%  -16.95% (p=0.001 n=7)       392.0 ± 0%  -16.95% (p=0.001 n=7)
TextloggerOutput/KObjSlice_okay-12                                                           296.0 ± 0%              296.0 ± 0%        ~ (p=1.000 n=7) ¹     296.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/KObjSlice_ints-12                                                           304.0 ± 0%              304.0 ± 0%        ~ (p=1.000 n=7) ¹     304.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/String()_for_nil-12                                                         712.0 ± 0%              520.0 ± 0%  -26.97% (p=0.001 n=7)       520.0 ± 0%  -26.97% (p=0.001 n=7)
TextloggerOutput/MarshalLog()_that_returns_itself-12                                         296.0 ± 0%              296.0 ± 0%        ~ (p=1.000 n=7) ¹     296.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/log_with_values-12                                                          312.0 ± 0%              296.0 ± 0%   -5.13% (p=0.001 n=7)       296.0 ± 0%   -5.13% (p=0.001 n=7)
TextloggerOutput/override_single_value-12                                                    360.0 ± 0%              328.0 ± 0%   -8.89% (p=0.001 n=7)       328.0 ± 0%   -8.89% (p=0.001 n=7)
TextloggerOutput/override_WithValues-12                                                      416.0 ± 0%              376.0 ± 0%   -9.62% (p=0.001 n=7)       376.0 ± 0%   -9.62% (p=0.001 n=7)
TextloggerOutput/handle_integer_keys-12                                                      448.0 ± 0%              392.0 ± 0%  -12.50% (p=0.001 n=7)       392.0 ± 0%  -12.50% (p=0.001 n=7)
TextloggerOutput/duplicates-12                                                             2.892Ki ± 0%            2.188Ki ± 0%  -24.32% (p=0.001 n=7)     2.188Ki ± 0%  -24.32% (p=0.001 n=7)
TextloggerOutput/struct_keys-12                                                              408.0 ± 0%              368.0 ± 0%   -9.80% (p=0.001 n=7)       368.0 ± 0%   -9.80% (p=0.001 n=7)
TextloggerOutput/slice_values-12                                                             296.0 ± 0%              296.0 ± 0%        ~ (p=1.000 n=7) ¹     296.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/verbosity_disabled-12                                                       64.00 ± 0%              64.00 ± 0%        ~ (p=1.000 n=7) ¹     64.00 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/log_with_multiple_names_and_values-12                                       568.0 ± 0%              536.0 ± 0%   -5.63% (p=0.001 n=7)       536.0 ± 0%   -5.63% (p=0.001 n=7)
TextloggerOutput/empty_WithValues-12                                                         296.0 ± 0%              296.0 ± 0%        ~ (p=1.000 n=7) ¹     296.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/MarshalLog()_for_nil-12                                                     408.0 ± 0%              408.0 ± 0%        ~ (p=1.000 n=7) ¹     408.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/klog.Format-12                                                              560.0 ± 0%              560.0 ± 0%        ~ (p=1.000 n=7) ¹     560.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/verbosity_enabled-12                                                        296.0 ± 0%              296.0 ± 0%        ~ (p=1.000 n=7) ¹     296.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/KObj-12                                                                     296.0 ± 0%              296.0 ± 0%        ~ (p=1.000 n=7) ¹     296.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/regular_error_types_when_using_logr.Error-12                                312.0 ± 0%              296.0 ± 0%   -5.13% (p=0.001 n=7)       296.0 ± 0%   -5.13% (p=0.001 n=7)
TextloggerOutput/map_values-12                                                               488.0 ± 0%              488.0 ± 0%        ~ (p=1.000 n=7) ¹     488.0 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/cyclic_list-12                                                              580.0 ± 1%              577.0 ± 1%   -0.52% (p=0.038 n=7)       581.0 ± 1%        ~ (p=0.346 n=7)
TextloggerOutput/print_duplicate_keys_in_arguments-12                                        328.0 ± 0%              296.0 ± 0%   -9.76% (p=0.001 n=7)       296.0 ± 0%   -9.76% (p=0.001 n=7)
TextloggerOutput/html_characters-12                                                          312.0 ± 0%              296.0 ± 0%   -5.13% (p=0.001 n=7)       296.0 ± 0%   -5.13% (p=0.001 n=7)
TextloggerOutput/map_keys-12                                                                 440.0 ± 0%              440.0 ± 0%        ~ (p=1.000 n=7) ¹     440.0 ± 0%        ~ (p=1.000 n=7) ¹
geomean                                                                                      397.1                   370.9        -6.60%                     370.9        -6.59%
¹ all samples are equal

                                                                               │ benchmark_out_main.txt │ benchmark_out_reduce-allocations-fix-alloc.txt │ benchmark_out_reduce-allocations.txt │
                                                                               │       allocs/op        │       allocs/op        vs base                 │  allocs/op   vs base                 │
TextloggerOutput/handle_odd-numbers_of_KVs_in_both_log_values_and_Info_args-12               8.000 ± 0%              4.000 ± 0%  -50.00% (p=0.001 n=7)      4.000 ± 0%  -50.00% (p=0.001 n=7)
TextloggerOutput/KObjs-12                                                                    3.000 ± 0%              3.000 ± 0%        ~ (p=1.000 n=7) ¹    3.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/KObjSlice_int_arg-12                                                        3.000 ± 0%              3.000 ± 0%        ~ (p=1.000 n=7) ¹    3.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/String()_that_panics-12                                                     5.000 ± 0%              4.000 ± 0%  -20.00% (p=0.001 n=7)      4.000 ± 0%  -20.00% (p=0.001 n=7)
TextloggerOutput/mixed_duplicates-12                                                         58.00 ± 0%              40.00 ± 0%  -31.03% (p=0.001 n=7)      40.00 ± 0%  -31.03% (p=0.001 n=7)
TextloggerOutput/vmodule-12                                                                  3.000 ± 0%              3.000 ± 0%        ~ (p=1.000 n=7) ¹    3.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/call_depth-12                                                               5.000 ± 0%              4.000 ± 0%  -20.00% (p=0.001 n=7)      4.000 ± 0%  -20.00% (p=0.001 n=7)
TextloggerOutput/Error()_for_nil-12                                                          8.000 ± 0%              6.000 ± 0%  -25.00% (p=0.001 n=7)      6.000 ± 0%  -25.00% (p=0.001 n=7)
TextloggerOutput/Error()_that_panics-12                                                      5.000 ± 0%              4.000 ± 0%  -20.00% (p=0.001 n=7)      4.000 ± 0%  -20.00% (p=0.001 n=7)
TextloggerOutput/MarshalLog()_that_panics-12                                                 6.000 ± 0%              5.000 ± 0%  -16.67% (p=0.001 n=7)      5.000 ± 0%  -16.67% (p=0.001 n=7)
TextloggerOutput/other_vmodule-12                                                            1.000 ± 0%              1.000 ± 0%        ~ (p=1.000 n=7) ¹    1.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/KObjSlice_nil_entry-12                                                      3.000 ± 0%              3.000 ± 0%        ~ (p=1.000 n=7) ¹    3.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/ignore_MarshalJSON-12                                                       4.000 ± 0%              3.000 ± 0%  -25.00% (p=0.001 n=7)      3.000 ± 0%  -25.00% (p=0.001 n=7)
TextloggerOutput/struct_values-12                                                            3.000 ± 0%              3.000 ± 0%        ~ (p=1.000 n=7) ¹    3.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/log_with_name_and_values-12                                                 9.000 ± 0%              6.000 ± 0%  -33.33% (p=0.001 n=7)      6.000 ± 0%  -33.33% (p=0.001 n=7)
TextloggerOutput/handle_odd-numbers_of_KVs-12                                                5.000 ± 0%              3.000 ± 0%  -40.00% (p=0.001 n=7)      3.000 ± 0%  -40.00% (p=0.001 n=7)
TextloggerOutput/regular_error_types_as_value-12                                             4.000 ± 0%              3.000 ± 0%  -25.00% (p=0.001 n=7)      3.000 ± 0%  -25.00% (p=0.001 n=7)
TextloggerOutput/KObjSlice_nil_arg-12                                                        3.000 ± 0%              3.000 ± 0%        ~ (p=1.000 n=7) ¹    3.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/odd_WithValues-12                                                           15.00 ± 0%              11.00 ± 0%  -26.67% (p=0.001 n=7)      11.00 ± 0%  -26.67% (p=0.001 n=7)
TextloggerOutput/multiple_WithValues-12                                                      16.00 ± 0%              16.00 ± 0%        ~ (p=1.000 n=7) ¹    16.00 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/quotation-12                                                                4.000 ± 0%              3.000 ± 0%  -25.00% (p=0.001 n=7)      3.000 ± 0%  -25.00% (p=0.001 n=7)
TextloggerOutput/preserve_order_of_key/value_pairs-12                                        9.000 ± 0%              4.000 ± 0%  -55.56% (p=0.001 n=7)      4.000 ± 0%  -55.56% (p=0.001 n=7)
TextloggerOutput/KObjSlice_okay-12                                                           3.000 ± 0%              3.000 ± 0%        ~ (p=1.000 n=7) ¹    3.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/KObjSlice_ints-12                                                           4.000 ± 0%              4.000 ± 0%        ~ (p=1.000 n=7) ¹    4.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/String()_for_nil-12                                                         8.000 ± 0%              6.000 ± 0%  -25.00% (p=0.001 n=7)      6.000 ± 0%  -25.00% (p=0.001 n=7)
TextloggerOutput/MarshalLog()_that_returns_itself-12                                         3.000 ± 0%              3.000 ± 0%        ~ (p=1.000 n=7) ¹    3.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/log_with_values-12                                                          4.000 ± 0%              3.000 ± 0%  -25.00% (p=0.001 n=7)      3.000 ± 0%  -25.00% (p=0.001 n=7)
TextloggerOutput/override_single_value-12                                                    6.000 ± 0%              4.000 ± 0%  -33.33% (p=0.001 n=7)      4.000 ± 0%  -33.33% (p=0.001 n=7)
TextloggerOutput/override_WithValues-12                                                     12.000 ± 0%              6.000 ± 0%  -50.00% (p=0.001 n=7)      6.000 ± 0%  -50.00% (p=0.001 n=7)
TextloggerOutput/handle_integer_keys-12                                                     10.000 ± 0%              6.000 ± 0%  -40.00% (p=0.001 n=7)      6.000 ± 0%  -40.00% (p=0.001 n=7)
TextloggerOutput/duplicates-12                                                               58.00 ± 0%              40.00 ± 0%  -31.03% (p=0.001 n=7)      40.00 ± 0%  -31.03% (p=0.001 n=7)
TextloggerOutput/struct_keys-12                                                              9.000 ± 0%              5.000 ± 0%  -44.44% (p=0.001 n=7)      5.000 ± 0%  -44.44% (p=0.001 n=7)
TextloggerOutput/slice_values-12                                                             3.000 ± 0%              3.000 ± 0%        ~ (p=1.000 n=7) ¹    3.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/verbosity_disabled-12                                                       1.000 ± 0%              1.000 ± 0%        ~ (p=1.000 n=7) ¹    1.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/log_with_multiple_names_and_values-12                                       12.00 ± 0%              10.00 ± 0%  -16.67% (p=0.001 n=7)      10.00 ± 0%  -16.67% (p=0.001 n=7)
TextloggerOutput/empty_WithValues-12                                                         3.000 ± 0%              3.000 ± 0%        ~ (p=1.000 n=7) ¹    3.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/MarshalLog()_for_nil-12                                                     5.000 ± 0%              5.000 ± 0%        ~ (p=1.000 n=7) ¹    5.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/klog.Format-12                                                              11.00 ± 0%              11.00 ± 0%        ~ (p=1.000 n=7) ¹    11.00 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/verbosity_enabled-12                                                        3.000 ± 0%              3.000 ± 0%        ~ (p=1.000 n=7) ¹    3.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/KObj-12                                                                     3.000 ± 0%              3.000 ± 0%        ~ (p=1.000 n=7) ¹    3.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/regular_error_types_when_using_logr.Error-12                                4.000 ± 0%              3.000 ± 0%  -25.00% (p=0.001 n=7)      3.000 ± 0%  -25.00% (p=0.001 n=7)
TextloggerOutput/map_values-12                                                               11.00 ± 0%              11.00 ± 0%        ~ (p=1.000 n=7) ¹    11.00 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/cyclic_list-12                                                              8.000 ± 0%              8.000 ± 0%        ~ (p=1.000 n=7) ¹    8.000 ± 0%        ~ (p=1.000 n=7) ¹
TextloggerOutput/print_duplicate_keys_in_arguments-12                                        5.000 ± 0%              3.000 ± 0%  -40.00% (p=0.001 n=7)      3.000 ± 0%  -40.00% (p=0.001 n=7)
TextloggerOutput/html_characters-12                                                          5.000 ± 0%              3.000 ± 0%  -40.00% (p=0.001 n=7)      3.000 ± 0%  -40.00% (p=0.001 n=7)
TextloggerOutput/map_keys-12                                                                10.000 ± 0%              9.000 ± 0%  -10.00% (p=0.001 n=7)      9.000 ± 0%  -10.00% (p=0.001 n=7)
geomean                                                                                      5.598                   4.520       -19.27%                    4.520       -19.27%
¹ all samples are equal

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

Please confirm that if this PR changes any image versions, then that's the sole change this PR makes.

Release note:

Reduce memory allocations by replacing `strconv.Quote` with `strconv.AppendQuote` 

Updated the logging functions to utilize cached buffers more effectively by replacing the creation of new byte slices with calls to `AvailableBuffer()`. This change reduces memory allocations and enhances performance across multiple logging components, including `klog`, `klogr_slog`, and `textlogger`.

Additionally, modified the JSON serialization in `keyvalues_slog.go` and `keyvalues.go` to leverage the same buffer optimization for quoting string values. This refactor aims to streamline logging operations and improve overall efficiency.

Signed-off-by: Anton Sergunov <[email protected]>
Signed-off-by: Anton Sergunov <[email protected]>
Signed-off-by: Anton Sergunov <[email protected]>
Signed-off-by: Anton Sergunov <[email protected]>
@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: asergunov
Once this PR has been reviewed and has the lgtm label, please assign thockin for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Nov 14, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Nov 14, 2025
@k8s-ci-robot
Copy link

This issue is currently awaiting triage.

If klog contributors determine this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot
Copy link

Welcome @asergunov!

It looks like this is your first PR to kubernetes/klog 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/klog has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Nov 14, 2025
@asergunov
Copy link
Author

Don't take in account the reported speed improvements. It could be just Laptop heating. It has less allocations for sure.

Copy link

@pohly pohly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double-checked performance on an Arrow Lake desktop with pinning to one P-core (to avoid variance). I see moderate improvements:

pkg: k8s.io/klog/v2
...
geomean                                                                  61.43n         61.20n       -0.37%

pkg: k8s.io/klog/v2/textlogger
...
geomean                                                                        586.6n        578.7n       -1.35%

But any improvement which doesn't unduly increase complexity is welcome, so thanks for the PR!

@asergunov: can you squash into one commit?

/assign @dims

Tests passed locally, workflows still need to be triggered (don't have permissions for that).

@dims
Copy link
Member

dims commented Nov 21, 2025

i've approved workflows to run @pohly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants