rust-lang / rustdoc-types

Rustdoc's JSON output interface
Apache License 2.0
28 stars 15 forks source link

feat!: reduce size of Id and add IdRef #31

Closed jalil-salame closed 1 month ago

jalil-salame commented 1 month ago

This reduces the size of Id from 24 bytes to 16 bytes on 64-bit systems, and from 12 bytes to 8 bytes in 32-bit systems.

It also adds an IdRef type that we can use instead of a &Id, this avoids a double indirection (&str vs &String). In trustfall-rustdoc-adapter we see a 7% perf improvement when using &IdRef instead of &Id.

Sadly, AFAIK, there is no way to safely coerce Id(Box<str>) to &IdRef(str) so we need to do an unsafe std::mem::transmute(&str) -> &IdRef.

zulip discussion

jalil-salame commented 1 month ago

Apparently bytemuck's TransparentWrapper handles this case, so we could switch to that instead and have it execute the unsafe code.

aDotInTheVoid commented 1 month ago

Closing as this should be sent to rust-lang/rust

jalil-salame commented 1 month ago

Sorry, I didn't read the contributing guidelines. The comment addresses experimental changes where we switch from HashMap<&Id, _> to HashMap<&IdRef, _> instead of changing to FxHashMap<&Id, _>. I'm preparing a PR where I do add the IdRef.

The generics are from an experiment on unsized coercion, I thought I might be able to coerce Box<str> to str (because you can't build &IdRef safely otherwise). This works for [_; N] -> [_] coercion, but not Box<str> -> str sadly.