-
-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend formatters: implement boolean compression #65
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
056021c
to
84504e1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a rebase + squash, but after that go ahead and merge!
impl Format for bool { | ||
fn format(&self, fmt: &mut Formatter) { | ||
let t = internp!("{:bool}"); | ||
fmt.write(&[t, *self as u8]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, wait, does this actually work in the decoder? It seems like it wouldn't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this doesn't decode properly:
#[derive(Format)]
struct Flags {
a: bool,
b: bool,
c: bool,
}
binfmt::info!(
"{:bool} {:?}",
true,
Flags {
a: true,
b: false,
c: true
}
);
Error: couldn't decode all data (remaining: [a, c, 10, d])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm actually not sure if this is even using the Format
impl, but something is going wrong somewhere 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup.. 😅 looking into how I can best attack this with a debugger now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try to help:
Encoded data: [0x0A, 0x0C, 0x10, 0x0D]
- 0x0A - string index of
"{:bool} {:?}"
- 0x0C - timestamp (10 µs)
- 0x10 - string index of
Flags
generatead format string (should be"Flags {{ a: {:bool}, b: {:bool}, c: {:bool} }}"
) - 0x0D - 0b1101 - all 4 compressed bools
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The decoder doesn't seem to handle this correctly. When it encounters the {:?}
it recursively calls parse_args
, but doesn't do any special handling for the bool decoding state. Maybe it just needs to be passed down, but I'm not sure yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for the record/note for future us:
from what I've gathered stepping through the bools_bool_struct()
test, empty_bool_indices
is overwritten when parse_args()
is recursively called to handle the {:?}
that catches our struct, which messes up the whole compression scheme etc. The question is whether we want to compress such nested bools into one u8 or if that's opening a whole can of worms. Will be mulling this over now but I don't expect any useful results before tomorrow morning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
next note: if we don't compress bools in structs like we would any other "flat" arrangement of arguments, the spec is going to get pretty tangled: compressed bools may be split (for example in a bool struct{bool bool} bool
situation) and you have to keep track of which compression u8s
cover which levels. plus, you may lose compression.
plus– to implement this, we have to keep track of state during encoding, which adds expense in terms of computation and code size.
So, I'll see how I can make flat compression work in the decoder :D
4ade024
to
45327f9
Compare
firmware/qemu/src/bin/log.rs
Outdated
@@ -18,6 +18,23 @@ fn main() -> ! { | |||
binfmt::info!("Hello {1:u16} {0:u8} {:bool}", 42u8, 256u16, false); | |||
binfmt::info!("🍕 {:[u8]}", [3,14]); | |||
|
|||
#[derive(Format)] | |||
struct Flags { | |||
a: bool, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note to self: debug output; delete
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some comments regarding the spec; haven't looked at the code yet. Will do so later.
|
||
``` rust | ||
binfmt::error!("x: {:bool}, y: {:bool}, z: {:bool}", false, false, true); | ||
// on the wire: [1, 0b100] | ||
// on the wire: [1, 0b001] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't the value be 0b100
to match the bzyx
in the comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
other way round– the xyz comment is outdated. fixing now
} | ||
|
||
binfmt::error!("x: {:bool}, {:?}", false, Flags { a: true, b: false }); | ||
// on the wire: [1, 2, 0b010,] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 to compressing the whole thing in a single byte.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately it's not as easy as I thought...
@@ -66,6 +66,13 @@ impl Format for &[u8] { | |||
} | |||
} | |||
|
|||
impl Format for bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thread below talks about structs that contain bool
. This Format
impl is going to appear in generic types like Option<bool>
formatted with {:?}
. I think this implementation as it is is correct for those types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup but the de/encoding is broken for fmt strings that contain bools in- and outside of a struct because the compression may transcend those type barriers, but the handling in the de/encoder does not.
d83eee1
to
f97da7f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a rebase, but looks good to merge otherwise.
👍 waiting a bit in case @japaric has code comments |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Feel free to squash and merge.
src/lib.rs
Outdated
} | ||
|
||
fn flush_and_reset_bools(&mut self) { | ||
self.write(&[self.bool_flags]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: you can use self.u8(self.bool_flags)
here
fb16131
to
3bbf2b1
Compare
- teach encoder how to serialze bools - change bool order in compression bytes - change position of compressed bool from first `{:bool}` to last `{:bool}` - update docs - add test for known struct format bug
3bbf2b1
to
c5e0bc4
Compare
resolves #29 .
happy to hear feedback esp. on the decoder changes, surely there's a more idiomatic way to do this.
as always, will squash before merging :)