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

AST Tweaks #551

Merged
merged 7 commits into from
Jun 4, 2021
Merged

AST Tweaks #551

merged 7 commits into from
Jun 4, 2021

Conversation

pelletier
Copy link
Owner

@pelletier pelletier commented Jun 3, 2021

  • Pass around ast.Node as pointers, not copies.
  • Add new []byte to ast.Node to have both the raw input (for error reporting) and the actual parsed value (for strings with escaped characters).
  • Replace ast.Reference from a struct to a single int to save space.

name                             old time/op    new time/op    delta
UnmarshalDataset/config            28.6ms ± 1%    26.4ms ± 1%   -7.66%  (p=0.000 n=9+10)
UnmarshalDataset/canada             114ms ± 0%     101ms ± 1%  -11.42%  (p=0.000 n=10+9)
UnmarshalDataset/citm_catalog      38.5ms ± 2%    31.0ms ± 1%  -19.62%  (p=0.000 n=10+10)
UnmarshalDataset/twitter           14.6ms ± 1%    11.9ms ± 0%  -18.50%  (p=0.000 n=10+10)
UnmarshalDataset/code               127ms ± 1%     117ms ± 1%   -7.73%  (p=0.000 n=9+10)
UnmarshalDataset/example            193µs ± 0%     173µs ± 0%  -10.25%  (p=0.000 n=10+10)
Unmarshal/SimpleDocument/struct     707ns ± 2%     576ns ± 0%  -18.57%  (p=0.000 n=10+10)
Unmarshal/SimpleDocument/map       1.00µs ± 2%    0.87µs ± 0%  -12.87%  (p=0.000 n=10+9)
Unmarshal/ReferenceFile/struct     48.8µs ± 3%    42.1µs ± 0%  -13.55%  (p=0.000 n=10+10)
Unmarshal/ReferenceFile/map        77.5µs ± 0%    72.5µs ± 0%   -6.51%  (p=0.000 n=9+9)
Unmarshal/HugoFrontMatter          15.9µs ± 0%    14.6µs ± 0%   -7.71%  (p=0.000 n=8+10)

name                             old speed      new speed      delta
UnmarshalDataset/config          36.7MB/s ± 1%  39.8MB/s ± 1%   +8.28%  (p=0.000 n=9+10)
UnmarshalDataset/canada          19.3MB/s ± 0%  21.8MB/s ± 1%  +12.70%  (p=0.000 n=10+10)
UnmarshalDataset/citm_catalog    14.5MB/s ± 2%  18.0MB/s ± 1%  +24.42%  (p=0.000 n=10+10)
UnmarshalDataset/twitter         30.2MB/s ± 1%  37.0MB/s ± 0%  +22.70%  (p=0.000 n=10+10)
UnmarshalDataset/code            21.2MB/s ± 1%  23.0MB/s ± 1%   +8.38%  (p=0.000 n=9+10)
UnmarshalDataset/example         41.9MB/s ± 0%  46.7MB/s ± 0%  +11.42%  (p=0.000 n=10+10)
Unmarshal/SimpleDocument/struct  15.6MB/s ± 2%  19.1MB/s ± 0%  +22.78%  (p=0.000 n=10+10)
Unmarshal/SimpleDocument/map     11.0MB/s ± 2%  12.6MB/s ± 0%  +14.75%  (p=0.000 n=10+9)
Unmarshal/ReferenceFile/struct    108MB/s ± 3%   124MB/s ± 0%  +15.65%  (p=0.000 n=10+10)
Unmarshal/ReferenceFile/map      67.6MB/s ± 0%  72.3MB/s ± 0%   +6.96%  (p=0.000 n=9+9)
Unmarshal/HugoFrontMatter        34.4MB/s ± 0%  37.3MB/s ± 0%   +8.36%  (p=0.000 n=8+10)

name                             old alloc/op   new alloc/op   delta
UnmarshalDataset/config            5.91MB ± 0%    5.91MB ± 0%     ~     (p=0.054 n=10+10)
UnmarshalDataset/canada            84.4MB ± 0%    84.4MB ± 0%   -0.00%  (p=0.001 n=8+9)
UnmarshalDataset/citm_catalog      35.6MB ± 0%    35.6MB ± 0%     ~     (p=1.000 n=10+10)
UnmarshalDataset/twitter           13.5MB ± 0%    13.5MB ± 0%     ~     (p=0.684 n=10+10)
UnmarshalDataset/code              22.2MB ± 0%    22.2MB ± 0%   -0.00%  (p=0.000 n=10+10)
UnmarshalDataset/example            193kB ± 0%     193kB ± 0%   -0.01%  (p=0.001 n=10+10)
Unmarshal/SimpleDocument/struct      597B ± 0%      581B ± 0%   -2.68%  (p=0.000 n=10+10)
Unmarshal/SimpleDocument/map         973B ± 0%      957B ± 0%   -1.64%  (p=0.000 n=10+10)
Unmarshal/ReferenceFile/struct     11.6kB ± 0%    11.6kB ± 0%   -0.14%  (p=0.000 n=10+10)
Unmarshal/ReferenceFile/map        28.9kB ± 0%    28.9kB ± 0%   -0.06%  (p=0.000 n=9+9)
Unmarshal/HugoFrontMatter          7.39kB ± 0%    7.38kB ± 0%   -0.22%  (p=0.000 n=9+10)

name                             old allocs/op  new allocs/op  delta
UnmarshalDataset/config              233k ± 0%      233k ± 0%     ~     (p=0.137 n=10+8)
UnmarshalDataset/canada              782k ± 0%      782k ± 0%     ~     (all equal)
UnmarshalDataset/citm_catalog        192k ± 0%      192k ± 0%     ~     (p=0.650 n=10+10)
UnmarshalDataset/twitter            56.9k ± 0%     56.9k ± 0%     ~     (p=0.418 n=10+10)
UnmarshalDataset/code               1.06M ± 0%     1.06M ± 0%     ~     (all equal)
UnmarshalDataset/example            1.36k ± 0%     1.36k ± 0%     ~     (all equal)
Unmarshal/SimpleDocument/struct      7.00 ± 0%      7.00 ± 0%     ~     (all equal)
Unmarshal/SimpleDocument/map         12.0 ± 0%      12.0 ± 0%     ~     (all equal)
Unmarshal/ReferenceFile/struct        182 ± 0%       182 ± 0%     ~     (all equal)
Unmarshal/ReferenceFile/map           649 ± 0%       649 ± 0%     ~     (all equal)
Unmarshal/HugoFrontMatter             143 ± 0%       143 ± 0%     ~     (all equal)

Node is a 56B struct that is constantly in the hot path. Passing nodes
around by copy had a cost that started to add up. This change replaces
them by pointers. Using unsafe pointer arithmetic and converting
sibling/child indexes to relative offsets, it removes the need to carry
around a pointer to the root of the tree. This saves 8B per Node. This
space will be used to store an extra []byte slice to provide contextual
error handling on all nodes, including the ones whose data is different
than the raw input (for example: strings with escaped characters), while
staying under the size of a cache line.

---

```
name                             old time/op    new time/op    delta
UnmarshalDataset/config            28.6ms ± 1%    26.0ms ± 1%   -9.03%  (p=0.000 n=9+10)
UnmarshalDataset/canada             114ms ± 0%     100ms ± 2%  -12.23%  (p=0.000 n=10+10)
UnmarshalDataset/citm_catalog      38.5ms ± 2%    30.1ms ± 0%  -21.87%  (p=0.000 n=10+10)
UnmarshalDataset/twitter           14.6ms ± 1%    11.3ms ± 1%  -23.01%  (p=0.000 n=10+10)
UnmarshalDataset/code               127ms ± 1%     116ms ± 1%   -8.32%  (p=0.000 n=9+10)
UnmarshalDataset/example            193µs ± 0%     167µs ± 0%  -13.73%  (p=0.000 n=10+10)
Unmarshal/SimpleDocument/struct     707ns ± 2%     540ns ± 0%  -23.64%  (p=0.000 n=10+10)
Unmarshal/SimpleDocument/map       1.00µs ± 2%    0.86µs ± 1%  -13.49%  (p=0.000 n=10+10)
Unmarshal/ReferenceFile/struct     48.8µs ± 3%    41.2µs ± 0%  -15.51%  (p=0.000 n=10+8)
Unmarshal/ReferenceFile/map        77.5µs ± 0%    70.6µs ± 0%   -8.93%  (p=0.000 n=9+10)
Unmarshal/HugoFrontMatter          15.9µs ± 0%    14.4µs ± 0%   -9.27%  (p=0.000 n=8+10)

name                             old speed      new speed      delta
UnmarshalDataset/config          36.7MB/s ± 1%  40.4MB/s ± 1%   +9.92%  (p=0.000 n=9+10)
UnmarshalDataset/canada          19.3MB/s ± 0%  22.0MB/s ± 2%  +13.94%  (p=0.000 n=10+10)
UnmarshalDataset/citm_catalog    14.5MB/s ± 2%  18.5MB/s ± 0%  +27.98%  (p=0.000 n=10+10)
UnmarshalDataset/twitter         30.2MB/s ± 1%  39.2MB/s ± 1%  +29.90%  (p=0.000 n=10+10)
UnmarshalDataset/code            21.2MB/s ± 1%  23.1MB/s ± 1%   +9.08%  (p=0.000 n=9+10)
UnmarshalDataset/example         41.9MB/s ± 0%  48.6MB/s ± 0%  +15.91%  (p=0.000 n=10+10)
Unmarshal/SimpleDocument/struct  15.6MB/s ± 2%  20.4MB/s ± 0%  +30.99%  (p=0.000 n=10+9)
Unmarshal/SimpleDocument/map     11.0MB/s ± 2%  12.7MB/s ± 0%  +15.50%  (p=0.000 n=10+9)
Unmarshal/ReferenceFile/struct    108MB/s ± 3%   127MB/s ± 0%  +18.32%  (p=0.000 n=10+8)
Unmarshal/ReferenceFile/map      67.6MB/s ± 0%  74.3MB/s ± 0%   +9.81%  (p=0.000 n=9+10)
Unmarshal/HugoFrontMatter        34.4MB/s ± 0%  37.9MB/s ± 0%  +10.21%  (p=0.000 n=8+10)

name                             old alloc/op   new alloc/op   delta
UnmarshalDataset/config            5.91MB ± 0%    5.90MB ± 0%   -0.27%  (p=0.000 n=10+10)
UnmarshalDataset/canada            84.4MB ± 0%    73.8MB ± 0%  -12.49%  (p=0.000 n=8+8)
UnmarshalDataset/citm_catalog      35.6MB ± 0%    30.3MB ± 0%  -15.08%  (p=0.000 n=10+10)
UnmarshalDataset/twitter           13.5MB ± 0%    11.3MB ± 0%  -16.10%  (p=0.000 n=10+10)
UnmarshalDataset/code              22.2MB ± 0%    22.2MB ± 0%   -0.00%  (p=0.000 n=10+10)
UnmarshalDataset/example            193kB ± 0%     160kB ± 0%  -16.74%  (p=0.000 n=10+10)
Unmarshal/SimpleDocument/struct      597B ± 0%      533B ± 0%  -10.72%  (p=0.000 n=10+10)
Unmarshal/SimpleDocument/map         973B ± 0%      909B ± 0%   -6.58%  (p=0.000 n=10+10)
Unmarshal/ReferenceFile/struct     11.6kB ± 0%    11.3kB ± 0%   -2.21%  (p=0.000 n=10+10)
Unmarshal/ReferenceFile/map        28.9kB ± 0%    28.7kB ± 0%   -0.88%  (p=0.000 n=9+10)
Unmarshal/HugoFrontMatter          7.39kB ± 0%    7.26kB ± 0%   -1.73%  (p=0.000 n=9+10)

name                             old allocs/op  new allocs/op  delta
UnmarshalDataset/config              233k ± 0%      233k ± 0%     ~     (p=1.000 n=10+10)
UnmarshalDataset/canada              782k ± 0%      782k ± 0%     ~     (all equal)
UnmarshalDataset/citm_catalog        192k ± 0%      192k ± 0%     ~     (p=0.059 n=10+8)
UnmarshalDataset/twitter            56.9k ± 0%     56.9k ± 0%     ~     (p=0.878 n=10+10)
UnmarshalDataset/code               1.06M ± 0%     1.06M ± 0%     ~     (all equal)
UnmarshalDataset/example            1.36k ± 0%     1.36k ± 0%     ~     (all equal)
Unmarshal/SimpleDocument/struct      7.00 ± 0%      7.00 ± 0%     ~     (all equal)
Unmarshal/SimpleDocument/map         12.0 ± 0%      12.0 ± 0%     ~     (all equal)
Unmarshal/ReferenceFile/struct        182 ± 0%       182 ± 0%     ~     (all equal)
Unmarshal/ReferenceFile/map           649 ± 0%       649 ± 0%     ~     (all equal)
Unmarshal/HugoFrontMatter             143 ± 0%       143 ± 0%     ~     (all equal)
```
@pelletier pelletier merged commit 618f018 into v2 Jun 4, 2021
@pelletier pelletier deleted the ast-tweaks branch June 4, 2021 01:48
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