Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Shrink the size of all Error types #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alexcrichton
Copy link
Contributor

This commit improves the in-memory size of Error from 7 pointers to 1 pointer.
Errors are in general relatively rare in applications and having a huge error
type ends up generating lots of instructions for moves and such, so this PR
optimizes for size and passing around errors rather than creating errors.

#[doc(hidden)]
pub $crate::State,
);
pub struct $error_name(pub Box<($error_kind_name, $crate::State)>);
Copy link
Contributor

@Yamakaky Yamakaky Sep 24, 2017

Choose a reason for hiding this comment

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

Since we can't pattern match on the kind (box if nightly only), maybe we could remove pub and put everything into $crate as

struct ErrorInternals<K> {
  kind: K,
  backtrace: Backtrace,
  next: ...
}

I tried that at one time, but it was rejected because it prohibited the use of pattern matching. But if we can't use it anyway...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At least in the usage I've seen I think it's quite important to match on ErrorKind, but is matching on the entire error itself that important?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes : https://github.com/rust-lang-nursery/error-chain/blob/master/src/lib.rs#L348-L353
With the new method, you can't do that anymore. You have to match on Result, then on error.kind().

Copy link
Contributor

Choose a reason for hiding this comment

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

Wrong link. Adding #226

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's true, yes.

@Yamakaky
Copy link
Contributor

Nice! You just have to update the tests which use pattern matching on the kind. It's also a breaking change so could you update the CHANGELOG?

@alexcrichton
Copy link
Contributor Author

Updated

@Yamakaky
Copy link
Contributor

Could you rebase please?

This commit improves the in-memory size of `Error` from 7 pointers to 1 pointer.
Errors are in general relatively rare in applications and having a huge error
type ends up generating lots of instructions for moves and such, so this PR
optimizes for size and passing around errors rather than creating errors.
@alexcrichton
Copy link
Contributor Author

Sure thing, done now

ErrorKind::Msg(_) => {}
_ => {}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not very satisfied with this new behaviour, it's less straightforward. Not sure how to improve it though...

Copy link

Choose a reason for hiding this comment

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

box_patterns will be stabilized some day, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Yamakaky do you have a preferred method of how we might land this? I'd personally like to see this as at least an option, it's what I'd want for libraries 99% of the time I believe.

@Lymia
Copy link

Lymia commented Oct 28, 2017

When might this pull request be land/be merged?

@AndyGauge AndyGauge added the 0.13 Breaking Change label Jun 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
0.13 Breaking Change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants