ulid icon indicating copy to clipboard operation
ulid copied to clipboard

optimize MarshalTextTo

Open shogo82148 opened this issue 2 years ago • 2 comments

I optimized the MarshalTextTo process by using uint64.

Instead of directly converting ULID to a string, it is converted through a pair of uint64 variables. This change allows us to reduce the number of bit operations, leading to faster performance.

The result of benchmark is here:

goos: darwin
goarch: arm64
pkg: github.com/oklog/ulid/v2
                  │   .old.txt   │              .new.txt               │
                  │    sec/op    │   sec/op     vs base                │
String-10            13.24n ± 0%   10.33n ± 0%  -22.02% (p=0.000 n=10)
Marshal/Text-10     12.570n ± 0%   9.833n ± 2%  -21.78% (p=0.000 n=10)
Marshal/TextTo-10   10.630n ± 1%   7.909n ± 0%  -25.59% (p=0.000 n=10)
geomean              12.09n        9.295n       -23.15%

                  │   .old.txt   │               .new.txt               │
                  │     B/s      │     B/s       vs base                │
String-10           1.126Gi ± 0%   1.443Gi ± 0%  +28.17% (p=0.000 n=10)
Marshal/Text-10     1.185Gi ± 0%   1.516Gi ± 2%  +27.85% (p=0.000 n=10)
Marshal/TextTo-10   1.402Gi ± 1%   1.884Gi ± 0%  +34.35% (p=0.000 n=10)
geomean             1.232Gi        1.603Gi       +30.09%

                  │   .old.txt   │              .new.txt               │
                  │     B/op     │    B/op     vs base                 │
String-10           0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Text-10     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/TextTo-10   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                        ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                  │   .old.txt   │              .new.txt               │
                  │  allocs/op   │ allocs/op   vs base                 │
String-10           0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Text-10     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/TextTo-10   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                        ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

shogo82148 avatar Mar 01 '24 13:03 shogo82148