Skip to content

extra::test: define an opaque function to assist with accurate benchmarking. #12105

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

Closed
wants to merge 2 commits into from

Conversation

huonw
Copy link
Member

@huonw huonw commented Feb 8, 2014

This allows a result to be marked as "used" by passing it to a function
LLVM cannot see inside (unless LTO is enabled).

Closes #8261.

@huonw
Copy link
Member Author

huonw commented Feb 8, 2014

I'll file a bug replacing #8261 about fixing the various overly-optimised benchmarks in the source tree when this lands.

@lilyball
Copy link
Contributor

lilyball commented Feb 8, 2014

Is there any way you could use asm!() to tell LLVM that the value is used without actually doing anything with it?

@huonw
Copy link
Member Author

huonw commented Feb 8, 2014

Probably, but I don't think that's particularly better than this solution. (It might maybe work better in the face of LTO; I'll test.)

@huonw
Copy link
Member Author

huonw commented Feb 8, 2014

asm does seem to work better: even LTO can't kill it.

I've added two new commits; the second of which is an example of how to fix broken benchmarks.

@alexcrichton
Copy link
Member

It seems a little unfortunate to require that most benchmarks use black_box in order to make themselves meaningful. Perhaps we could have fn iter<T>(f: || -> T) and by default you call black_box on the return value of the closure?

@lilyball
Copy link
Contributor

lilyball commented Feb 9, 2014

I think @alexcrichton's suggestion is the right way to go.

huonw added 2 commits February 9, 2014 12:02
benchmarking.

This allows a result to be marked as "used" by passing it to a function
LLVM cannot see inside. By making `iter` generic and using this
`black_box` on the result benchmarks can get this behaviour simply by
returning their computation.
Before:

    test test::bench_nonpod_nonarena  ... bench:        62 ns/iter (+/- 6)
    test test::bench_pod_nonarena     ... bench:         0 ns/iter (+/- 0)

After:

    test test::bench_nonpod_nonarena  ... bench:       158 ns/iter (+/- 11)
    test test::bench_pod_nonarena     ... bench:        48 ns/iter (+/- 2)

The other tests show no change, but are adjusted to use the generic
return value of `.iter` anyway so that this doesn't change in future.
@huonw
Copy link
Member Author

huonw commented Feb 9, 2014

Done.

bors added a commit that referenced this pull request Feb 9, 2014
This allows a result to be marked as "used" by passing it to a function
LLVM cannot see inside (unless LTO is enabled).

Closes #8261.
@bors bors closed this Feb 9, 2014
@huonw huonw deleted the bench-black-box branch February 9, 2014 04:42
flip1995 pushed a commit to flip1995/rust that referenced this pull request Jan 11, 2024
… r=llogiq

Extend `useless_asref` lint on `map(clone)`

If you have code like:

```rust
Some(String::new()).as_ref().map(Clone::clone)
```

the `as_ref` call is unneeded.

Interestingly enough, this lint and `map_clone` are starting to share a same "space" where both lints warn about different things for the same code. Not sure what's the policy about such cases though...

r? `@llogiq`

changelog: Extend `useless_asref` lint on `map(clone)`
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.

Dead code elimination in benchmarks
4 participants