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

test: reduce size of vec #6488

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft

test: reduce size of vec #6488

wants to merge 7 commits into from

Conversation

Dunqing
Copy link
Member

@Dunqing Dunqing commented Oct 13, 2024

related: oxc-project/backlog#18

TEST ONLY!

Everything is hard-coded, just for testing

Copy link

graphite-app bot commented Oct 13, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “0-merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

@Dunqing Dunqing marked this pull request as draft October 13, 2024 03:53
@github-actions github-actions bot added A-linter Area - Linter A-parser Area - Parser A-minifier Area - Minifier A-ast Area - AST A-transformer Area - Transformer / Transpiler A-codegen Area - Code Generation A-prettier Area - Prettier A-isolated-declarations Isolated Declarations A-ast-tools Area - AST tools labels Oct 13, 2024
Copy link
Member Author

Dunqing commented Oct 13, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @Dunqing and the rest of your teammates on Graphite Graphite

Copy link

codspeed-hq bot commented Oct 13, 2024

CodSpeed Performance Report

Merging #6488 will improve performances by 7.23%

Comparing 10-13-test_reduce_size_of_vec (29c102e) with main (a9260cf)

Summary

⚡ 6 improvements
✅ 23 untouched benchmarks

Benchmarks breakdown

Benchmark main 10-13-test_reduce_size_of_vec Change
minifier[antd.js] 426.2 ms 397.4 ms +7.23%
minifier[typescript.js] 519.8 ms 489.6 ms +6.17%
semantic[cal.com.tsx] 35.9 ms 34.7 ms +3.37%
transformer[antd.js] 50.3 ms 48.2 ms +4.36%
transformer[checker.ts] 20.1 ms 19 ms +5.88%
transformer[pdf.mjs] 7.6 ms 7.2 ms +5.64%

@Boshen Boshen added the needs-discussion Requires a discussion from the core team label Oct 14, 2024
@Boshen
Copy link
Member

Boshen commented Oct 14, 2024

Need to measure memory (RSS) change thorough /usr/bin/time

@Boshen
Copy link
Member

Boshen commented Oct 14, 2024

First phase should not introduce breaking change. Only change the underlying u64s to u32.

@Dunqing Dunqing force-pushed the 10-13-test_reduce_size_of_vec branch from 36573f6 to 74b40d2 Compare October 14, 2024 13:15
@Dunqing Dunqing force-pushed the 10-13-test_reduce_size_of_vec branch from afd9a55 to 4f219ad Compare October 14, 2024 13:43
@Dunqing
Copy link
Member Author

Dunqing commented Oct 14, 2024

First phase should not introduce breaking change. Only change the underlying u64s to u32.

I have changed the underlying usize to u32 in this PR and have saved the original changes in back-reduce-size-of-vec branch

@Dunqing Dunqing removed A-linter Area - Linter A-parser Area - Parser A-minifier Area - Minifier A-transformer Area - Transformer / Transpiler labels Oct 14, 2024
@Dunqing Dunqing removed A-codegen Area - Code Generation A-prettier Area - Prettier labels Oct 14, 2024
Boshen pushed a commit that referenced this pull request Oct 19, 2024
`oxc_allocator::Vec` is intended for storing AST types in the arena. `Vec` is intended to be non-drop, because all AST types are non-drop. If they were not, it would be a memory leak, because those types will not have `drop` called on them when the allocator is dropped.

However, `oxc_allocator::Vec` is currently a wrapper around `allocator_api2::vec::Vec`, which *is* drop. That unintentionally makes `oxc_allocator::Vec` drop too.

This PR fixes this by wrapping the inner `allocator_api2::vec::Vec` in `ManuallyDrop`. This makes `oxc_allocator::Vec` non-drop.

The wider consequence of this change is that the compiler is now able to see that loads of other types which contain `oxc_allocator::Vec` are also non-drop. Once it can prove that, it can remove a load of code which handles dropping these types in the event of a panic. This probably also then allows it to make many further optimizations on that simplified code.

Strictly speaking, this PR is a breaking change. If `oxc_allocator::Vec` is abused to store drop types, then in some circumstances this change could produce a memory leak where there was none before. However, we've always been clear that only non-drop types should be stored in the arena, so such usage was always a bug.

#6622 fixes the only place in Oxc where we mistakenly stored non-drop types in `Vec`.

The change to `oxc_prettier` is because compiler can now deduce that `Doc` is non-drop, which causes clippy to raise a warning about using `then` instead of `then_some`.

As follow-up, we should:

1. Wrap other `allocator_api2` types (e.g. `IntoIter`) in `ManuallyDrop` too, so compiler can prove they are non-drop too (or reimplement `Vec` ourselves - #6488).
2. Introduce static checks to prevent non-drop types being used in `Box` and `Vec`, to make memory leaks impossible, and detect them at compile time.
@Boshen Boshen removed the needs-discussion Requires a discussion from the core team label Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ast Area - AST A-ast-tools Area - AST tools A-isolated-declarations Isolated Declarations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants