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

Don't use bytes.Buffer when not necessary #549

Merged
merged 1 commit into from
Jun 1, 2021
Merged

Don't use bytes.Buffer when not necessary #549

merged 1 commit into from
Jun 1, 2021

Conversation

pelletier
Copy link
Owner

@pelletier pelletier commented Jun 1, 2021

When parsing strings, they can be referenced directly from the document when they don't contain escaped characters. This avoids paying to cost of allocating (and sometimes growing) the bytes buffer unnecessarily.

The next step after this will likely to use unsafe to also avoid the copy for the string when not needed (e.g when doing lookup of existing entries in maps, or struct fields).


name                              old time/op    new time/op    delta
UnmarshalDataset/config-32          40.1ms ± 2%    31.5ms ± 2%  -21.44%  (p=0.000 n=9+10)
UnmarshalDataset/canada-32           111ms ± 2%     101ms ± 3%   -8.52%  (p=0.000 n=10+10)
UnmarshalDataset/citm_catalog-32    42.4ms ± 3%    37.9ms ± 2%  -10.61%  (p=0.000 n=9+10)
UnmarshalDataset/twitter-32         17.0ms ± 3%    13.9ms ± 2%  -18.31%  (p=0.000 n=10+9)
UnmarshalDataset/code-32             133ms ± 5%     119ms ± 2%  -10.04%  (p=0.000 n=10+9)
UnmarshalDataset/example-32          288µs ± 4%     245µs ± 4%  -15.08%  (p=0.000 n=10+10)
UnmarshalSimple/struct-32           1.10µs ± 4%    0.87µs ± 1%  -21.10%  (p=0.000 n=10+9)
UnmarshalSimple/map-32              1.53µs ± 3%    1.26µs ± 3%  -17.84%  (p=0.000 n=9+10)
ReferenceFile/struct-32             66.6µs ± 5%    55.9µs ± 5%  -16.06%  (p=0.000 n=10+10)
ReferenceFile/map-32                 110µs ± 4%      92µs ± 4%  -16.67%  (p=0.000 n=10+10)
HugoFrontMatter-32                  25.3µs ± 6%    19.0µs ± 3%  -24.73%  (p=0.000 n=9+10)

name                              old speed      new speed      delta
UnmarshalDataset/config-32        26.2MB/s ± 2%  33.3MB/s ± 2%  +27.31%  (p=0.000 n=9+10)
UnmarshalDataset/canada-32        19.9MB/s ± 2%  21.8MB/s ± 3%   +9.33%  (p=0.000 n=10+10)
UnmarshalDataset/citm_catalog-32  13.2MB/s ± 3%  14.7MB/s ± 2%  +11.87%  (p=0.000 n=9+10)
UnmarshalDataset/twitter-32       25.9MB/s ± 3%  31.7MB/s ± 2%  +22.41%  (p=0.000 n=10+9)
UnmarshalDataset/code-32          20.2MB/s ± 5%  22.5MB/s ± 2%  +11.09%  (p=0.000 n=10+9)
UnmarshalDataset/example-32       28.1MB/s ± 4%  33.1MB/s ± 4%  +17.75%  (p=0.000 n=10+10)
UnmarshalSimple/struct-32         10.0MB/s ± 4%  12.6MB/s ± 1%  +26.71%  (p=0.000 n=10+9)
UnmarshalSimple/map-32            7.17MB/s ± 3%  8.73MB/s ± 3%  +21.73%  (p=0.000 n=9+10)
ReferenceFile/struct-32           78.8MB/s ± 5%  93.9MB/s ± 5%  +19.16%  (p=0.000 n=10+10)
ReferenceFile/map-32              47.5MB/s ± 4%  57.0MB/s ± 4%  +19.97%  (p=0.000 n=10+10)
HugoFrontMatter-32                21.6MB/s ± 6%  28.7MB/s ± 3%  +32.81%  (p=0.000 n=9+10)

name                              old alloc/op   new alloc/op   delta
UnmarshalDataset/config-32          7.60MB ± 0%    5.96MB ± 0%  -21.49%  (p=0.000 n=9+9)
UnmarshalDataset/canada-32          84.4MB ± 0%    84.4MB ± 0%   -0.00%  (p=0.000 n=9+10)
UnmarshalDataset/citm_catalog-32    35.7MB ± 0%    35.6MB ± 0%   -0.13%  (p=0.000 n=10+10)
UnmarshalDataset/twitter-32         14.0MB ± 0%    13.5MB ± 0%   -3.48%  (p=0.000 n=10+10)
UnmarshalDataset/code-32            23.2MB ± 0%    22.4MB ± 0%   -3.54%  (p=0.000 n=9+10)
UnmarshalDataset/example-32          202kB ± 0%     193kB ± 0%   -4.41%  (p=0.000 n=10+10)
UnmarshalSimple/struct-32             661B ± 0%      597B ± 0%   -9.68%  (p=0.000 n=10+10)
UnmarshalSimple/map-32              1.04kB ± 0%    0.97kB ± 0%   -6.17%  (p=0.000 n=10+10)
ReferenceFile/struct-32             13.0kB ± 0%    11.6kB ± 0%  -10.84%  (p=0.000 n=10+10)
ReferenceFile/map-32                30.4kB ± 0%    29.0kB ± 0%   -4.63%  (p=0.001 n=8+9)
HugoFrontMatter-32                  8.58kB ± 0%    7.41kB ± 0%  -13.62%  (p=0.002 n=8+10)

name                              old allocs/op  new allocs/op  delta
UnmarshalDataset/config-32            259k ± 0%      233k ± 0%   -9.84%  (p=0.000 n=8+8)
UnmarshalDataset/canada-32            782k ± 0%      782k ± 0%   -0.00%  (p=0.000 n=10+10)
UnmarshalDataset/citm_catalog-32      193k ± 0%      192k ± 0%   -0.38%  (p=0.000 n=8+10)
UnmarshalDataset/twitter-32          62.1k ± 0%     56.9k ± 0%   -8.38%  (p=0.000 n=10+10)
UnmarshalDataset/code-32             1.08M ± 0%     1.06M ± 0%   -1.19%  (p=0.000 n=9+8)
UnmarshalDataset/example-32          1.48k ± 0%     1.36k ± 0%   -8.05%  (p=0.000 n=10+10)
UnmarshalSimple/struct-32             8.00 ± 0%      7.00 ± 0%  -12.50%  (p=0.000 n=10+10)
UnmarshalSimple/map-32                13.0 ± 0%      12.0 ± 0%   -7.69%  (p=0.000 n=10+10)
ReferenceFile/struct-32                204 ± 0%       182 ± 0%  -10.78%  (p=0.000 n=10+10)
ReferenceFile/map-32                   671 ± 0%       649 ± 0%   -3.28%  (p=0.000 n=10+10)
HugoFrontMatter-32                     160 ± 0%       143 ± 0%  -10.62%  (p=0.000 n=10+10)

When parsing strings, they can be referenced directly from the document
when they don't contain escaped characters. This avoids paying to cost
of allocating (and sometimes growing) the bytes buffer unecessarily.
@pelletier pelletier merged commit b0d6c62 into v2 Jun 1, 2021
@pelletier pelletier deleted the builder-alloc branch June 1, 2021 13:52
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.

1 participant