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 the performance of deserializing types containing options #568

Merged
merged 5 commits into from
Aug 21, 2024

Conversation

frankdavid
Copy link
Contributor

The recoverable_visit_some method needs to clone the Deserializer in order to backtrack in case of errors. This cloning is a very expensive operation which this PR optimizes.

  1. Wrap the TypeEnv in an Rc. Since the TypeEnv is a heavy object and is barely updated after the Deserializer is constructed, it makes sense to keep a single instance of it so cloning becomes cheap.
  2. Make the happy path in recoverable_visit_some as cheap as possible by recursing on self instead of the clone. In case of an error, we reset the state from the clone.

The recoverable_visit_some method needs to clone the Deserializer in order to backtrack in case of errors. This cloning is a very expensive operation which this PR optimizes.

1. Wrap the TypeEnv in an Rc. Since the TypeEnv is a heavy object and is barely updated after the Deserializer is constructed, it makes sense to keep a single instance of it so cloning becomes cheap.
2. Make the happy path in recoverable_visit_some as cheap as possible by recursing on self instead of the clone. In case of an error, we reset the state from the clone.
Copy link

Name Max Mem (Kb) Encode Decode
blob 4_224 20_459_319 ($\textcolor{green}{-0.00\%}$) 12_083_680 ($\textcolor{red}{0.00\%}$)
btreemap 75_456 4_022_699_205 ($\textcolor{green}{-0.00\%}$) 15_670_422_014 ($\textcolor{green}{-0.25\%}$)
nns 128 ($\textcolor{green}{-33.33\%}$) 2_257_486 ($\textcolor{green}{-0.70\%}$) 5_498_887 ($\textcolor{green}{-56.48\%}$)
nns_list_proposal 1_792 7_056_677 ($\textcolor{green}{-0.14\%}$) 85_046_827 ($\textcolor{green}{-54.67\%}$)
option_list 128 ($\textcolor{green}{-77.78\%}$) 7_140_179 ($\textcolor{green}{-0.01\%}$) 25_749_409 ($\textcolor{green}{-20.42\%}$)
text 6_336 20_455_442 17_839_559 ($\textcolor{red}{0.00\%}$)
variant_list 128 7_142_188 ($\textcolor{green}{-0.01\%}$) 24_354_404 ($\textcolor{green}{-1.03\%}$)
vec_int16 16_704 168_582_443 ($\textcolor{green}{-0.00\%}$) 1_073_771_478 ($\textcolor{red}{0.00\%}$)
  • Parser cost: 18_786_809 ($\textcolor{green}{-0.00\%}$)
  • Extra args: 3_240_083 ($\textcolor{green}{-0.43\%}$)
Click to see raw report

---------------------------------------------------

Benchmark: blob
  total:
    instructions: 32.55 M (0.00%) (change within noise threshold)
    heap_increase: 66 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 20.46 M (-0.00%) (change within noise threshold)
    heap_increase: 66 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 12.08 M (0.00%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: text
  total:
    instructions: 38.30 M (0.00%) (change within noise threshold)
    heap_increase: 99 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 20.46 M (no change)
    heap_increase: 66 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 17.84 M (0.00%) (change within noise threshold)
    heap_increase: 33 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: vec_int16
  total:
    instructions: 1.24 B (0.00%) (change within noise threshold)
    heap_increase: 261 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 168.58 M (-0.00%) (change within noise threshold)
    heap_increase: 261 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 1.07 B (0.00%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: btreemap
  total:
    instructions: 19.69 B (-0.20%) (change within noise threshold)
    heap_increase: 1179 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 4.02 B (-0.00%) (change within noise threshold)
    heap_increase: 159 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 15.67 B (-0.25%) (change within noise threshold)
    heap_increase: 1020 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: option_list
  total:
    instructions: 32.89 M (improved by 16.73%)
    heap_increase: 2 pages (improved by 77.78%)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 7.14 M (-0.01%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 25.75 M (improved by 20.42%)
    heap_increase: 2 pages (improved by 77.78%)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: variant_list
  total:
    instructions: 31.50 M (-0.80%) (change within noise threshold)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 7.14 M (-0.01%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 24.35 M (-1.03%) (change within noise threshold)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: nns
  total:
    instructions: 27.38 M (improved by 20.72%)
    heap_increase: 2 pages (improved by 33.33%)
    stable_memory_increase: 0 pages (no change)

  0. Parsing (scope):
    instructions: 18.79 M (-0.00%) (change within noise threshold)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 2.26 M (-0.70%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 5.50 M (improved by 56.48%)
    heap_increase: 0 pages (improved by 100.00%)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: nns_list_proposal
  total:
    instructions: 92.11 M (improved by 52.69%)
    heap_increase: 28 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 7.06 M (-0.14%) (change within noise threshold)
    heap_increase: 3 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 85.05 M (improved by 54.67%)
    heap_increase: 25 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: extra_args
  total:
    instructions: 3.24 M (-0.43%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------
Successfully persisted results to canbench_results.yml

@frankdavid frankdavid marked this pull request as ready for review August 21, 2024 10:45
Copy link
Contributor

@chenyan-dfinity chenyan-dfinity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is great improvements!

@frankdavid frankdavid merged commit cfa7b54 into master Aug 21, 2024
6 checks passed
@frankdavid frankdavid deleted the frankdavid/optimize-deser-option branch August 21, 2024 16:25
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.

2 participants