Skip to content

proposal: time: add Time.AppendText #66972

Closed
@sylr

Description

@sylr

Proposal Details

Hi,

I wrote a zero allocation marshaller with mus-go for a struct that contains a lot of time.Time. Unfortunately, it still allocates because of time.MarshalBinary().

I played a bit with the standard library and implemented another version of time.MarshalBinary() ([]byte, error) that does not allocate (see: sylr@384f060).

Here are the results:

goos: darwin
goarch: arm64
pkg: utils/nats
cpu: Apple M2
                   │    old.txt    │              new5.txt               │
                   │    sec/op     │   sec/op     vs base                │
Encoders/mus/raw-8   15.259µ ± 91%   6.268µ ± 0%  -58.92% (p=0.000 n=10)

                   │   old.txt    │             new5.txt             │
                   │    bytes     │    bytes      vs base            │
Encoders/mus/raw-8   4.001Ki ± 0%   4.001Ki ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

                   │   old.txt    │               new5.txt                │
                   │     B/op     │     B/op      vs base                 │
Encoders/mus/raw-8   1.562Ki ± 0%   0.000Ki ± 0%  -100.00% (p=0.000 n=10)

                   │  old.txt   │              new5.txt              │
                   │ allocs/op  │ allocs/op  vs base                 │
Encoders/mus/raw-8   100.0 ± 0%    0.0 ± 0%  -100.00% (p=0.000 n=10)

I'm wondering if we could merge something like this, possible implementations I could think of are:

  • func(t time.Time) Read(p []byte) (int, error) (what I implemented in sylr@384f060)
  • func(t time.Time) ReadBinary(p []byte) (int, error) (same as previous but does not satisfy io.Reader to avoid implementing a known interface for a specific encoding)
  • A separate wrapper.
    • type BinaryReader struct { Time }
    • func(r BinaryReader) Read(p []byte) (int, error)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions