4

Deduplicate panic_fmt by nbdd0121 · Pull Request #88860 · rust-lang/rust · GitHu...

 2 years ago
source link: https://github.com/rust-lang/rust/pull/88860
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Copy link

Contributor

@klensy klensy on Sep 11

edited

Ahh, begin_panic_fmt get params by reference, when panic_fmt takes by value. It's actually interesting to change panic_fmt to get it by reference too, as reference should be shorter than Arguments struct.

Copy link

Contributor

Author

@nbdd0121 nbdd0121 on Sep 11

edited

It's an existing inconsistency between libcore and libstd API. The inconsistency actually makes the compiler/rustc_const_eval/src/const_eval/machine.rs's begin_panic_fmt -> const_panic_fmt replacement unsound (but since const panic doesn't allow arguments today this unsound path is never hit).

Copy link

Contributor

Author

@nbdd0121 nbdd0121 on Sep 11

Ahh, begin_panic_fmt get params by reference, when panic_fmt takes by value. It's actually interesting to change panic_fmt to get it by reference too, as reference should be shorter that Arguments struct.

I actually don't think there'll be any meaningful difference. Size of Arguments would make it be passed by reference anyway.

Plus, many existing methods like write_fmt by value and Arguments is Copy so I think it's actually good to take it by value for consistency.

Copy link

Contributor

@klensy klensy on Sep 11

print-type-size type: `std::fmt::Arguments`: 48 bytes, alignment: 8 bytes
print-type-size     field `.pieces`: 16 bytes
print-type-size     field `.fmt`: 16 bytes
print-type-size     field `.args`: 16 bytes

Copy link

Contributor

Author

@nbdd0121 nbdd0121 on Sep 11

What are you suggesting by listing the numbers? As I've said, the size of Arguments (is larger than 2 usize) would make it be passed by reference. So codegen will behave exactly the same with or without the & (https://godbolt.org/z/h69MKjdsn), and for semantics and consistency it should be Arguments rather than &Arguments.

Copy link

Contributor

@klensy klensy on Sep 11

edited

Sorry, i didn't knew that there some optimization that pass big Copy types by ref even if i explicitly pass it by value.

Value passed into PanicInfo::internal_constructor by Option'ing reference anyway, so there is no sense to copying it before that. (this comment about pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> !)

Copy link

Contributor

@klensy klensy on Sep 11

Tried to check that in small example, looks like parameter really isn't copied in panic_fmt (didn't checked this in stdlib). I remember that i played somewhere with fmt::Arguments and saw needless copying in stdlib.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK