Add if len > CAP { unreachable_unchecked() }#314
Conversation
| fn rustc_version() -> Option<u32> { | ||
| // Code copied from cxx crate | ||
|
|
||
| let rustc = std::env::var_os("RUSTC")?; | ||
| let output = std::process::Command::new(rustc) | ||
| .arg("--version") | ||
| .output() | ||
| .ok()?; | ||
| let version = String::from_utf8(output.stdout).ok()?; | ||
| let mut pieces = version.split('.'); | ||
| if pieces.next() != Some("rustc 1") { | ||
| return None; | ||
| } | ||
| let minor = pieces.next()?.parse().ok()?; | ||
| Some(minor) | ||
| } |
There was a problem hiding this comment.
Two big questions for the maintainer here: should we add a build script? Going from no build script to having one is a step back.
Second, reviewing the function: this function rustc_version must be removed. Use a maintained crate to do this - preferably autocfg - don't copy/code your own version check. The reason is long-term maintainability.
Suggested resolution: Wait until the MSRV allows this without trouble. Publish a new 0.8.x soon with updated MSRV (and a slowly sliding MSRV policy), use this without build.rs.
There was a problem hiding this comment.
i found a workaround that doesn't require build.rs, tell me what you think
|
@bluss I replaced |
hint::assert_unchecked(len <= CAP)if len > CAP { unreachable_unchecked() }
|
oh, unreachable_unchecked is stable since 1.27 but as const fn only from 1.57... at least is lower than assert_unchecked'a 1.81 requirement. |
|
We're fixing clone in the other PR, so we need one more example for when this helps in this case. I thought "codegen tests" are a good way to look at this, so if it's possible to make one like for clone it would be good. Alt solution, add |
First of all, love the library! use it all the time 😄
This PR add a
if len > CAP { unreachable_unchecked() }call inArrayVec::len().We know this assumption, and telling the compiler about it let it generate better code and eliminate some bound checks.
It also include a small benchmark for
ArrayVec::clone()that demonstrate the (massive) speed up.Clone is my specific use case, but im sure other generated code is more optimized when the compiler knows this.
Thanks for the support in advance ❤️