Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Time.AppendFormat when possible #786

Merged
merged 3 commits into from Feb 19, 2020
Merged

Use Time.AppendFormat when possible #786

merged 3 commits into from Feb 19, 2020

Commits on Feb 13, 2020

  1. Use Time.AppendFormat when possible

    Fixes #783.
    
    We can take advantage of Time.AppendFormat by adding a new exported
    method to the JSON encoder, and upcasting when encoding time to use
    the append-based method. This avoids an allocation to convert the time
    to a string before appending to the buffer.
    
    The benchmarks use the production config, which uses a nanos encoder
    so to understand the performance difference, I tweaked
    `BenchmarkZapJSON` to use RFC3339TimeEncoder and ran benchmarks:
    
    ```
    > benchcmp -best old new
    benchmark               old ns/op     new ns/op     delta
    BenchmarkZapJSON-12     514           497           -3.31%
    
    benchmark               old allocs     new allocs     delta
    BenchmarkZapJSON-12     5              4              -20.00%
    
    benchmark               old bytes     new bytes     delta
    BenchmarkZapJSON-12     1297          1265          -2.47%
    ```
    
    I also wrote a benchmark that only logs a simple message using the
    RFC3339TimeEncoder,
    ```
    func BenchmarkTimeEncoder(b *testing.B) {
            cfg := NewProductionConfig().EncoderConfig
            cfg.EncodeTime = zapcore.RFC3339TimeEncoder
            logger := New(
                    zapcore.NewCore(
                            zapcore.NewJSONEncoder(cfg),
                            &ztest.Discarder{},
                            DebugLevel,
                    ))
            b.ResetTimer()
    
            for i := 0; i < b.N; i++ {
                    logger.Info("test")
            }
    }
    ```
    
    Results:
    ```
    > benchcmp -best old new
    benchmark                   old ns/op     new ns/op     delta
    BenchmarkTimeEncoder-12     695           571           -17.84%
    
    benchmark                   old allocs     new allocs     delta
    BenchmarkTimeEncoder-12     1              0              -100.00%
    
    benchmark                   old bytes     new bytes     delta
    BenchmarkTimeEncoder-12     32            0             -100.00%
    ```
    prashantv committed Feb 13, 2020
    Copy the full SHA
    1942475 View commit details
    Browse the repository at this point in the history
  2. Copy the full SHA
    f88859e View commit details
    Browse the repository at this point in the history

Commits on Feb 14, 2020

  1. CR feedback

    prashantv committed Feb 14, 2020
    Copy the full SHA
    ee5afed View commit details
    Browse the repository at this point in the history