Skip to content

Copy-on-write for record values - #12305

Merged
IanManske merged 10 commits into
nushell:mainfrom
devyn:value-record-as-arc
Apr 14, 2024
Merged

IanManske merged 10 commits into
nushell:mainfrom
devyn:value-record-as-arc

Conversation

@devyn

@devyn devyn commented Mar 27, 2024 •

Copy link
Copy Markdown
Contributor

Description

This adds a SharedCow type as a transparent copy-on-write pointer that clones to unique on mutate.

As an initial test, the Record within Value::Record is shared.

There are some pretty big wins for performance. I'll post benchmark results in a comment. The biggest winner is nested access, as that would have cloned the records for each cell path follow before and it doesn't have to anymore.

The reusability of the SharedCow type is nice and I think it could be used to clean up the previous work I did with Arc in EngineState. It's meant to be a mostly transparent clone-on-write that just clones on .to_mut() or .into_owned() if there are actually multiple references, but avoids cloning if the reference is unique.

User-Facing Changes

  • Value::Record field is a different type (plugin authors)

Tests + Formatting

  • 🟢 toolkit fmt
  • 🟢 toolkit clippy
  • 🟢 toolkit test
  • 🟢 toolkit test stdlib

After Submitting

  • use for EngineState
  • use for Value::List

@devyn

devyn commented Mar 27, 2024

Copy link
Copy Markdown
Contributor Author

Benchmarks: first line is before, next line is after.

benchmarks                       fastest       │ slowest       │ median        │ mean          │ samples │ iters
benchmarks                       fastest       │ slowest       │ median        │ mean          │ samples │ iters
├─ load_standard_lib             18.15 ms      │ 24.9 ms       │ 18.92 ms      │ 19.11 ms      │ 259     │ 259
├─ load_standard_lib             18.33 ms      │ 29.24 ms      │ 18.8 ms       │ 18.97 ms      │ 262     │ 262
├─ decoding_benchmarks                         │               │               │               │         │
├─ decoding_benchmarks                         │               │               │               │         │
│  ├─ json_decode                              │               │               │               │         │
│  ├─ json_decode                              │               │               │               │         │
│  │  ├─ (100, 5)                810.9 µs      │ 1.482 ms      │ 872.6 µs      │ 877.8 µs      │ 5418    │ 5418
│  │  ├─ (100, 5)                831.6 µs      │ 1.336 ms      │ 872.9 µs      │ 872.1 µs      │ 5450    │ 5450
│  │  ╰─ (10000, 15)             289.4 ms      │ 346.8 ms      │ 292.5 ms      │ 293.7 ms      │ 100     │ 100
│  │  ╰─ (10000, 15)             289.9 ms      │ 347.4 ms      │ 294 ms        │ 295.7 ms      │ 100     │ 100
│  ╰─ msgpack_decode                           │               │               │               │         │
│  ╰─ msgpack_decode                           │               │               │               │         │
│     ├─ (100, 5)                321.2 µs      │ 2.535 ms      │ 343.8 µs      │ 345.1 µs      │ 13091   │ 13091
│     ├─ (100, 5)                320.1 µs      │ 488.6 µs      │ 337.5 µs      │ 337.9 µs      │ 13255   │ 13255
│     ╰─ (10000, 15)             106.3 ms      │ 117.8 ms      │ 108 ms        │ 108.4 ms      │ 100     │ 100
│     ╰─ (10000, 15)             106.8 ms      │ 120.3 ms      │ 108 ms        │ 108.5 ms      │ 100     │ 100
├─ encoding_benchmarks                         │               │               │               │         │
├─ encoding_benchmarks                         │               │               │               │         │
│  ├─ json_encode                              │               │               │               │         │
│  ├─ json_encode                              │               │               │               │         │
│  │  ├─ (100, 5)                129.5 µs      │ 617.2 µs      │ 139.5 µs      │ 139.6 µs      │ 35764   │ 35764
│  │  ├─ (100, 5)                134.8 µs      │ 317 µs        │ 143.9 µs      │ 145.2 µs      │ 34388   │ 34388
│  │  ╰─ (10000, 15)             33.88 ms      │ 35.77 ms      │ 34.56 ms      │ 34.59 ms      │ 145     │ 145
│  │  ╰─ (10000, 15)             33.54 ms      │ 40.13 ms      │ 36.45 ms      │ 36.15 ms      │ 139     │ 139
│  ╰─ msgpack_encode                           │               │               │               │         │
│  ╰─ msgpack_encode                           │               │               │               │         │
│     ├─ (100, 5)                49.63 µs      │ 120.2 µs      │ 53.18 µs      │ 53.38 µs      │ 93380   │ 93380
│     ├─ (100, 5)                54.5 µs       │ 376.1 µs      │ 57.48 µs      │ 57.5 µs       │ 86712   │ 86712
│     ╰─ (10000, 15)             11.98 ms      │ 17.63 ms      │ 12.49 ms      │ 12.59 ms      │ 397     │ 397
│     ╰─ (10000, 15)             12.89 ms      │ 14.83 ms      │ 13.98 ms      │ 13.95 ms      │ 359     │ 359
├─ eval_benchmarks                             │               │               │               │         │
├─ eval_benchmarks                             │               │               │               │         │
│  ├─ eval_default_config        4.344 ms      │ 14.03 ms      │ 4.493 ms      │ 4.531 ms      │ 1023    │ 1023
│  ├─ eval_default_config        3.804 ms      │ 6.708 ms      │ 3.976 ms      │ 3.997 ms      │ 1159    │ 1159
│  ╰─ eval_default_env           701.1 µs      │ 1.182 ms      │ 737.8 µs      │ 739 µs        │ 5145    │ 5145
│  ╰─ eval_default_env           693.3 µs      │ 1.953 ms      │ 743.8 µs      │ 749.7 µs      │ 5074    │ 5074
├─ eval_commands                               │               │               │               │         │
├─ eval_commands                               │               │               │               │         │
│  ├─ each                                     │               │               │               │         │
│  ├─ each                                     │               │               │               │         │
│  │  ├─ 1                       104.2 µs      │ 932.8 µs      │ 137.7 µs      │ 144.2 µs      │ 28917   │ 28917
│  │  ├─ 1                       99.83 µs      │ 1.138 ms      │ 163.1 µs      │ 153 µs        │ 27782   │ 27782
│  │  ├─ 5                       150.7 µs      │ 1.272 ms      │ 380.6 µs      │ 357.2 µs      │ 12754   │ 12754
│  │  ├─ 5                       173.4 µs      │ 2.076 ms      │ 378.4 µs      │ 358.9 µs      │ 12830   │ 12830
│  │  ├─ 10                      219.5 µs      │ 2.367 ms      │ 610.4 µs      │ 543.1 µs      │ 8573    │ 8573
│  │  ├─ 10                      210.3 µs      │ 1.465 ms      │ 595.5 µs      │ 523.1 µs      │ 8899    │ 8899
│  │  ├─ 100                     340.7 µs      │ 5.872 ms      │ 1.075 ms      │ 1.926 ms      │ 2523    │ 2523
│  │  ├─ 100                     335.8 µs      │ 6.074 ms      │ 748.5 µs      │ 1.287 ms      │ 3733    │ 3733
│  │  ╰─ 1000                    1.611 ms      │ 54.17 ms      │ 8.727 ms      │ 13.26 ms      │ 375     │ 375
│  │  ╰─ 1000                    1.509 ms      │ 54.32 ms      │ 4.643 ms      │ 9.192 ms      │ 541     │ 541
│  ├─ for_range                                │               │               │               │         │
│  ├─ for_range                                │               │               │               │         │
│  │  ├─ 1                       105.4 µs      │ 1.952 ms      │ 147.5 µs      │ 145.4 µs      │ 28737   │ 28737
│  │  ├─ 1                       99.15 µs      │ 1.845 ms      │ 163.6 µs      │ 151.4 µs      │ 27986   │ 27986
│  │  ├─ 5                       206.1 µs      │ 2.902 ms      │ 349.1 µs      │ 355.2 µs      │ 12906   │ 12906
│  │  ├─ 5                       198.1 µs      │ 3.073 ms      │ 377.2 µs      │ 354.3 µs      │ 12952   │ 12952
│  │  ├─ 10                      205.1 µs      │ 2.319 ms      │ 608.2 µs      │ 552.8 µs      │ 8472    │ 8472
│  │  ├─ 10                      210.6 µs      │ 1.527 ms      │ 514.1 µs      │ 505.6 µs      │ 9161    │ 9161
│  │  ├─ 100                     317.3 µs      │ 5.625 ms      │ 2.875 ms      │ 2.716 ms      │ 1804    │ 1804
│  │  ├─ 100                     288.9 µs      │ 5.652 ms      │ 922.7 µs      │ 1.751 ms      │ 2775    │ 2775
│  │  ╰─ 1000                    1.261 ms      │ 54.22 ms      │ 18.16 ms      │ 15.41 ms      │ 323     │ 323
│  │  ╰─ 1000                    1.054 ms      │ 31.22 ms      │ 18.62 ms      │ 15.52 ms      │ 321     │ 321
│  ├─ interleave                               │               │               │               │         │
│  ├─ interleave                               │               │               │               │         │
│  │  ├─ 100                     981.2 µs      │ 2.58 ms       │ 1.382 ms      │ 1.385 ms      │ 3525    │ 3525
│  │  ├─ 100                     1.016 ms      │ 2.271 ms      │ 1.386 ms      │ 1.391 ms      │ 3517    │ 3517
│  │  ├─ 1000                    9.003 ms      │ 14.92 ms      │ 12.39 ms      │ 12.22 ms      │ 408     │ 408
│  │  ├─ 1000                    8.093 ms      │ 16.1 ms       │ 12.32 ms      │ 12.22 ms      │ 408     │ 408
│  │  ╰─ 10000                   89.55 ms      │ 130.2 ms      │ 121.6 ms      │ 119.5 ms      │ 100     │ 100
│  │  ╰─ 10000                   88.44 ms      │ 125.6 ms      │ 118.4 ms      │ 116.1 ms      │ 100     │ 100
│  ├─ interleave_with_ctrlc                    │               │               │               │         │
│  ├─ interleave_with_ctrlc                    │               │               │               │         │
│  │  ├─ 100                     944.8 µs      │ 2.52 ms       │ 1.376 ms      │ 1.368 ms      │ 3570    │ 3570
│  │  ├─ 100                     997.1 µs      │ 1.966 ms      │ 1.383 ms      │ 1.39 ms       │ 3521    │ 3521
│  │  ├─ 1000                    7.817 ms      │ 14.69 ms      │ 12.02 ms      │ 11.89 ms      │ 419     │ 419
│  │  ├─ 1000                    7.781 ms      │ 14.48 ms      │ 12.15 ms      │ 11.81 ms      │ 423     │ 423
│  │  ╰─ 10000                   66.65 ms      │ 130.3 ms      │ 117.8 ms      │ 113.3 ms      │ 100     │ 100
│  │  ╰─ 10000                   86.82 ms      │ 127.8 ms      │ 119.9 ms      │ 117.5 ms      │ 100     │ 100
│  ├─ par_each_1t                              │               │               │               │         │
│  ├─ par_each_1t                              │               │               │               │         │
│  │  ├─ 1                       157.2 µs      │ 2.236 ms      │ 211.3 µs      │ 215.6 µs      │ 20053   │ 20053
│  │  ├─ 1                       134.9 µs      │ 1.604 ms      │ 192.3 µs      │ 196.3 µs      │ 21927   │ 21927
│  │  ├─ 5                       187 µs        │ 1.433 ms      │ 433.7 µs      │ 422.2 µs      │ 10590   │ 10590
│  │  ├─ 5                       174.8 µs      │ 1.535 ms      │ 364.6 µs      │ 360.7 µs      │ 12497   │ 12497
│  │  ├─ 10                      192.8 µs      │ 1.903 ms      │ 549.3 µs      │ 556.6 µs      │ 8193    │ 8193
│  │  ├─ 10                      199.5 µs      │ 3.722 ms      │ 602.3 µs      │ 602.8 µs      │ 7641    │ 7641
│  │  ├─ 100                     438.3 µs      │ 7.537 ms      │ 3.221 ms      │ 3.183 ms      │ 1540    │ 1540
│  │  ├─ 100                     447.1 µs      │ 7.365 ms      │ 3.01 ms       │ 2.953 ms      │ 1661    │ 1661
│  │  ╰─ 1000                    2.096 ms      │ 56.14 ms      │ 9.157 ms      │ 15.89 ms      │ 314     │ 314
│  │  ╰─ 1000                    2.308 ms      │ 55.3 ms       │ 16.85 ms      │ 18 ms         │ 278     │ 278
│  ╰─ par_each_2t                              │               │               │               │         │
│  ╰─ par_each_2t                              │               │               │               │         │
│     ├─ 1                       165.1 µs      │ 979.9 µs      │ 212.5 µs      │ 219.1 µs      │ 19632   │ 19632
│     ├─ 1                       154.3 µs      │ 942 µs        │ 203.1 µs      │ 206.9 µs      │ 20730   │ 20730
│     ├─ 5                       173.7 µs      │ 1.893 ms      │ 293.8 µs      │ 294 µs        │ 15127   │ 15127
│     ├─ 5                       196 µs        │ 2.017 ms      │ 301.2 µs      │ 298.4 µs      │ 15067   │ 15067
│     ├─ 10                      194.3 µs      │ 2.825 ms      │ 355.3 µs      │ 378.2 µs      │ 11967   │ 11967
│     ├─ 10                      202.1 µs      │ 2.879 ms      │ 372.1 µs      │ 383 µs        │ 11825   │ 11825
│     ├─ 100                     314.2 µs      │ 3.699 ms      │ 1.027 ms      │ 1.506 ms      │ 3189    │ 3189
│     ├─ 100                     339.9 µs      │ 4.703 ms      │ 801.1 µs      │ 1.172 ms      │ 4086    │ 4086
│     ╰─ 1000                    1.456 ms      │ 27.89 ms      │ 4.497 ms      │ 6.798 ms      │ 729     │ 729
│     ╰─ 1000                    1.414 ms      │ 29.04 ms      │ 4.756 ms      │ 8.194 ms      │ 607     │ 607
├─ parser_benchmarks                           │               │               │               │         │
├─ parser_benchmarks                           │               │               │               │         │
│  ├─ parse_default_config_file  3.071 ms      │ 7.423 ms      │ 3.278 ms      │ 3.307 ms      │ 1472    │ 1472
│  ├─ parse_default_config_file  3.106 ms      │ 5.924 ms      │ 3.307 ms      │ 3.309 ms      │ 1472    │ 1472
│  ╰─ parse_default_env_file     561.8 µs      │ 929 µs        │ 597 µs        │ 600.2 µs      │ 8057    │ 8057
│  ╰─ parse_default_env_file     560.1 µs      │ 1.222 ms      │ 599.9 µs      │ 599.2 µs      │ 8072    │ 8072
├─ record                                      │               │               │               │         │
├─ record                                      │               │               │               │         │
│  ├─ create                                   │               │               │               │         │
│  ├─ create                                   │               │               │               │         │
│  │  ├─ 1                       48.76 µs      │ 246.2 µs      │ 52.93 µs      │ 54.36 µs      │ 60758   │ 60758
│  │  ├─ 1                       46.72 µs      │ 136.2 µs      │ 49.81 µs      │ 50.03 µs      │ 66449   │ 66449
│  │  ├─ 10                      71.14 µs      │ 295.8 µs      │ 82.93 µs      │ 83.11 µs      │ 44273   │ 44273
│  │  ├─ 10                      68.75 µs      │ 297.5 µs      │ 73.88 µs      │ 75.22 µs      │ 49331   │ 49331
│  │  ├─ 100                     292.1 µs      │ 907.2 µs      │ 311.8 µs      │ 312.5 µs      │ 14618   │ 14618
│  │  ├─ 100                     291.8 µs      │ 937.4 µs      │ 305.3 µs      │ 308.4 µs      │ 14875   │ 14875
│  │  ╰─ 1000                    2.576 ms      │ 4.03 ms       │ 2.704 ms      │ 2.715 ms      │ 1817    │ 1817
│  │  ╰─ 1000                    2.593 ms      │ 3.703 ms      │ 2.725 ms      │ 2.73 ms       │ 1808    │ 1808
│  ├─ flat_access                              │               │               │               │         │
│  ├─ flat_access                              │               │               │               │         │
│  │  ├─ 1                       25.92 µs      │ 74.36 µs      │ 27.96 µs      │ 28.89 µs      │ 87468   │ 87468
│  │  ├─ 1                       23.87 µs      │ 162 µs        │ 25.95 µs      │ 26.95 µs      │ 92086   │ 92086
│  │  ├─ 10                      27.98 µs      │ 268.1 µs      │ 33.42 µs      │ 33.57 µs      │ 79504   │ 79504
│  │  ├─ 10                      24.8 µs       │ 102.2 µs      │ 31.15 µs      │ 31.11 µs      │ 81588   │ 81588
│  │  ├─ 100                     38.88 µs      │ 219.5 µs      │ 42.45 µs      │ 43.32 µs      │ 65197   │ 65197
│  │  ├─ 100                     30.88 µs      │ 271.4 µs      │ 38.35 µs      │ 37.83 µs      │ 74237   │ 74237
│  │  ╰─ 1000                    157 µs        │ 333.7 µs      │ 189.4 µs      │ 189.9 µs      │ 20463   │ 20463
│  │  ╰─ 1000                    69.17 µs      │ 380.3 µs      │ 83.98 µs      │ 85.61 µs      │ 35284   │ 35284
│  ╰─ nest_access                              │               │               │               │         │
│  ╰─ nest_access                              │               │               │               │         │
│     ├─ 1                       25.88 µs      │ 173.6 µs      │ 31 µs         │ 30.76 µs      │ 83200   │ 83200
│     ├─ 1                       24.1 µs       │ 95.49 µs      │ 29.6 µs       │ 29.41 µs      │ 87542   │ 87542
│     ├─ 2                       28.31 µs      │ 275.2 µs      │ 33.07 µs      │ 34.2 µs       │ 83265   │ 83265
│     ├─ 2                       27.15 µs      │ 125.7 µs      │ 28.81 µs      │ 29.25 µs      │ 94740   │ 94740
│     ├─ 4                       31.13 µs      │ 179.4 µs      │ 34.41 µs      │ 35.35 µs      │ 82596   │ 82596
│     ├─ 4                       25.4 µs       │ 309.3 µs      │ 27.28 µs      │ 28.04 µs      │ 92732   │ 92732
│     ├─ 8                       36.35 µs      │ 166.9 µs      │ 39.73 µs      │ 41.39 µs      │ 69360   │ 69360
│     ├─ 8                       27.72 µs      │ 195.1 µs      │ 33.47 µs      │ 33.14 µs      │ 81488   │ 81488
│     ├─ 16                      53.38 µs      │ 128.5 µs      │ 56.46 µs      │ 56.65 µs      │ 56890   │ 56890
│     ├─ 16                      32.18 µs      │ 344.8 µs      │ 34.92 µs      │ 36.07 µs      │ 79318   │ 79318
│     ├─ 32                      109.4 µs      │ 510.6 µs      │ 116.7 µs      │ 117.7 µs      │ 32794   │ 32794
│     ├─ 32                      37.5 µs       │ 254.8 µs      │ 45.19 µs      │ 45.52 µs      │ 65333   │ 65333
│     ├─ 64                      316.8 µs      │ 813.5 µs      │ 339.4 µs      │ 348.2 µs      │ 12880   │ 12880
│     ├─ 64                      51.16 µs      │ 541.6 µs      │ 56.08 µs      │ 58.06 µs      │ 55110   │ 55110
│     ╰─ 128                     1.109 ms      │ 1.649 ms      │ 1.16 ms       │ 1.169 ms      │ 4117    │ 4117
│     ╰─ 128                     82.4 µs       │ 379.9 µs      │ 89.43 µs      │ 90.83 µs      │ 40993   │ 40993
╰─ table                                       │               │               │               │         │
╰─ table                                       │               │               │               │         │
   ├─ create                                   │               │               │               │         │
   ├─ create                                   │               │               │               │         │
   │  ├─ 1                       55.06 µs      │ 313.7 µs      │ 59.12 µs      │ 60.68 µs      │ 56388   │ 56388
   │  ├─ 1                       53.56 µs      │ 154.5 µs      │ 62.49 µs      │ 61.44 µs      │ 56116   │ 56116
   │  ├─ 10                      79.38 µs      │ 364.7 µs      │ 83.63 µs      │ 85.01 µs      │ 44530   │ 44530
   │  ├─ 10                      78.86 µs      │ 304.2 µs      │ 85.63 µs      │ 86.51 µs      │ 45327   │ 45327
   │  ├─ 100                     330.8 µs      │ 929.4 µs      │ 353.5 µs      │ 357.3 µs      │ 12944   │ 12944
   │  ├─ 100                     338.1 µs      │ 523.6 µs      │ 354.9 µs      │ 357.1 µs      │ 13040   │ 13040
   │  ╰─ 1000                    2.886 ms      │ 4.95 ms       │ 3.054 ms      │ 3.078 ms      │ 1608    │ 1608
   │  ╰─ 1000                    2.979 ms      │ 6.881 ms      │ 3.142 ms      │ 3.163 ms      │ 1567    │ 1567
   ├─ get                                      │               │               │               │         │
   ├─ get                                      │               │               │               │         │
   │  ├─ 1                       38.52 µs      │ 268.5 µs      │ 44.27 µs      │ 45.53 µs      │ 69669   │ 69669
   │  ├─ 1                       36.89 µs      │ 243.3 µs      │ 40.13 µs      │ 41.29 µs      │ 72489   │ 72489
   │  ├─ 10                      44.2 µs       │ 243.9 µs      │ 51.38 µs      │ 51.14 µs      │ 61104   │ 61104
   │  ├─ 10                      38.63 µs      │ 132.7 µs      │ 43.06 µs      │ 43.37 µs      │ 72711   │ 72711
   │  ├─ 100                     98.44 µs      │ 559.8 µs      │ 106.3 µs      │ 107.1 µs      │ 35432   │ 35432
   │  ├─ 100                     48.19 µs      │ 140.7 µs      │ 51.6 µs       │ 53.87 µs      │ 60608   │ 60608
   │  ╰─ 1000                    594.5 µs      │ 1.756 ms      │ 636.5 µs      │ 639 µs        │ 7151    │ 7151
   │  ╰─ 1000                    140.5 µs      │ 1.257 ms      │ 151.9 µs      │ 156.1 µs      │ 27174   │ 27174
   ╰─ select                                   │               │               │               │         │
   ╰─ select                                   │               │               │               │         │
      ├─ 1                       37.19 µs      │ 301 µs        │ 40.06 µs      │ 40.76 µs      │ 72201   │ 72201
      ├─ 1                       35.62 µs      │ 170.1 µs      │ 45.02 µs      │ 45.54 µs      │ 68280   │ 68280
      ├─ 10                      49.98 µs      │ 387.3 µs      │ 60.45 µs      │ 59.5 µs       │ 54649   │ 54649
      ├─ 10                      39.64 µs      │ 220.5 µs      │ 43.24 µs      │ 44.76 µs      │ 68429   │ 68429
      ├─ 100                     159.9 µs      │ 266.4 µs      │ 174 µs        │ 176.9 µs      │ 23254   │ 23254
      ├─ 100                     83.08 µs      │ 657.2 µs      │ 90.62 µs      │ 91.69 µs      │ 40448   │ 40448
      ╰─ 1000                    1.26 ms       │ 3.23 ms       │ 1.36 ms       │ 1.377 ms      │ 3468    │ 3468
      ╰─ 1000                    498.2 µs      │ 2.724 ms      │ 564.4 µs      │ 566.1 µs      │ 8276    │ 8276

@devyn

devyn commented Mar 27, 2024

Copy link
Copy Markdown
Contributor Author

In general, I feel like improving the Clone performance of values can be significant - because this is a pass-by-value language with immutable data, a lot of things just clone values without any consideration. Doing better here can have far-reaching impact.

@FilipAndersson245

Copy link
Copy Markdown
Contributor

This is very awesome, 10x on better on nested record access, Awesome work @devyn.
Reducing wasteful allocations fell like a very neat move to gain performance in some areas.

You can also check the difference in memory allocation done after this change in divan by adding these two lines to the benchmark file.

Side note:
Also I think we should maybe create some simple script to compare two benchmark outputs,
Divan is working on a format independent format but is yet there 😒

@devyn

devyn commented Mar 27, 2024 •

Copy link
Copy Markdown
Contributor Author

Thanks @FilipAndersson245!

For future reference, I did this basically:

git checkout main
cargo bench -- --min-time=5 | tee { save before.txt }
git checkout -
cargo bench -- --min-time=5 | tee { save after.txt }
open before.txt | lines | zip { open after.txt | lines } | flatten | save combined.txt

We could potentially have a script for this.

Also I use --min-time=5 to try to improve the sampling, but that might actually be more than necessary - I'll probably start going with --min-time=1 or 2 and see how it goes

@devyn

devyn commented Mar 27, 2024

Copy link
Copy Markdown
Contributor Author

@devyn
devyn force-pushed the value-record-as-arc branch from a65f14c to e341902 Compare March 27, 2024 06:37
Comment thread crates/nu-utils/src/shared.rs Resolved
@FilipAndersson245

Copy link
Copy Markdown
Contributor

Here are the mem benchmarks: https://gist.github.com/devyn/6421728d99df861e51b3d9d370ce8f7f

for the 128 nested record we have 26x fewer allocations,
roughly halfing allocations for small nested records and tables.

😎Nice

@FilipAndersson245

Copy link
Copy Markdown
Contributor

Thanks @FilipAndersson245!

For future reference, I did this basically:

git checkout main
cargo bench -- --min-time=5 | tee { save before.txt }
git checkout -
cargo bench -- --min-time=5 | tee { save after.txt }
open before.txt | lines | zip { open after.txt | lines } | flatten | save combined.txt

We could potentially have a script for this.

Also I use --min-time=5 to try to improve the sampling, but that might actually be more than necessary - I'll probably start going with --min-time=1 or 2 and see how it goes

One of the selling points of divan is it robustness, so it should probably not be needed, having a good script that compares two different outputs would be very nice,

@devyn

devyn commented Mar 27, 2024

Copy link
Copy Markdown
Contributor Author

@FilipAndersson245

One of the selling points of divan is it robustness, so it should probably not be needed

I personally found that some of the really quick benchmarks could be overly noisy with the default settings, and setting a minimum time helped a lot... so I'm not sure.

@devyn

devyn commented Mar 27, 2024 •

Copy link
Copy Markdown
Contributor Author

I renamed Shared to SharedCow, as I think it reflects the intention a little better, and I also made it so you don't need to do Shared::unwrap(), you can just do shared.into_owned() like Cow has.

@devyn
devyn force-pushed the value-record-as-arc branch 2 times, most recently from f68b478 to 796079b Compare March 28, 2024 21:15
Comment thread crates/nu-utils/src/shared_cow.rs Resolved
@sholderbach

Copy link
Copy Markdown
Member

Looking at the numbers, I am honestly not yet fully convinced of the runtime benefit for the incurred complexity.

The only dramatic change is appearing for nested which to me indicates we are doing Value::follow_cell_path wrong. (in theory accessing shouldn't have to clone the Record). And fixing that would apply to the other record operations where we are running into this.

/// Follow a given cell path into the value: for example accessing select elements in a stream or list
pub fn follow_cell_path(
self,
cell_path: &[PathMember],
insensitive: bool,
) -> Result<Value, ShellError> {

And indeed @IanManske ran into an annoying situation needing to clone the retrieved value for .... drum roll ... case-insensitive matching + did you mean. #11066 (comment)

Value::Record { val, .. } => {
// Make reverse iterate to avoid duplicate column leads to first value, actually last value is expected.
if let Some(found) = val.iter().rev().find(|x| {
if insensitive {
x.0.eq_ignore_case(column_name)
} else {
x.0 == column_name
}
}) {
current = found.1.clone(); // TODO: avoid clone here
} else if *optional {
return Ok(Value::nothing(*origin_span)); // short-circuit
} else if let Some(suggestion) =
did_you_mean(val.columns(), column_name)
{
return Err(ShellError::DidYouMean {
suggestion,
span: *origin_span,
});
} else {
return Err(ShellError::CantFindColumn {
col_name: column_name.clone(),
span: *origin_span,
src_span: span,
});
}
}

@IanManske

IanManske commented Mar 28, 2024 •

Copy link
Copy Markdown
Member

In theory, the Arc/SharedCow should help when cloning a large record (it has many columns and/or has many recursive children). Maybe the benchmarks don't fully highlight this scenario. Some places that come to mind as to where record cloning would be mandatory are:

  • Variable reference: $x has to clone the variable's value
  • Closures: closing over variables requires cloning
  • tee
  • Closures that have $in populated alongside the first parameter: e.g., in $record_with_many_nested_children | update column {|x| $in | ... }, $in is a clone of the value at column (and $x is the original record)
  • Commands that clone arguments: e.g., $table | update col $val clones $val for each table row

I think most of the remaining places that we clone are due to tricky or lazy implementation. But of course, they would still benefit from the reference counting until fixed.

@sholderbach

Copy link
Copy Markdown
Member

Good points @IanManske . Your reasoning is solid @devyn, my flippant reply is certainly based on the lack of benchmarks covering more "real" operations fairly instead of drilling down on just one aspect.

Having some of those engine specific aspects exercised by benchmarks would also allow us to trade off at which granularity we would want to introduce the CoW behavior. (top level values stored in variables, records/lists, strings as well...)

With how costly some of those Value refactors are (nervously looking at #12239), I really want to make sure that we are not obfuscating patterns in our implementation that need general rework.

@devyn

devyn commented Mar 30, 2024

Copy link
Copy Markdown
Contributor Author

I would certainly like to try adding some more benchmarks that do some simulated real work with a complex mix of operations rather than just simply covering core operations on different data structures. There are likely to be synergistic effects that don't really make sense to benchmark in isolation.

sholderbach added a commit to sholderbach/nushell that referenced this pull request Mar 30, 2024
Only short-term solution

Competes with nushell#12305
sholderbach added a commit to sholderbach/nushell that referenced this pull request Mar 31, 2024
Only short-term solution

Competes with nushell#12305
@sholderbach

Copy link
Copy Markdown
Member

Rebased onto main (after #12325 and #12326)

Seems to be overall performing better than #12340, but creation is slightly tending towards reduced performance.
Surprising to me are the table/record benchmarks that benefit at higher n even though I wasn't expecting too relevant clones after #12325. The eval_commands benchmark gains sound plausible.

Name main@0cf7de5 this on main Relative Note
load_standard_lib 12ms 250µs 12ms 320µs 0.99
decoding_benchmarks
json_decode
(100, 5) 559µs 700ns 556µs 500ns 1.01
(10000, 15) 217ms 300µs 216ms 400µs 1
msgpack_decode
(100, 5) 277µs 900ns 276µs 600ns 1
(10000, 15) 112ms 200µs 112ms 300µs 1
encoding_benchmarks
json_encode
(100, 5) 108µs 700ns 111µs 100ns 0.98
(10000, 15) 31ms 30µs 29ms 570µs 1.05
msgpack_encode
(100, 5) 42µs 570ns 42µs 310ns 1.01
(10000, 15) 11ms 920µs 11ms 330µs 1.05
eval_benchmarks
eval_default_config 2ms 991µs 2ms 537µs 1.18
eval_default_env 500µs 200ns 492µs 600ns 1.02
eval_commands
each
1 75µs 40ns 70µs 760ns 1.06
5 216µs 174µs 500ns 1.24
10 449µs 500ns 383µs 600ns 1.17
100 4ms 130µs 3ms 211µs 1.29
1000 53ms 230µs 32ms 509µs 1.64 🟢
for_range
1 75µs 240ns 71µs 60ns 1.06
5 214µs 100ns 212µs 600ns 1.01
10 423µs 300ns 393µs 800ns 1.07
100 4ms 889µs 3ms 581µs 1.37
1000 57ms 700µs 42ms 70µs 1.37
interleave
100 1ms 225µs 1ms 286µs 0.95
1000 12ms 270µs 12ms 610µs 0.97
10000 144ms 300µs 132ms 800µs 1.09
interleave_with_ctrlc
100 1ms 327µs 1ms 309µs 1.01
1000 12ms 850µs 13ms 90µs 0.98
10000 147ms 300µs 139ms 700µs 1.05
par_each_1t
1 146µs 500ns 121µs 500ns 1.21
5 258µs 899ns 242µs 200ns 1.07
10 492µs 200ns 434µs 600ns 1.13
100 3ms 483µs 3ms 342µs 1.04
1000 43ms 980µs 33ms 30µs 1.33
par_each_2t
1 169µs 164µs 100ns 1.03
5 249µs 600ns 243µs 600ns 1.02
10 321µs 800ns 320µs 800ns 1
100 1ms 967µs 1ms 905µs 1.03
1000 24ms 600µs 22ms 460µs 1.1
parser_benchmarks
parse_default_config_file 2ms 16µs 2ms 119µs 0.95
parse_default_env_file 346µs 378µs 600ns 0.91
record
create
1 30µs 210ns 31µs 130ns 0.97
10 43µs 290ns 45µs 990ns 0.94
100 176µs 200ns 194µs 600ns 0.91
1000 1ms 549µs 1ms 705µs 0.91
flat_access
1 17µs 570ns 18µs 430ns 0.95
10 18µs 680ns 20µs 150ns 0.93
100 30µs 960ns 24µs 840ns 1.25
1000 119µs 300ns 90µs 140ns 1.32
nest_access
1 18µs 700ns 18µs 640ns 1
2 18µs 410ns 19µs 240ns 0.96
4 19µs 840ns 19µs 730ns 1.01
8 22µs 810ns 21µs 260ns 1.07
16 28µs 460ns 24µs 370ns 1.17
32 36µs 280ns 31µs 140ns 1.17
64 49µs 200ns 42µs 380ns 1.16
128 75µs 640ns 61µs 220ns 1.24
table
create
1 37µs 740ns 37µs 460ns 1.01
10 52µs 970ns 52µs 990ns 1
100 213µs 100ns 216µs 400ns 0.98
1000 1ms 833µs 1ms 851µs 0.99
get
1 27µs 240ns 29µs 130ns 0.94
10 30µs 390ns 27µs 940ns 1.09
100 64µs 650ns 42µs 990ns 1.5
1000 366µs 500ns 189µs 800ns 1.93 🟢
select
1 27µs 720ns 27µs 150ns 1.02
10 32µs 790ns 30µs 120ns 1.09
100 96µs 670ns 74µs 240ns 1.3
1000 724µs 200ns 495µs 600ns 1.46

@sholderbach

Copy link
Copy Markdown
Member

And for the direct comparison to #12340

This rebased on main vs inserting `Record` directly into `Value`

Name #12340 9057a5f this rebased onto main@0cf7de5 Relative Note
load_standard_lib 12ms 250µs 12ms 320µs 0.99
decoding_benchmarks
json_decode
(100, 5) 561µs 100ns 556µs 500ns 1.01
(10000, 15) 218ms 800µs 216ms 400µs 1.01
msgpack_decode
(100, 5) 271µs 500ns 276µs 600ns 0.98
(10000, 15) 109ms 200µs 112ms 300µs 0.97
encoding_benchmarks
json_encode
(100, 5) 105µs 500ns 111µs 100ns 0.95
(10000, 15) 28ms 890µs 29ms 570µs 0.98
msgpack_encode
(100, 5) 41µs 430ns 42µs 310ns 0.98
(10000, 15) 11ms 360µs 11ms 330µs 1
eval_benchmarks
eval_default_config 2ms 895µs 2ms 537µs 1.14
eval_default_env 495µs 400ns 492µs 600ns 1.01
eval_commands
each
1 73µs 610ns 70µs 760ns 1.04
5 217µs 600ns 174µs 500ns 1.25
10 403µs 300ns 383µs 600ns 1.05
100 3ms 598µs 3ms 211µs 1.12
1000 37ms 240µs 32ms 509µs 1.15
for_range
1 75µs 510ns 71µs 60ns 1.06
5 202µs 800ns 212µs 600ns 0.95
10 383µs 400ns 393µs 800ns 0.97
100 3ms 638µs 3ms 581µs 1.02
1000 41ms 940µs 42ms 70µs 1
interleave
100 1ms 229µs 1ms 286µs 0.96
1000 13ms 110µs 12ms 610µs 1.04
10000 136ms 900µs 132ms 800µs 1.03
interleave_with_ctrlc
100 1ms 356µs 1ms 309µs 1.04
1000 13ms 100µs 13ms 90µs 1
10000 141ms 139ms 700µs 1.01
par_each_1t
1 145µs 600ns 121µs 500ns 1.2
5 244µs 900ns 242µs 200ns 1.01
10 466µs 300ns 434µs 600ns 1.07
100 3ms 362µs 3ms 342µs 1.01
1000 31ms 680µs 33ms 30µs 0.96
par_each_2t
1 168µs 800ns 164µs 100ns 1.03
5 246µs 700ns 243µs 600ns 1.01
10 308µs 200ns 320µs 800ns 0.96
100 1ms 900µs 1ms 905µs 1
1000 20ms 100µs 22ms 460µs 0.89
parser_benchmarks
parse_default_config_file 2ms 129µs 2ms 119µs 1
parse_default_env_file 380µs 500ns 378µs 600ns 1.01
record
create
1 31µs 940ns 31µs 130ns 1.03
10 47µs 45µs 990ns 1.02
100 193µs 800ns 194µs 600ns 1
1000 1ms 713µs 1ms 705µs 1
flat_access
1 17µs 980ns 18µs 430ns 0.98
10 19µs 820ns 20µs 150ns 0.98
100 30µs 820ns 24µs 840ns 1.24
1000 120µs 300ns 90µs 140ns 1.33
nest_access
1 20µs 540ns 18µs 640ns 1.1
2 18µs 660ns 19µs 240ns 0.97
4 20µs 470ns 19µs 730ns 1.04
8 22µs 180ns 21µs 260ns 1.04
16 27µs 190ns 24µs 370ns 1.12
32 32µs 320ns 31µs 140ns 1.04
64 46µs 500ns 42µs 380ns 1.1
128 70µs 210ns 61µs 220ns 1.15
table
create
1 35µs 490ns 37µs 460ns 0.95
10 52µs 890ns 52µs 990ns 1
100 217µs 200ns 216µs 400ns 1
1000 1ms 879µs 1ms 851µs 1.02
get
1 26µs 970ns 29µs 130ns 0.93
10 30µs 250ns 27µs 940ns 1.08
100 59µs 340ns 42µs 990ns 1.38
1000 325µs 300ns 189µs 800ns 1.71 🟢
select
1 25µs 840ns 27µs 150ns 0.95
10 32µs 80ns 30µs 120ns 1.07
100 91µs 30ns 74µs 240ns 1.23
1000 653µs 400ns 495µs 600ns 1.32

For reference comparing my two runs on `main@0cf7de5`

Name Old New Relative Note
load_standard_lib 12ms 260µs 12ms 250µs 1
decoding_benchmarks
json_decode
(100, 5) 557µs 559µs 700ns 1
(10000, 15) 216ms 400µs 217ms 300µs 1
msgpack_decode
(100, 5) 273µs 277µs 900ns 0.98
(10000, 15) 111ms 112ms 200µs 0.99
encoding_benchmarks
json_encode
(100, 5) 108µs 600ns 108µs 700ns 1
(10000, 15) 28ms 750µs 31ms 30µs 0.93
msgpack_encode
(100, 5) 41µs 860ns 42µs 570ns 0.98
(10000, 15) 11ms 590µs 11ms 920µs 0.97
eval_benchmarks
eval_default_config 2ms 987µs 2ms 991µs 1
eval_default_env 500µs 400ns 500µs 200ns 1
eval_commands
each
1 74µs 220ns 75µs 40ns 0.99
5 214µs 900ns 216µs 0.99
10 478µs 800ns 449µs 500ns 1.07
100 4ms 640µs 4ms 130µs 1.12
1000 54ms 260µs 53ms 230µs 1.02
for_range
1 76µs 130ns 75µs 240ns 1.01
5 218µs 214µs 100ns 1.02
10 450µs 200ns 423µs 300ns 1.06
100 4ms 753µs 4ms 889µs 0.97
1000 56ms 450µs 57ms 700µs 0.98
interleave
100 1ms 271µs 1ms 225µs 1.04
1000 12ms 740µs 12ms 270µs 1.04
10000 136ms 700µs 144ms 300µs 0.95
interleave_with_ctrlc
100 1ms 338µs 1ms 327µs 1.01
1000 12ms 790µs 12ms 850µs 1
10000 139ms 200µs 147ms 300µs 0.95
par_each_1t
1 144µs 200ns 146µs 500ns 0.98
5 252µs 700ns 258µs 899ns 0.98
10 475µs 200ns 492µs 200ns 0.97
100 3ms 985µs 3ms 483µs 1.14
1000 38ms 970µs 43ms 980µs 0.89
par_each_2t
1 165µs 600ns 169µs 0.98
5 246µs 200ns 249µs 600ns 0.99
10 315µs 600ns 321µs 800ns 0.98
100 2ms 206µs 1ms 967µs 1.12
1000 24ms 40µs 24ms 600µs 0.98
parser_benchmarks
parse_default_config_file 2ms 167µs 2ms 16µs 1.07
parse_default_env_file 381µs 700ns 346µs 1.1
record
create
1 32µs 80ns 30µs 210ns 1.06
10 47µs 350ns 43µs 290ns 1.09
100 193µs 900ns 176µs 200ns 1.1
1000 1ms 702µs 1ms 549µs 1.1
flat_access
1 17µs 610ns 17µs 570ns 1
10 19µs 770ns 18µs 680ns 1.06
100 27µs 740ns 30µs 960ns 0.9
1000 119µs 200ns 119µs 300ns 1
nest_access
1 18µs 18µs 700ns 0.96
2 18µs 890ns 18µs 410ns 1.03
4 20µs 800ns 19µs 840ns 1.05
8 24µs 180ns 22µs 810ns 1.06
16 27µs 120ns 28µs 460ns 0.95
32 36µs 190ns 36µs 280ns 1
64 49µs 210ns 49µs 200ns 1
128 76µs 130ns 75µs 640ns 1.01
table
create
1 38µs 380ns 37µs 740ns 1.02
10 53µs 610ns 52µs 970ns 1.01
100 215µs 100ns 213µs 100ns 1.01
1000 1ms 860µs 1ms 833µs 1.01
get
1 26µs 580ns 27µs 240ns 0.98
10 29µs 910ns 30µs 390ns 0.98
100 65µs 170ns 64µs 650ns 1.01
1000 364µs 100ns 366µs 500ns 0.99
select
1 25µs 510ns 27µs 720ns 0.92
10 33µs 540ns 32µs 790ns 1.02
100 98µs 690ns 96µs 670ns 1.02
1000 729µs 800ns 724µs 200ns 1.01

@IanManske

Copy link
Copy Markdown
Member

To add yet more chaos, I have another branch using the ecow crate's EcoVec in Record. The benefit is that it removes a level of pointer indirection. Wonder how it compares to the other two.

@devyn

devyn commented Apr 1, 2024

Copy link
Copy Markdown
Contributor Author

Okay, interesting. It seems like it's mostly a wash vs #12340, but a bit faster sometimes. In that case, if this isn't really any slower, I think it might be better to go with this so we get the extra benefits.

Thanks for comparing @sholderbach

@devyn

devyn commented Apr 1, 2024

Copy link
Copy Markdown
Contributor Author

Okay, so the remaining thing to clear up is whether we're comfortable with the DerefMut on SharedCow. It could lead to a sneaky clone() if required, but most of the time it won't - and plus is someone really likely to rethink whether they need a mutable ref just because they have to call .into_mut()? Maybe, I'm not sure.

I definitely understand the aversion to introducing a clone() that could happen without really being obvious, though.

@sholderbach

Copy link
Copy Markdown
Member

Not a fan of this DerefMut implementation. (saying this as some bitten by Deref on a custom type before: nushell/nu-ansi-term#5 nushell/reedline#435 (comment) even though there it probably came down to an incorrect implementation of the clippy lint)

@devyn

devyn commented Apr 1, 2024

Copy link
Copy Markdown
Contributor Author

Okay, duly noted. I will fix it.

@devyn
devyn force-pushed the value-record-as-arc branch from 796079b to 2b8a0ac Compare April 4, 2024 07:59
devyn added 4 commits April 4, 2024 02:26
This adds a `Shared` type as a transparent copy-on-write pointer that
clones to unique on mutable dereference.

As an initial test, the `Record` within `Value::Record` is shared.
`Shared::unwrap()` renamed to `SharedCow::into_owned()`, which takes
`self`, a little more ergonomic
This makes it more like the `Cow` interface and avoids sneaky clones
@devyn
devyn force-pushed the value-record-as-arc branch from 446faf2 to 7ac57ba Compare April 4, 2024 09:30
@devyn

devyn commented Apr 4, 2024

Copy link
Copy Markdown
Contributor Author

All green.

  • Removed DerefMut and IntoIterator
  • Added to_mut(&mut self) to reflect the Cow API that this is named after

I think the slight awkwardness will actually encourage less unnecessary taking ownership, so we should have less cloning over time

@fdncred

fdncred commented Apr 12, 2024

Copy link
Copy Markdown
Contributor

Where are we at on this? Seems like we're close but conflicts are now preventing landing?

@devyn

devyn commented Apr 13, 2024

Copy link
Copy Markdown
Contributor Author

Sure, I'll resolve conflicts probably tomorrow

@devyn

devyn commented Apr 13, 2024

Copy link
Copy Markdown
Contributor Author

Updated. 🙂

@IanManske IanManske left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! This looks good to go. Just a few minor fixes left.

Comment thread crates/nu-command/src/math/utils.rs Resolved
@devyn

devyn commented Apr 14, 2024

Copy link
Copy Markdown
Contributor Author

@IanManske thanks for looking at this in detail! Great catches! I implemented everything you suggested.

@IanManske

Copy link
Copy Markdown
Member

Great, thanks! Merging 🚀

@IanManske
IanManske merged commit 2ae9ad8 into nushell:main Apr 14, 2024
@hustcer hustcer added this to the v0.93.0 milestone Apr 14, 2024
@IanManske IanManske mentioned this pull request Apr 14, 2024
@devyn
devyn deleted the value-record-as-arc branch April 19, 2024 04:38
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.

7 participants