Skip to content

Commit

Permalink
codec: cbor: cbor: use fmtTime instead of time.Format for RFC3339Nano
Browse files Browse the repository at this point in the history
This saves on some possible allocation, at the expense of a 5-word
increase in the cborEncDriver structure.

Fixes #394
  • Loading branch information
ugorji committed Nov 28, 2023
1 parent a315d04 commit 1acedc2
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion codec/cbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ type cborEncDriver struct {
encDriverNoopContainerWriter
h *CborHandle

// scratch buffer for: encode time, numbers, etc
//
// RFC3339Nano uses 35 chars: 2006-01-02T15:04:05.999999999Z07:00
b [40]byte

e Encoder
}

Expand Down Expand Up @@ -204,7 +209,7 @@ func (e *cborEncDriver) EncodeTime(t time.Time) {
e.EncodeNil()
} else if e.h.TimeRFC3339 {
e.encUint(0, cborBaseTag)
e.encStringBytesS(cborBaseString, t.Format(time.RFC3339Nano))
e.encStringBytesS(cborBaseString, stringView(fmtTime(t, time.RFC3339Nano, e.b[:0])))
} else {
e.encUint(1, cborBaseTag)
t = t.UTC().Round(time.Microsecond)
Expand Down

0 comments on commit 1acedc2

Please sign in to comment.