Skip to content

performance(runtime)!: Copy-on-Write value optimization #1349

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

Zettroke
Copy link
Contributor

@Zettroke Zettroke commented Mar 19, 2025

Summary

This request introduces CoW logic into vrl Value. It aims to allow passing Value between function by reference without the need for copies. It also makes using resolve_constant to precompute expressions viable, just making Object and Array expressions use it yielded 15% more throughput.

This request requires vectordotdev/vector#22693 to work in vector.

Change Type

  • Bug fix
  • New feature
  • Non-functional (chore, refactoring, docs)
  • Performance

Implementation

All of it is based on Arc::make_mut API. To reduce the number of code changes I decided to use custom DerefMut which calls to Arc::make_mut. While it can potentially lead to excessive copies it's still strictly better than before. But I will understand if you want a more explicit API for this.
I decided to wrap it up with Arc-only Object and Array variants. This avoids indirection for trivially copyable scalar values, and Bytes are already ref-counted. Also, these variants already include an indirection, so overhead is not that noticeable.

Benchmarks

I used our set of 120+ VRL transforms used for event normalization.
On average this change yielded almost 2x speedup in pure vrl benchmarks.
I attached the full table with results, sadly I'm not allowed to provide sources of our VRL transforms, so I kindly ask you to benchmark on your rule-sets to see speedups in other use cases

AVG MEDIAN
1.92x 1.49x
Benchmark results

Google sheets

ID Baseline (KElem/s) CoW (KElem/s) Speedup
9d521b11-8120-42d4-9afe-7ee3c6a49f20 23.904 198.31 8.30
3cf0e90b-5ce1-42f8-b8cb-7e2988ef7b17 30.832 174.9 5.67
42427cfe-7188-478b-af5b-949e5f7251cc 19.891 103.94 5.23
78441203-a45f-46ac-b4b1-0445a7ec8ce6 39.139 191.34 4.89
77d02f9c-1422-4544-b890-f5687f5764bd 26.434 121.72 4.60
06dbc9c6-ac4d-4538-b088-6379e9b78955 26.222 118.31 4.51
2884a275-e78f-47ad-9047-ec6c0a8ea36a 18.522 79.86 4.31
d53ceb64-355a-4329-921d-9bbc45b2485d 2.1036 8.9994 4.28
7390a03e-6de5-4490-990f-d9a22efaed39 10.496 43.288 4.12
e899ee9b-3309-45b2-90e8-f336e939fafc 6.054 24.107 3.98
eda4dc99-e416-4abb-9f65-ec4eab78e379 62.802 242.33 3.86
1c80f848-3b67-452d-aa98-b3ae62041982 7.1462 25.908 3.63
155a5d6f-f706-42bd-8b74-1c5725a4b9ba 29.662 101.39 3.42
6003a405-e1da-484c-9fce-36d81540acb4 4.6089 15.388 3.34
5b76321d-adf3-45f4-8e86-09b0dd41daad 17.695 58.38 3.30
693f3edb-dd8b-4571-a9da-dac13a9ca16e 27.852 89.938 3.23
174c994c-94ee-40a9-bc8a-27627827e1a1 17.663 55.056 3.12
06dd7946-fc4a-4549-b088-6379b9b78955 14.361 44.696 3.11
8a8ee06b-e1b2-4498-8451-e3f2d95565bc 64.637 182.34 2.82
29ffb493-cf1e-4975-85b3-ea47a3fc5668 14.147 39.702 2.81
89ea0f59-2c09-4939-b191-4770e087551b 25.544 70.109 2.74
561830ac-2187-42f0-b8f8-d004e2a94235 59.329 159.38 2.69
3803e51f-a3a0-4bcb-aa3c-ff0165de2505 34.363 88.658 2.58
4a6b1ace-5856-4228-9d23-83894d355833 23.262 59.045 2.54
6b68b7d5-79ff-4454-b8e5-38039833fb83 36.779 84.572 2.30
c5ae51fa-ea2c-4a81-aeef-0c1e3cac6d8c 32.911 74.799 2.27
ccbe9986-2f9e-4c87-9c78-90fea3af761a 53.407 120.72 2.26
064d78c6-fc4d-3548-b018-6479e9b78e55 15.272 34.342 2.25
bbaf0d44-61b7-482c-a274-0a572ff6637e 38.89 87.168 2.24
4d7c0c58-f36d-450d-b14e-7f89d4f86969 25.391 52.008 2.05
9ac15de2-9652-4682-aa93-c30a9b6a6818 27.205 54.721 2.01
64b68a83-6015-4c9b-8101-cc4a4ebc4c64 37.141 74.424 2.00
06dd79c6-fc4d-4548-b088-6379e9b78955 14.916 29.409 1.97
06dc79c6-fc5d-4548-b088-6379e9b79955 40.55 79.821 1.97
79993e93-1de8-4350-820c-1d8def2f2f83 20.563 39.499 1.92
388a5c82-e629-41ea-9c53-73052b5e59f0 51.147 98.14 1.92
59ed0909-473d-4404-94fe-fffffcd211ea 43.114 82.361 1.91
dd6f2836-45ce-4d0e-8f1b-57c16166ef29 85.064 161.78 1.90
6c8fedcd-93ba-4a04-911c-ba313a2abafc 63.091 118.4 1.88
a0ee97ce-a0d9-4c01-99f3-15cb09ef9ce2 17.664 33.075 1.87
19d60a93-924e-4b75-bcba-9bc869f7b12b 78.701 145.88 1.85
9b7d3c1d-7ed6-4f8e-b2c1-48551e0e4462 51.184 93.697 1.83
b14f0464-0506-4d79-8268-ec1b45815bea 27.926 49.441 1.77
9f1474b7-659f-4eb8-b214-31779a8232ad 10.854 19.133 1.76
53bc39ab-7114-49f3-9def-1579de7c5b3e 64.335 113.06 1.76
38755417-d706-446d-9912-2e5ccba231db 38.971 68.105 1.75
2b90c03c-340d-44bc-bb1d-38fca175d8c6 66.721 113.55 1.70
6e28bf67-a7e0-4d60-89ae-eae1462c016c 14.948 25.283 1.69
23c972d1-4133-43c6-aa6e-5ea207b779c6 15.363 25.957 1.69
99c07f40-27ed-436c-9d6b-076c9062e885 28.566 47.556 1.66
931561ea-cb6a-48f9-befa-67288cd16a1f 69.588 113.5 1.63
8e082450-dffd-493f-88a1-cf14626b5249 35.692 57.718 1.62
7c8d6cf2-13e4-4ae1-8cbe-a2f236ae6097 42.04 67.905 1.62
5c6c8c76-2950-4fa6-98ad-c3a07c24d233 77.595 125.21 1.61
d9653990-3127-43b7-bac3-bc8cb52956b0 67.515 107.95 1.60
5caaa0cd-fe14-4586-96cb-cc38a8408072 43.697 68.68 1.57
f94cbd2f-f534-48d2-93f9-38dfd31dbc68 14.536 22.574 1.55
d2f60369-d13c-4347-87db-8a1713263b86 57.051 88.124 1.54
778f01c2-c774-4f7d-a10e-fb924e9c92f1 11.776 18.085 1.54
064d78c6-fc4d-3548-b018-6479e9b78f55 46.765 71.662 1.53
a05f0547-fa22-40b5-aa11-fe33ddf7d4c6 16.603 24.84 1.50
dc3d361d-0dfa-43bd-8ea5-3c38944d7217 71.035 105.7 1.49
064d78c6-fc4d-3528-b017-6479e9b78e54 16.469 24.306 1.48
4e511949-4388-4933-9011-357d7fbce1c1 49.71 73.282 1.47
266c7bbc-4fbc-4e6d-b850-dfbac1f605da 61.477 90.383 1.47
5d167ee0-5548-4e71-96b2-16efed0ca4e2 73.164 105.45 1.44
1e099979-d0ec-4387-8284-5d85a8c71611 38.058 53.661 1.41
dd488716-3e04-419f-a2a6-a4a0c5ca90ce 54.318 76.463 1.41
6c5b3b40-a2f0-458d-b54c-0266a6502089 62.124 85.603 1.38
50b3a275-5175-4b85-873c-758986121265 117.61 161.81 1.38
a446d6f6-3331-4cb1-b5a8-3b39b343b1e3 97.355 133.05 1.37
068d78a8-fc4d-3549-b018-6479e9b78e55 8.1624 11.04 1.35
58c245d6-a0af-41e9-bf4f-31652386df11 142.39 192.04 1.35
2e32128b-a279-4959-a018-405919392301 82.283 110.83 1.35
0cf89cb0-bc6e-42de-9a8b-ab85b0c4e18c 120.88 162.08 1.34
9a2df316-90da-4be2-b4cf-afffaccda358 84.468 112.82 1.34
8ad81a8e-1d8f-4183-8a89-ca2455a6da10 37.671 49.3 1.31
a43a77a2-c93c-40c3-8cf4-77145d8ab77c 21.382 27.79 1.30
cc889d2d-64ab-4209-a8fc-afbede11b94d 82.721 106.65 1.29
7f8b9899-4ef0-4546-95cb-36e2077229b0 5.1225 6.5779 1.28
10d48ec3-811e-4eba-8049-f4dc8f3b5d1b 102.05 130.35 1.28
646c0409-d34d-448a-811a-9ff71e61f0ab 104.91 133.35 1.27
b306a97b-bc6c-467d-95a6-03fa71c2c221 55.863 70.849 1.27
c3042d90-10a9-41a4-9f9c-4f74dbd67f05 123.48 155.9 1.26
03f08506-6568-4fed-9bc4-f949bf6f880f 123.3 154.69 1.25
50c61824-0a0b-404b-aff7-bc68ca4e006a 13.958 17.474 1.25
953b7ea8-f5e4-48a4-80ff-1a47f6985015 23.536 29.307 1.25
19003575-98d7-4a4b-8da2-e93c1546626e 31.945 39.773 1.25
eac9d717-e24b-426d-b0c1-c84a29e12427 100.66 124.94 1.24
067e76c6-fc4d-3a83-b068-6479e9b78e65 18.985 23.552 1.24
cbc9086f-9c3a-453d-8137-7539755d7603 63.384 78.539 1.24
70cf482a-154c-4e88-a1af-4cb6a313c7be 65.007 80.5 1.24
9cb23525-a853-4c28-8eb5-ad7c79a0ac4a 134.56 164.8 1.22
50d29b2c-bca7-4cd2-be35-662cb30e29fd 41.526 50.122 1.21
99806da3-2eda-4f36-a551-c0b4c2123d1e 90.796 109.5 1.21
1f80d853-0b1f-4d47-a0f3-4d04752cb895 53.387 63.098 1.18
fcbec513-9701-4a39-8161-66e1e53ef1c2 40.488 47.807 1.18
293e2742-3a9b-4dae-9ffb-7aef000d8429 85.96 100.97 1.17
a4b278ab-38d5-4ee3-800b-23939f342b41 41.406 48.112 1.16
d92c74b1-cdb9-4f06-a64b-0f02e09602c1 73.206 84.249 1.15
0bae0d44-a1b7-492c-a374-0a572ff6637e 34.127 39.272 1.15
065d71c6-1d4d-35c8-b038-6279eab71f55 48.407 55.482 1.15
068d78c6-fc4d-3549-b018-6479e9b78e55 47.316 54.213 1.15
9720b198-4c79-4539-a3fb-d80c9a72e16a 111.43 127.3 1.14
9347482d-48c9-4429-b63e-b864d67cef93 52.937 60.12 1.14
10ad20a6-18d0-4c88-a454-dd4458f3296d 68.783 77.883 1.13
067e79e6-fc4d-3a88-b0a8-6471ebb78e55 14.163 15.874 1.12
39b1e516-b1de-476e-bf9b-d44e31d2dd65 53.725 60.054 1.12
7ne6cbv4-12sd-25f3-a21d-vfdt7d8a32f1 175.29 195.55 1.12
7765f60c-a3dc-40d6-bcc7-5d9aabb359e5 56.253 62.624 1.11
7fc14122-48a3-4da3-8554-27a72de52f8a 17.953 19.962 1.11
60024e00-6ff5-4274-b4e7-1d98eead7db3 156.89 172.92 1.10
bdf1e9b2-649c-4233-aa7e-c3dbcf969464 158.52 174.54 1.10
f5d33440-fbbb-4663-b6d1-db2bc74e21b6 91.087 99.585 1.09
6ca34b9d-cae5-4cff-ab7c-b1609dbfa888 78.086 85.08 1.09
b4a8c51f-b211-4967-825e-71cf9a3b856a 109.44 115.45 1.05
02aef7bc-6a70-487c-815d-6a3641aca1d9 223.69 233.35 1.04
e917c646-ee08-4ebc-8578-6f8a42af1511 76.56 79.799 1.04
5f2b2b8e-63f3-48c9-ae25-8db1d320944a 173.15 179.25 1.04
d5a15b44-ed4c-483d-a901-a78377b33f66 178.9 181.64 1.02
782ce02b-dc4b-4c66-b85d-210ae8de1f3b 124.68 124.31 1.00
74d77206-86cb-480f-97bf-4162ddc5ea93 186.42 181.49 0.97
8433f0e2-911b-4ea0-a4c4-7279a3e33951 285.83 274.7 0.96

Is this a breaking change

  • Yes, sadly it's not possible to make it non-breaking without using some advanced rust wizardry.
  • No

How did you test this PR?

We used included unit tests + we already used this change in our vector load-testing setup.

Does this PR include user facing changes?

  • Yes. Please add a changelog fragment based on
    our guidelines.
  • No. A maintainer will apply the "no-changelog" label to this PR.

Checklist

  • Our CONTRIBUTING.md is a good starting place.
  • If this PR introduces changes to LICENSE-3rdparty.csv, please
    run dd-rust-license-tool write and commit the changes. More details here.
  • For new VRL functions, please also create a sibling PR in Vector to document the new function.

@Zettroke Zettroke changed the title enhancement(Value)!: CoW value optimization enhancement(Value)!: Copy-on-Write value optimization Mar 19, 2025
@pront
Copy link
Member

pront commented Mar 20, 2025

Related: #1021
cc @bruceg you might be interested in this.

@pront pront changed the title enhancement(Value)!: Copy-on-Write value optimization performance(runtime)!: Copy-on-Write value optimization Mar 20, 2025
@pront
Copy link
Member

pront commented Mar 20, 2025

Hi @Zettroke, thank you for submitting this. I added it to my list for review. Is this a breaking change for users or for developers only?

@Zettroke
Copy link
Contributor Author

Zettroke commented Mar 20, 2025

Hi @pront, no it's a breaking change only in terms of crate API. VRL itself is unchanged.

@@ -12,12 +12,26 @@ use crate::{

#[derive(Debug, Clone, PartialEq)]
pub struct Array {
resolved: Option<Value>,
Copy link
Member

Choose a reason for hiding this comment

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

How hard would it be to separate out the changes to cache the resolved value of these array and object expressions? On first reading, I thought this was part of the Value CoW optimization and then realized it was doing something different.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I just wanted to show bigger gains ;)

Copy link
Member

@bruceg bruceg left a comment

Choose a reason for hiding this comment

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

This makes sense to me, if the numbers look good, except for the issue of merging the two changes together and a couple of naming nits. It's too bad it means having to .into() everything, but that seems unavoidable.

@Zettroke Zettroke requested a review from bruceg March 29, 2025 13:16
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.

3 participants