Skip to content

Commit 1303205

Browse files
authored
Merge pull request #1934 from saschagrunert/optimize/json-buffer-pooling
Add buffer pooling for JSON operations
2 parents f7a4d7c + c477e55 commit 1303205

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

cmd/crictl/bufferpool.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"bytes"
21+
"sync"
22+
)
23+
24+
// jsonBufferPool is a pool of bytes.Buffer instances used for JSON operations.
25+
// This reduces GC pressure by reusing buffers instead of allocating new ones
26+
// for each JSON indentation operation.
27+
var jsonBufferPool = sync.Pool{
28+
New: func() interface{} {
29+
return new(bytes.Buffer)
30+
},
31+
}
32+
33+
// getJSONBuffer retrieves a buffer from the pool.
34+
// The caller must call putJSONBuffer when done to return it to the pool.
35+
func getJSONBuffer() *bytes.Buffer {
36+
buf, ok := jsonBufferPool.Get().(*bytes.Buffer)
37+
if !ok {
38+
return new(bytes.Buffer)
39+
}
40+
41+
return buf
42+
}
43+
44+
// putJSONBuffer resets and returns a buffer to the pool.
45+
// This should be called with defer after getJSONBuffer.
46+
func putJSONBuffer(buf *bytes.Buffer) {
47+
buf.Reset()
48+
jsonBufferPool.Put(buf)
49+
}

cmd/crictl/util.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"bytes"
2120
"context"
2221
"encoding/json"
2322
"errors"
@@ -421,8 +420,10 @@ func outputStatusData(statuses []statusData, format, tmplStr string) (err error)
421420

422421
fmt.Println(string(yamlInfo))
423422
case outputTypeJSON:
424-
var output bytes.Buffer
425-
if err := json.Indent(&output, jsonResult, "", " "); err != nil {
423+
output := getJSONBuffer()
424+
defer putJSONBuffer(output)
425+
426+
if err := json.Indent(output, jsonResult, "", " "); err != nil {
426427
return fmt.Errorf("indent JSON result: %w", err)
427428
}
428429

@@ -519,9 +520,10 @@ func marshalMapInOrder(m map[string]any, t any) (string, error) {
519520

520521
sb.WriteString("}")
521522

522-
var buf bytes.Buffer
523+
buf := getJSONBuffer()
524+
defer putJSONBuffer(buf)
523525

524-
if err := json.Indent(&buf, []byte(sb.String()), "", " "); err != nil {
526+
if err := json.Indent(buf, []byte(sb.String()), "", " "); err != nil {
525527
return "", err
526528
}
527529

0 commit comments

Comments
 (0)