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

optimize MarshalTextTo #115

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

shogo82148
Copy link
Contributor

@shogo82148 shogo82148 commented Mar 1, 2024

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

@peterbourgon
Copy link
Member

peterbourgon commented Mar 2, 2024

Ha, that's pretty good.

I get even slightly better results than you did:

name               old time/op    new time/op    delta
Marshal/Text-11      9.94ns ± 0%    7.14ns ± 4%  -28.19%  (p=0.002 n=6+6)
Marshal/TextTo-11    8.86ns ± 0%    6.39ns ± 2%  -27.82%  (p=0.002 n=6+6)
String-11            10.5ns ± 0%     8.0ns ± 2%  -23.23%  (p=0.010 n=4+6)

name               old speed      new speed      delta
Marshal/Text-11    1.61GB/s ± 0%  2.24GB/s ± 4%  +39.32%  (p=0.002 n=6+6)
Marshal/TextTo-11  1.81GB/s ± 0%  2.50GB/s ± 2%  +38.58%  (p=0.002 n=6+6)
String-11          1.53GB/s ± 0%  1.99GB/s ± 2%  +30.30%  (p=0.004 n=5+6)

@peterbourgon
Copy link
Member

Same as #116, please help future maintainers (and me!) understand how this works, and why it's better that the previous implementation, with some form of documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants