Rework structured value casting #396

Merged
KodrAus merged 24 commits from fix/value-casting into master 2020-08-03 03:05:16 -05:00
KodrAus commented 2020-05-22 12:00:31 -05:00 (Migrated from github.com)

This is a spike to see if we can improve the way we cast values to concrete types. It does more work when capturing Debug and Display to 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.

This is a spike to see if we can improve the way we cast values to concrete types. It does more work when capturing `Debug` and `Display` to 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.
KodrAus (Migrated from github.com) reviewed 2020-05-22 12:05:08 -05:00
@ -2,13 +2,72 @@
//! atomics and sets `cfg` flags accordingly.
KodrAus (Migrated from github.com) commented 2020-05-22 12:01:33 -05:00

I’m using this so the into_primitive module can tell if it’s in the build script or not. Is there a cfg that exists already for this?

I’m using this so the `into_primitive` module can tell if it’s in the build script or not. Is there a cfg that exists already for this?
KodrAus (Migrated from github.com) commented 2020-05-22 12:03:57 -05:00

This seems nice for debug assertions. I’ll look into where stabilisation is at.

This seems nice for debug assertions. I’ll look into where stabilisation is at.
KodrAus (Migrated from github.com) commented 2020-05-22 12:04:54 -05:00

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.

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.
KodrAus (Migrated from github.com) reviewed 2020-05-23 01:29:05 -05:00
KodrAus (Migrated from github.com) commented 2020-05-23 01:29:05 -05:00
See: https://github.com/rust-lang/rust/pull/72488
KodrAus (Migrated from github.com) reviewed 2020-05-23 01:29:25 -05:00
KodrAus (Migrated from github.com) commented 2020-05-23 01:29:24 -05:00
See: https://github.com/rust-lang/rust/issues/53485
KodrAus (Migrated from github.com) reviewed 2020-05-26 04:56:45 -05:00
KodrAus (Migrated from github.com) commented 2020-05-26 04:56:45 -05:00

Specialization is possible because we use impl Trait instead of dyn Trait for inputs. This approach would still be necessary if we switched to dyn Trait. The trade-off would be that with dyn Trait we could generate less code in calling libraries at the cost of still requiring this runtime check.

Specialization is possible because we use `impl Trait` instead of `dyn Trait` for inputs. This approach would still be necessary if we switched to `dyn Trait`. The trade-off would be that with `dyn Trait` we could generate less code in calling libraries at the cost of still requiring this runtime check.
KodrAus (Migrated from github.com) reviewed 2020-05-26 09:15:12 -05:00
KodrAus (Migrated from github.com) commented 2020-05-26 09:15:12 -05:00

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 + ‘static input 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 as min_specialization sometime over the next year. Maybe you could consider it for something like the #[instrument] macro, to keep the nice compatible Debug bound, but also retain numbers and booleans as structured values?

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 + ‘static` input 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 as `min_specialization` sometime over the next year. Maybe you could consider it for something like the `#[instrument]` macro, to keep the nice compatible `Debug` bound, but also retain numbers and booleans as structured values?
hawkw (Migrated from github.com) reviewed 2020-05-26 11:30:29 -05:00
hawkw (Migrated from github.com) commented 2020-05-26 11:30:29 -05:00

This is fascinating, thanks for pinging me!

This is fascinating, thanks for pinging me!
KodrAus (Migrated from github.com) reviewed 2020-05-27 22:45:10 -05:00
KodrAus (Migrated from github.com) commented 2020-05-27 22:45:09 -05:00

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_id PR.

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_id` PR.
KodrAus (Migrated from github.com) reviewed 2020-06-03 08:59:17 -05:00
KodrAus (Migrated from github.com) commented 2020-06-03 08:57:36 -05:00

Just testing different strategies for destructuring.

Just testing different strategies for destructuring.
KodrAus (Migrated from github.com) commented 2020-06-03 08:58:38 -05:00

So strangely enough this approach using const seems slightly slower than the runtime binary search. I haven’t looked into why that is yet.

So strangely enough this approach using const seems slightly slower than the runtime binary search. I haven’t looked into why that is yet.
KodrAus (Migrated from github.com) reviewed 2020-06-03 11:01:08 -05:00
KodrAus (Migrated from github.com) commented 2020-06-03 11:01:08 -05:00

Just a few missing inlines.

Just a few missing inlines.
KodrAus (Migrated from github.com) reviewed 2020-06-03 21:25:50 -05:00
@ -0,0 +1,195 @@
/*
KodrAus (Migrated from github.com) commented 2020-06-03 21:25:39 -05:00

@hawkw I’ve got a few different strategies for capturing the value from a generic T here 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 ’static bound in order to get the type id, which from what I can see isn’t compatible with how the #[instrument] macro currently works.

@hawkw I’ve got a few different strategies for capturing the value from a generic `T` here 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 `’static` bound in order to get the type id, which from what I can see isn’t compatible with how the `#[instrument]` macro currently works.
KodrAus (Migrated from github.com) reviewed 2020-06-30 20:43:32 -05:00
KodrAus (Migrated from github.com) commented 2020-06-30 20:43:31 -05:00

I'll wait for https://github.com/rust-lang/rust/pull/72488 before merging this PR so this unstable feature can be removed.

I'll wait for https://github.com/rust-lang/rust/pull/72488 before merging this PR so this unstable feature can be removed.
KodrAus commented 2020-07-30 00:35:56 -05:00 (Migrated from github.com)

Once a new nightly goes out this should be good to merge!

Once a new `nightly` goes out this should be good to merge!
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-rust-lang/log!396
No description provided.