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

Manually implement Clone #61

Merged
merged 5 commits into from
Jan 31, 2024
Merged

Conversation

AntonSol919
Copy link
Contributor

I noticed clone was producing inefficient assembly.
Here is a PR that manually implements Clone.

The first commit has benchmarks comparing three methods.

Here are my results:

Cloning inline strings:

running 3 tests
test bench::bench_derive_clone ... bench:     460,725 ns/iter (+/- 9,990)
test bench::bench_match_clone  ... bench:     177,981 ns/iter (+/- 13,023)
test bench::bench_new_clone    ... bench:     178,105 ns/iter (+/- 4,005)

Cloning heap strings:

running 3 tests
test bench::bench_derive_clone ... bench:   3,197,654 ns/iter (+/- 98,527)
test bench::bench_match_clone  ... bench:   3,193,360 ns/iter (+/- 74,968)
test bench::bench_new_clone    ... bench:   3,192,459 ns/iter (+/- 76,398)

Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

match vs unsafe does not seem to really make a difference here does it? In that case I'd probably prefer a safe implementation via matching. Otherwise, at least instead of transmute_copy I would want to use ptr::read here, as we aren't transmuting.

This regressed from a previous attempt.
The worst of the old results were in the range 450.000

current:

test bench::bench_derive_clone ... bench:   1,653,247 ns/iter (+/- 32,781)
test bench::bench_match_clone  ... bench:   1,716,482 ns/iter (+/- 34,192)
test bench::bench_new_clone    ... bench:   1,717,985 ns/iter (+/- 52,137)
test bench::bench_derive_clone ... bench:     454,318 ns/iter (+/- 11,401)
test bench::bench_match_clone  ... bench:     183,570 ns/iter (+/- 10,652)
test bench::bench_new_clone    ... bench:     177,907 ns/iter (+/- 2,234)
@AntonSol919
Copy link
Contributor Author

Match also uses unsafe. Its the only method I know to prevent the compiler from emitting additional branches.

I updated the PR to use ptr read.

Note that f8ed961 caused a regression due to Repr ordering (see the first commit for my results).

@Veykril
Copy link
Member

Veykril commented Jan 24, 2024

Needs a reformatting

@Veykril
Copy link
Member

Veykril commented Jan 31, 2024

Thanks!

@Veykril Veykril merged commit 0fb3a13 into rust-analyzer:master Jan 31, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants