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

Lint for large error types #6560

Open
Nemo157 opened this issue Jan 7, 2021 · 2 comments
Open

Lint for large error types #6560

Nemo157 opened this issue Jan 7, 2021 · 2 comments
Assignees
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-perf Lint: Belongs in the perf lint group T-middle Type: Probably requires verifiying types

Comments

@Nemo157
Copy link
Member

Nemo157 commented Jan 7, 2021

What it does

Warn when an error type (probably by detecting that it has impl std::error::Error and is publicly exported) is over some arbitrary threshold, suggesting boxing the inner data to reduce the size.

Categories (optional)

  • Kind: clippy::perf

Large error types result in a lot of redundant memory copying on the success path when the Ok value is small.

Drawbacks

None.

Example

struct Error {
    data: [u8; 512],
}

impl std::error::Error for Error {}

Could be written as:

struct Error {
    data: Box<[u8; 512]>,
}

impl std::error::Error for Error {}

Other

There are two existing related issues: #4652 and #3884. I think this is different because in those cases it detects when you use the error internally with a small success variant; whereas even if internally all usage is with large success variants, a user of the library may be mapping the success variant to something small, ending up hitting one of those lints in their usage and having to box the whole error; while it would be easier for all users if the size was reduced at the source.

@Nemo157 Nemo157 added the A-lint Area: New lints label Jan 7, 2021
@camsteffen camsteffen added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-perf Lint: Belongs in the perf lint group T-middle Type: Probably requires verifiying types labels Feb 18, 2021
@Y-Nak
Copy link
Contributor

Y-Nak commented Feb 24, 2021

@rustbot claim

bors added a commit that referenced this issue Aug 30, 2022
Initial implementation `result_large_err`

This is a shot at #6560, #4652, and #3884. The lint checks for `Result` being returned from functions/methods where the `Err` variant is larger than a configurable threshold (the default of which is 128 bytes). There has been some discussion around this, which I'll try to quickly summarize:

* A large `Err`-variant may force an equally large `Result` if `Err` is actually bigger than `Ok`.
* There is a cost involved in large `Result`, as LLVM may choose to `memcpy` them around above a certain size.
* We usually expect the `Err` variant to be seldomly used, but pay the cost every time.
* `Result` returned from library code has a high chance of bubbling up the call stack, getting stuffed into `MyLibError { IoError(std::io::Error), ParseError(parselib::Error), ...}`, exacerbating the problem.

This PR deliberately does not take into account comparing the `Ok` to the `Err` variant (e.g. a ratio, or one being larger than the other). Rather we choose an absolute threshold for `Err`'s size, above which we warn. The reason for this is that `Err`s probably get `map_err`'ed further up the call stack, and we can't draw conclusions from the ratio at the point where the `Result` is returned. A relative threshold would also be less predictable, while not accounting for the cost of LLVM being forced to generate less efficient code if the `Err`-variant is _large_ in absolute terms.

We lint private functions as well as public functions, as the perf-cost applies to in-crate code as well.

In order to account for type-parameters, I conjured up `fn approx_ty_size`. The function relies on `LateContext::layout_of` to compute the actual size, and in case of failure (e.g. due to generics) tries to come up with an "at least size". In the latter case, the size of obviously wrong, but the inspected size certainly can't be smaller than that. Please give the approach a heavy dose of review, as I'm not actually familiar with the type-system at all (read: I have no idea what I'm doing).

The approach does, however flimsy it is, allow us to successfully lint situations like

```rust
pub union UnionError<T: Copy> {
    _maybe: T,
    _or_perhaps_even: (T, [u8; 512]),
}

// We know `UnionError<T>` will be at least 512 bytes, no matter what `T` is
pub fn param_large_union<T: Copy>() -> Result<(), UnionError<T>> {
    Ok(())
}
```

I've given some refactoring to `functions/result_unit_err.rs` to re-use some bits. This is also the groundwork for #6409

The default threshold is 128 because of #4652 (comment)

`lintcheck` does not trigger this lint for a threshold of 128. It does warn for 64, though.

The suggestion currently is the following, which is just a placeholder for discussion to be had. I did have the computed size in a `span_label`. However, that might cause both ui-tests here and lints elsewhere to become flaky wrt to their output (as the size is platform dependent).

```
error: the `Err`-variant returned via this `Result` is very large
  --> $DIR/result_large_err.rs:36:34
   |
LL | pub fn param_large_error<R>() -> Result<(), (u128, R, FullyDefinedLargeError)> {
   |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The `Err` variant is unusually large, at least 128 bytes
```

changelog: Add [`result_large_err`] lint
@GnomedDev
Copy link
Contributor

Should this be closed now that we have #9373?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-perf Lint: Belongs in the perf lint group T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

4 participants