Skip to content

Commit 152f0a1

Browse files
authored
Merge pull request kubernetes#75693 from smarterclayton/speed_up_time_json
Reduce allocations in metav1.Time JSON serialization
2 parents 81d3738 + b405cdf commit 152f0a1

File tree

1 file changed

+7
-3
lines changed
  • staging/src/k8s.io/apimachinery/pkg/apis/meta/v1

1 file changed

+7
-3
lines changed

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/time.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"encoding/json"
2121
"time"
2222

23-
"github.com/google/gofuzz"
23+
fuzz "github.com/google/gofuzz"
2424
)
2525

2626
// Time is a wrapper around time.Time which supports correct
@@ -147,8 +147,12 @@ func (t Time) MarshalJSON() ([]byte, error) {
147147
// Encode unset/nil objects as JSON's "null".
148148
return []byte("null"), nil
149149
}
150-
151-
return json.Marshal(t.UTC().Format(time.RFC3339))
150+
buf := make([]byte, 0, len(time.RFC3339)+2)
151+
buf = append(buf, '"')
152+
// time cannot contain non escapable JSON characters
153+
buf = t.UTC().AppendFormat(buf, time.RFC3339)
154+
buf = append(buf, '"')
155+
return buf, nil
152156
}
153157

154158
// OpenAPISchemaType is used by the kube-openapi generator when constructing

0 commit comments

Comments
 (0)