Rework structured value casting #396
No reviewers
Labels
No labels
bug
duplicate
easy
enhancement
help wanted
invalid
question
rfc
structured-logging
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-rust-lang/log!396
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/value-casting"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This is a spike to see if we can improve the way we cast values to concrete types. It does more work when capturing
DebugandDisplayto retain the underlying primitive so that a new macro could reasonably default to creating values through these std traits while retaining numbers.It does some wacky build script code generation to create a static sorted set of type ids we care about so they can be binary searched quickly.
@ -2,13 +2,72 @@//! atomics and sets `cfg` flags accordingly.I’m using this so the
into_primitivemodule can tell if it’s in the build script or not. Is there a cfg that exists already for this?This seems nice for debug assertions. I’ll look into where stabilisation is at.
I think we just stabilise these const methods on-demand. I can’t see why this one shouldn’t be stabilised, but will look into it.
See: https://github.com/rust-lang/rust/pull/72488
See: https://github.com/rust-lang/rust/issues/53485
Specialization is possible because we use
impl Traitinstead ofdyn Traitfor inputs. This approach would still be necessary if we switched todyn Trait. The trade-off would be that withdyn Traitwe could generate less code in calling libraries at the cost of still requiring this runtime check.cc @hawkw sorry for the random ping but thought you might find this interesting for
tracing🙂This is using a static set of type ids sorted at build time and binary searches them for interesting types given a generic
T: Debug + ‘staticinput and treats the input as that concrete type. The runtime cost per argument is small (I measured ~9ns on my machine for a hit and ~5ns for a miss) but is not negligible. It could also use specialisation to remove that runtime cost, which I think might land asmin_specializationsometime over the next year. Maybe you could consider it for something like the#[instrument]macro, to keep the nice compatibleDebugbound, but also retain numbers and booleans as structured values?This is fascinating, thanks for pinging me!
So...
If https://github.com/rust-lang/rust/pull/72437 and https://github.com/rust-lang/rust/pull/72488 are both merged then you’d be able to do this at compile time without specialization :)
There’s an example in the
const_type_idPR.Just testing different strategies for destructuring.
So strangely enough this approach using const seems slightly slower than the runtime binary search. I haven’t looked into why that is yet.
Just a few missing inlines.
@ -0,0 +1,195 @@/*@hawkw I’ve got a few different strategies for capturing the value from a generic
There now.For
tracing, if this is something you wanted to do you might end up needing to look more at the specialization one, because the others require a’staticbound in order to get the type id, which from what I can see isn’t compatible with how the#[instrument]macro currently works.I'll wait for https://github.com/rust-lang/rust/pull/72488 before merging this PR so this unstable feature can be removed.
Once a new
nightlygoes out this should be good to merge!