<re_irc>
<@jamesmunns:beeper.com> Was looking at some code that did a lot of fixed point math recently, opt level s was half the size of opt level 3, with opt level z in the middle. But for my benchmarked code, opt level 3 was 40ms, opt level s was 50ms, and opt level z was 80ms
starblue has quit [Ping timeout: 252 seconds]
starblue has joined #rust-embedded
<re_irc>
<@therealprof:matrix.org> Yeah, with multiscalar pipelined CPUs with tons of cache all over the place compiler heuristics tend to make weird assumptions about the straight forward low end.
<re_irc>
<@jamesmunns:beeper.com> (this was on an nrf52), but yeah turning on I cache brought the numbers down to like 35/40/55
starblue has quit [Ping timeout: 240 seconds]
starblue has joined #rust-embedded
IlPalazzo-ojiisa has joined #rust-embedded
emerent has quit [Ping timeout: 256 seconds]
emerent has joined #rust-embedded
Dr_Who has joined #rust-embedded
<re_irc>
<@boiethios:matrix.org> Hey, are you aware of any stack-based vector than can be built at compile-time? I had a look at arrayvec and heapless::Vec, but none of them allows that.
<re_irc>
<@bumblebee10:matrix.org> but what would a stack based vector be? there are non-dynamic fixed-size arrays, [0u8; usize], but i guess that's not what you want?
<re_irc>
<@boiethios:matrix.org> bumblebee10: That's a vector whose items are on the stack, like arrayvec or heapless::Vec.
<re_irc>
<@boiethios:matrix.org> Those are no-std friendly vectors.
<re_irc>
<@jamesmunns:beeper.com> I guess the more enlightening question is "what are you trying to achieve"?
<re_irc>
<@jamesmunns:beeper.com> Like, a static initialization value?
<re_irc>
<@boiethios:matrix.org> It's a bit long to explain…
<re_irc>
<@jamesmunns:beeper.com> like a vector that COULD hold 32 items, but starts with 8 specific items always?
<re_irc>
<@boiethios:matrix.org> : Basically, yes
<re_irc>
<@jamesmunns:beeper.com> or a read only, constant item?
<re_irc>
<@boiethios:matrix.org> : Read only, constant. I need a vector, because it's a field inside a struct, something like "Foo<const N: usize> { bar: &'static[Bar] }" and I need an array of those "foo"s. I don't want the vector to be inline, because that would use a huge amount of bytes, while with this, it only cost a reference when needed.
<re_irc>
<@boiethios:matrix.org> * "Foo { bar: &'static [Bar]
<re_irc>
<@boiethios:matrix.org> So, the aim is: the user passes a const slice in, and I build a const stack-based vector whom I pass as a slice to build this struct.
<re_irc>
<@jamesmunns:beeper.com> okay, so why not:
<re_irc>
<@boiethios:matrix.org> Hum, I see. My code looks like that:
<re_irc>
where
<re_irc>
I: LayerIndex,
<re_irc>
pub enum Action<I>
<re_irc>
{
<re_irc>
KeyPress(KeyPress),
<re_irc>
/// A regular key press: optional weak modifiers and one key.
<re_irc>
// /// Several key presses.
<re_irc>
// Combo(todo)
<re_irc>
/// A regular (strong) modifier press.
<re_irc>
Modifiers(Modifiers),
<re_irc>
/// Change the layer as long as the key is pressed.
<re_irc>
LayerChange(LayerChange<I>),
<re_irc>
/// Handle a tap and a hold differently.
<re_irc>
TapOrHold(TapOrHold<I>),
<re_irc>
/// A macro, returning several keys.
<re_irc>
Combo(Combo),
<re_irc>
/// Custom action
<re_irc>
Custom(Custom<I>),
<re_irc>
}
<re_irc>
This enum weight 24 bytes. Combo can have a great amount of bytes of information, and if I store it inline, it can multiply the size of the enum by 10, which may not be sustainable (or so I think)
<re_irc>
<@boiethios:matrix.org> Hum, I see. My code looks like that:
<re_irc>
pub enum Action<I>
<re_irc>
where
<re_irc>
I: LayerIndex,
<re_irc>
{
<re_irc>
/// A regular key press: optional weak modifiers and one key.
<re_irc>
KeyPress(KeyPress),
<re_irc>
/// A regular (strong) modifier press.
<re_irc>
Modifiers(Modifiers),
<re_irc>
/// Change the layer as long as the key is pressed.
<re_irc>
LayerChange(LayerChange<I>),
<re_irc>
/// Handle a tap and a hold differently.
<re_irc>
TapOrHold(TapOrHold<I>),
<re_irc>
/// A macro, returning several keys.
<re_irc>
Combo(Combo),
<re_irc>
/// Custom action
<re_irc>
Custom(Custom<I>),
<re_irc>
}
<re_irc>
This enum weight 24 bytes. Combo can have a great amount of bytes of information, and if I store it inline, it can multiply the size of the enum by 10, which may not be sustainable (or so I think)
<re_irc>
<@boiethios:matrix.org> * weights
<re_irc>
<@boiethios:matrix.org> This is what I'm trying and fix.
<re_irc>
<@boiethios:matrix.org> : The normalized data can take more space or less, depending on the input.
<re_irc>
<@jamesmunns:beeper.com> So, the usual answer to this manually would be to have Combos take a "&'static [KeyPress]" (or whatever). This means tho you sorta have to write out all the items you want
<re_irc>
<@jamesmunns:beeper.com> the not-manual answer to this is "macros"
<re_irc>
<@boiethios:matrix.org> I don't want to use macros.
<re_irc>
<@jamesmunns:beeper.com> const can't really do "N items in, M items out, where N AND M are not specified"
<re_irc>
<@jamesmunns:beeper.com> the other option is "generate some code in a build rs, from like a JSON or YAML file"
<re_irc>
<@boiethios:matrix.org> OK, thanks for the help. I'll think about all of that.
<re_irc>
<@jamesmunns:beeper.com> There is some "far future" talk about being able to allocate a Vec in a const fn at compile time, and decay to a const slice or array, but I don't think that's possible even w/ unstable features today
<re_irc>
<@jamesmunns:beeper.com> (there's a lot of "it breaks a ton of assumptions everywhere" issues with it, I don't have the issue at hand tho)
<re_irc>
<@boiethios:matrix.org> : Well, it's no-std, so I cannot use a "Vec" anyway, nor allocate, more generally.
<re_irc>
<@jamesmunns:beeper.com> yeah, the idea is you use a Vec _at compile time_, and it becomes a slice/array you can use
<re_irc>
<@jamesmunns:beeper.com> so at COMPILE TIME you use an allocator, but it is not needed at runtime
<re_irc>
<@boiethios:matrix.org> Oh, I see.
<re_irc>
I could ask the user to provide a "&'static mut [u8]" which would be used as a poor man's allocator.
<re_irc>
<@jamesmunns:beeper.com> not exactly? but also I'm describing a feature that doesn't exist, so feel free to ignore
<re_irc>
<@jamesmunns:beeper.com> the goal would be to allow something like:
<re_irc>
<@jamesmunns:beeper.com> or return a Vec that can be used without an allocator (maybe only in a read only way), this sorta talks about it: https://github.com/rust-lang/const-eval/issues/20
<re_irc>
<@jamesmunns:beeper.com> but yeah. Rust super can't do that at all today.
<re_irc>
<@boiethios:matrix.org> Maybe I could write something like:
<re_irc>
<@jamesmunns:beeper.com> You _can_ get const to do fancy things sometimes, but you miiiight want to wrap it into a macro that does this stuff under the hood.
<re_irc>
<@jamesmunns:beeper.com> (if you want to end up with basically "&[&[Key]]" tho, it gets way more complicated)
<re_irc>
<@boiethios:matrix.org> : I don't. The final result will be a slice of HID reports.
<re_irc>
<@boiethios:matrix.org> IIUC correctly your example, without a macro, the user has to find the correct "N" by trial and error?
<re_irc>
<@boiethios:matrix.org> Oh, no, I see
<re_irc>
<@jamesmunns:beeper.com> or needs to invoke all the steps themselves, e.g.