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

improve performance and reduce allocations of UUID methods #96

Merged
merged 1 commit into from Jan 6, 2023

Commits on Dec 22, 2022

  1. improve performance and reduce allocations of most UUID methods

    This commit improves the performance and reduces the number of allocs of
    most UUID methods and adds a new UUID.Parse() method for parsing string
    encoded UUIDs.
    
    Parsing string encoded UUIDs is now 2x faster and no longer allocates.
    The NullUUID MarshalJSON() and UnmarshalJSON() methods have also been
    improved and no longer call out to json.Unmarshal. The UUID.Format
    method has been improved for common cases.
    
    Benchmark results:
    ```
    goos: linux
    goarch: amd64
    pkg: github.com/gofrs/uuid
    cpu: Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    
    name                        old time/op    new time/op    delta
    UnmarshalText/canonical-16    47.7ns ± 0%    23.1ns ± 1%   -51.56%  (p=0.000 n=8+10)
    UnmarshalText/urn-16          47.8ns ± 1%    23.0ns ± 1%   -51.85%  (p=0.000 n=10+10)
    UnmarshalText/braced-16       47.8ns ± 1%    23.1ns ± 1%   -51.66%  (p=0.000 n=9+10)
    ParseV4-16                    86.5ns ±10%    23.0ns ± 5%   -73.44%  (p=0.000 n=9+10)
    NullMarshalJSON/Valid-16       308ns ±21%      64ns ±23%   -79.18%  (p=0.000 n=10+10)
    NullMarshalJSON/Invalid-16    41.8ns ± 2%     1.5ns ± 4%   -96.51%  (p=0.000 n=10+10)
    Format/s-16                    151ns ± 3%     132ns ± 3%   -12.65%  (p=0.000 n=10+10)
    Format/S-16                    305ns ± 2%     157ns ± 4%   -48.70%  (p=0.000 n=10+10)
    Format/q-16                    217ns ±12%     139ns ± 6%   -35.71%  (p=0.000 n=10+10)
    Format/x-16                    170ns ±13%     120ns ± 2%   -29.41%  (p=0.000 n=10+10)
    Format/X-16                    324ns ±11%     142ns ± 0%   -56.24%  (p=0.000 n=10+10)
    Format/v-16                    156ns ± 4%     143ns ± 9%    -8.51%  (p=0.000 n=10+10)
    Format/+v-16                   155ns ± 3%     141ns ± 8%    -9.11%  (p=0.000 n=10+10)
    Format/#v-16                   894ns ± 1%     860ns ± 0%    -3.84%  (p=0.000 n=10+10)
    String-16                     70.1ns ±36%    71.9ns ±22%      ~     (p=0.393 n=10+10)
    FromBytes-16                  1.81ns ± 0%    1.82ns ± 2%      ~     (p=0.369 n=8+10)
    FromString/canonical-16       94.3ns ±20%    22.1ns ± 0%   -76.53%  (p=0.000 n=10+10)
    FromString/urn-16             93.7ns ±11%    22.4ns ± 0%   -76.07%  (p=0.000 n=10+10)
    FromString/braced-16          87.1ns ± 5%    22.6ns ± 0%   -74.04%  (p=0.000 n=9+10)
    MarshalBinary-16              0.20ns ± 3%    0.20ns ± 1%      ~     (p=0.515 n=10+10)
    MarshalText-16                 115ns ±25%      19ns ± 3%   -83.09%  (p=0.000 n=10+9)
    
    name                        old alloc/op   new alloc/op   delta
    UnmarshalText/canonical-16     0.00B          0.00B           ~     (all equal)
    UnmarshalText/urn-16           0.00B          0.00B           ~     (all equal)
    UnmarshalText/braced-16        0.00B          0.00B           ~     (all equal)
    ParseV4-16                     48.0B ± 0%      0.0B       -100.00%  (p=0.000 n=10+10)
    NullMarshalJSON/Valid-16        160B ± 0%       48B ± 0%   -70.00%  (p=0.000 n=10+10)
    NullMarshalJSON/Invalid-16     8.00B ± 0%     0.00B       -100.00%  (p=0.000 n=10+10)
    Format/s-16                    48.0B ± 0%     48.0B ± 0%      ~     (all equal)
    Format/S-16                    96.0B ± 0%     48.0B ± 0%   -50.00%  (p=0.000 n=10+10)
    Format/q-16                    96.0B ± 0%     48.0B ± 0%   -50.00%  (p=0.000 n=10+10)
    Format/x-16                    64.0B ± 0%     32.0B ± 0%   -50.00%  (p=0.000 n=10+10)
    Format/X-16                     112B ± 0%       32B ± 0%   -71.43%  (p=0.000 n=10+10)
    Format/v-16                    48.0B ± 0%     48.0B ± 0%      ~     (all equal)
    Format/+v-16                   48.0B ± 0%     48.0B ± 0%      ~     (all equal)
    Format/#v-16                    128B ± 0%       16B ± 0%   -87.50%  (p=0.000 n=10+10)
    String-16                      48.0B ± 0%     48.0B ± 0%      ~     (all equal)
    FromBytes-16                   0.00B          0.00B           ~     (all equal)
    FromString/canonical-16        48.0B ± 0%      0.0B       -100.00%  (p=0.000 n=10+10)
    FromString/urn-16              48.0B ± 0%      0.0B       -100.00%  (p=0.000 n=10+10)
    FromString/braced-16           48.0B ± 0%      0.0B       -100.00%  (p=0.000 n=10+10)
    MarshalBinary-16               0.00B          0.00B           ~     (all equal)
    MarshalText-16                 96.0B ± 0%      0.0B       -100.00%  (p=0.000 n=10+10)
    
    name                        old allocs/op  new allocs/op  delta
    UnmarshalText/canonical-16      0.00           0.00           ~     (all equal)
    UnmarshalText/urn-16            0.00           0.00           ~     (all equal)
    UnmarshalText/braced-16         0.00           0.00           ~     (all equal)
    ParseV4-16                      1.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)
    NullMarshalJSON/Valid-16        4.00 ± 0%      1.00 ± 0%   -75.00%  (p=0.000 n=10+10)
    NullMarshalJSON/Invalid-16      1.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)
    Format/s-16                     1.00 ± 0%      1.00 ± 0%      ~     (all equal)
    Format/S-16                     2.00 ± 0%      1.00 ± 0%   -50.00%  (p=0.000 n=10+10)
    Format/q-16                     2.00 ± 0%      1.00 ± 0%   -50.00%  (p=0.000 n=10+10)
    Format/x-16                     2.00 ± 0%      1.00 ± 0%   -50.00%  (p=0.000 n=10+10)
    Format/X-16                     3.00 ± 0%      1.00 ± 0%   -66.67%  (p=0.000 n=10+10)
    Format/v-16                     1.00 ± 0%      1.00 ± 0%      ~     (all equal)
    Format/+v-16                    1.00 ± 0%      1.00 ± 0%      ~     (all equal)
    Format/#v-16                    2.00 ± 0%      1.00 ± 0%   -50.00%  (p=0.000 n=10+10)
    String-16                       1.00 ± 0%      1.00 ± 0%      ~     (all equal)
    FromBytes-16                    0.00           0.00           ~     (all equal)
    FromString/canonical-16         1.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)
    FromString/urn-16               1.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)
    FromString/braced-16            1.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)
    MarshalBinary-16                0.00           0.00           ~     (all equal)
    MarshalText-16                  2.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)
    ```
    charlievieth committed Dec 22, 2022
    Configuration menu
    Copy the full SHA
    2a97ddf View commit details
    Browse the repository at this point in the history