r/rust • u/panstromek • 5d ago
Compile time improvements for derive(Debug)
https://yoyo-code.com/derive-debug-compile-time-improvements/
178
Upvotes
r/rust • u/panstromek • 5d ago
71
u/MalbaCato 5d ago
Interesting post overall.
small (but somewhat important) nitpick:
variant names, as all identifiers, aren't restricted to ascii in rust, but a splitting a UTF-8 string by a byte which corresponds to an ascii character (the space) results in valid UTF-8 on both sides.
15
u/Lucretiel Datadog 4d ago
I'm... pretty surprised that this was the approach that was taken, rather than just, like, introducing an optimization to the way that
matchis compiled that lets it handle the "typical"match(exactly one arm for each variant of a single enum) in linear time.14
u/BobTreehugger 4d ago
Interesting writeup, I'm sure everyone at every step thought, "oh this will be a quick fix" and it just kept being more complicated.
I did notice one small thing though:
Variant names don't have to be ascii as far as I can tell. Is it just for this optimization?
Doesn't seem needed though -- the nice thing about utf8 is that ascii bytes are encoded the same as ascii and don't depend on any parser state, so you can just look for the spaces without needing full unicode handling for this simple case (splitting by exactly a space).
So my guess is that variant names don't need to be ascii, but that's fine for this case.
5
u/dnew 4d ago
This seems sub-optimal. Create an array of booleans set to false the size of the enum. On each match, make sure the boolean entry is false, then set it true. At the end, make sure they're all set true. Seems like it ought to be linear in the size of the enum if they're all just enum matches.
3
u/minno 4d ago
A few ideas I had while reading this:
Sprites::Sprite1 => (0usize, 8usize).from_raw_partson the "Using concatenated string for C-like enums" code snippet, does it avoid the compile time slowdown?3
u/SkiFire13 4d ago
You got this the opposite way: you can reduce SAT to match exhaustiveness checking, and that proves it's NP-complete.
3
u/sephg 4d ago
Instead of splitting by space, wouldn’t it be better to just make an offsets array? Eg “RedGreenBlue” + [0,3,8]. Then printing a value would be something like print_str(str[inxs[i]..idxs.get(i+1).unwrap_or(str.len())])
This approach would increase the cost per discriminant by 3 bytes (u32 instead of a space character). But it’s constant time at runtime instead of linear time. And it wouldn’t require any complex string scanning iteration code to be written or pulled in.
4
u/matthieum [he/him] 4d ago
That TOCTOU issues with macros seems like a fairly serious problem, and yet... I guess it's unavoidable?
I wonder if macros could be classified in two categories:
I figure that this could be done by simply looking at the macro output, and double-check whether it changed the item or not.
And from there, a warning/error could be emitted if any additive macro is called before a rewriting macro, since anything it added is relying on an old version of the item. Perhaps rerunning the additive macro to check whether it generates the same output first, to avoid false-positives.
Note: a macro can definitely be BOTH rewriting and additive... and the additive parts may definitely be out-of-sync if another rewrite occurs after.