From: "Gary Guo" <gary@garyguo.net>
To: "Eliot Courtney" <ecourtney@nvidia.com>,
"Gary Guo" <gary@garyguo.net>,
"Alexandre Courbot" <acourbot@nvidia.com>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Uladzislau Rezki" <urezki@gmail.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Onur Özkan" <work@onurozkan.dev>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"John Hubbard" <jhubbard@nvidia.com>,
"Alistair Popple" <apopple@nvidia.com>,
"Timur Tabi" <ttabi@nvidia.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org,
dri-devel <dri-devel-bounces@lists.freedesktop.org>
Subject: Re: [PATCH v2 7/8] gpu: nova-core: add NVKV typed decoding
Date: Thu, 17 Sep 2026 15:12:00 +0100 [thread overview]
Message-ID: <DLHNICVL6Z3P.RRA9TJ2P7XHC@garyguo.net> (raw)
In-Reply-To: <DLHKBLIVFIX8.346WFGAIJBL9D@nvidia.com>
On Thu Sep 17, 2026 at 12:42 PM BST, Eliot Courtney wrote:
> On Thu Sep 17, 2026 at 5:52 PM JST, Gary Guo wrote:
>> On Thu Sep 17, 2026 at 9:37 AM BST, Alexandre Courbot wrote:
>>> On Thu Sep 17, 2026 at 8:53 AM BST, Gary Guo wrote:
>>>> On Mon Sep 14, 2026 at 7:55 AM BST, Eliot Courtney wrote:
>>>>> On Mon Sep 14, 2026 at 12:46 PM JST, Alexandre Courbot wrote:
>>>>>> On Thu Aug 27, 2026 at 11:12 PM JST, Eliot Courtney wrote:
>>>>>>> use crate::gsp::nvkv::{
>>>>>>> + Array,
>>>>>>> Index,
>>>>>>> + Key,
>>>>>>> KeyId,
>>>>>>> Op,
>>>>>>> Opcode, //
>>>>>>> };
>>>>>>> use crate::num;
>>>>>>>
>>>>>>> +/// Defines a schema struct together with its [`Schema`] implementation that decodes into `$target`.
>>>>>>> +///
>>>>>>> +/// Each member of the struct should implement `Schema`. For every (key, index, value) triple
>>>>>>> +/// decoded from the NVKV stream, the generated parent `Schema` implementation will call each member
>>>>>>> +/// in declaration order with that triple. If a member consumes that triple, it will stop there.
>>>>>>> +/// Otherwise it will keep going until all members are tried.
>>>>>>> +///
>>>>>>> +/// The schema struct holds the state required by the schema implementation to do the decode. It's
>>>>>>> +/// recommended to use one of the existing Schema kinds (`Required`, `Accumulated`, `Key`, `Array`,
>>>>>>> +/// `Indexed`) for each member.
>>>>>>> +///
>>>>>>> +/// # Examples
>>>>>>> +///
>>>>>>> +/// ```
>>>>>>> +/// nvkv_decode! {
>>>>>>> +/// struct RequestSchema => Request {
>>>>>>> +/// id: Required<u32, 0x0001>,
>>>>>>> +/// name: Array<u8, 64, 0x0002>,
>>>>>>> +/// }
>>>>>>> +/// }
>>>>>>> +/// ```
>>>>>>> +macro_rules! nvkv_decode {
>>>>>>> + (
>>>>>>> + $(#[$attr:meta])*
>>>>>>> + $vis:vis struct $name:ident => $target:ident {
>>>>>>> + $(
>>>>>>> + $(#[$field_attr:meta])*
>>>>>>> + $field_vis:vis $field:ident : $ty:ty
>>>>>>> + ),* $(,)?
>>>>>>> + }
>>>>>>> + ) => {
>>>>>>> + $(#[$attr])*
>>>>>>> + $vis struct $name {
>>>>>>> + $(
>>>>>>> + $(#[$field_attr])*
>>>>>>> + $field_vis $field: $ty,
>>>>>>> + )*
>>>>>>> + }
>>>>>>> +
>>>>>>> + impl $crate::gsp::nvkv::Schema for $name {
>>>>>>> + type Target = $target;
>>>>>>> +
>>>>>>> + fn init() -> impl ::kernel::prelude::Init<Self> {
>>>>>>> + ::pin_init::init!(Self {
>>>>>>> + $( $field <- <$ty as $crate::gsp::nvkv::Schema>::init(), )*
>>>>>>> + })
>>>>>>> + }
>>>>>>> +
>>>>>>> + fn visit(
>>>>>>> + &mut self,
>>>>>>> + key: $crate::gsp::nvkv::KeyId,
>>>>>>> + index: $crate::gsp::nvkv::Index,
>>>>>>> + value: $crate::gsp::nvkv::DecoderValue<'_>,
>>>>>>> + ) -> ::kernel::error::Result<bool> {
>>>>>>> + Ok(false
>>>>>>> + $( || $crate::gsp::nvkv::Schema::visit(&mut self.$field, key, index, value)? )*)
>>>>>>
>>>>>> Mmm looks like this is going to be `O(n)` with `n` being the number of
>>>>>> fields?
>>>>>>
>>>>>> This is ok for a first implementation but eventually I hope we can
>>>>>> switch to a more efficient dispatch.
>>>>>
>>>>> I thought quite a bit about this while writing this code, since we need
>>>>> the escape hatch to imperative decode (custom Schema impl basically). To
>>>>> be able to get it down to a match on the key, we need to know ahead of
>>>>> time which keys a Schema will consume. That duplicates the info from the
>>>>> visit() implementation.
>>>>>
>>>>> I thought up a few methods but it's unclear to me which one is best, so
>>>>> I just left it for now. Please LMK if you think this is urgent, I can
>>>>> try in a follow up to improve this. Here are my ideas (when I say O(1)
>>>>> lookup I mean modulo how the compiler decides to do it with the set of
>>>>> key IDs it gets):
>>>>>
>>>>> 1. current code - just visit()
>>>>> pros: key source of truth not duplicates
>>>>> cons: O(field) visit as you say
>>>>>
>>>>> 2. Associated const KEY_ID: Option<KeyId> - None if a Schema accepts multiple keys.
>>>>> You can match on each associated const in the macro.
>>>>> pros: O(1) if the current key goes to a field with KEY_ID = Some(...)
>>>>> cons: O(#fields accepting multiple keys) if current key is one of them
>>>>>
>>>>> 3. fn accepts() -> bool
>>>>> You can match on `if F::accepts(key)` for each field. We could potentially make
>>>>> this const with Gary's const traits polyfill.
>>>>> pros: O(1) if you write an inline-able+optimizable implementation.
>>>>>
>>>>> 4. Associated const KEYS table; use tricks to concat tables
>>>>> pros: O(1) lookup
>>>>> cons: actually MSRV can't get this to optimize down to O(1)
>>>>> if you use slice::contains(), but stable can.
>>>>
>>>> Hmm, am I missing the obvious? Why not generate a `match` expression on IDs of
>>>> fields? It looks like in the example all keys would have a known ID to the
>>>> macro.
>>>
>>> Some keys may come from embedded structs, which the macro has no way to
>>> see.
>>
>> You can match all keys that you can see, and delegate to embedded structs if
>> keys are not known.
>>
>> This is essentially the same pattern that `#[serde(flatten)]` uses, just
>> replacing identifier names with keys.
>>
>> The keys don't need be part of the type system, and it just additional metadata
>> for the macro to generate correct impl.
>>
>> Best,
>> Gary
>
> This sounds approximately equivalent to the option #2 I mentioned
> (w.r.t. delegating if the keys are not known).
>
> I considered a serde-like approach where the metadata is annotated for
> the macro rather than by the type system, but I wanted to keep the macro
> simple and not introduce more macro-DSL than I need to. And also keep it
> kinda easy to write manual Schemas by not keeping the deserialization
> info in macro metadata.
I guess that given that your schema types are completely separate from the
struct definitions, it is okay.
Best,
Gary
next prev parent reply other threads:[~2026-09-17 14:12 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-27 14:12 [PATCH v2 0/8] gpu: nova-core: add NVKV codec Eliot Courtney
2026-08-27 14:12 ` [PATCH v2 1/8] rust: alloc: add Vec::try_push_init Eliot Courtney
2026-08-27 14:12 ` [PATCH v2 2/8] rust: alloc: add Vec::push_init Eliot Courtney
2026-08-27 14:30 ` sashiko-bot
2026-08-27 14:12 ` [PATCH v2 3/8] rust: alloc: add ArrayVec Eliot Courtney
2026-08-27 14:33 ` sashiko-bot
2026-08-27 14:12 ` [PATCH v2 4/8] gpu: nova-core: add NVKV encoder Eliot Courtney
2026-08-27 14:35 ` sashiko-bot
2026-09-07 15:07 ` Alexandre Courbot
2026-09-14 4:44 ` Eliot Courtney
2026-08-27 14:12 ` [PATCH v2 5/8] gpu: nova-core: add NVKV decoder Eliot Courtney
2026-08-27 14:36 ` sashiko-bot
2026-09-09 0:51 ` Alexandre Courbot
2026-09-09 1:13 ` Eliot Courtney
2026-09-09 4:48 ` Alexandre Courbot
2026-09-14 4:45 ` Eliot Courtney
2026-09-10 7:47 ` Alexandre Courbot
2026-09-14 5:45 ` Eliot Courtney
2026-08-27 14:12 ` [PATCH v2 6/8] gpu: nova-core: add NVKV typed encoding Eliot Courtney
2026-09-10 8:10 ` Alexandre Courbot
2026-09-11 5:17 ` Alexandre Courbot
2026-09-11 5:28 ` Eliot Courtney
2026-09-11 11:18 ` Alexandre Courbot
2026-09-14 4:46 ` Eliot Courtney
2026-08-27 14:12 ` [PATCH v2 7/8] gpu: nova-core: add NVKV typed decoding Eliot Courtney
2026-09-14 3:46 ` Alexandre Courbot
2026-09-14 6:55 ` Eliot Courtney
2026-09-14 7:04 ` John Hubbard
2026-09-14 7:16 ` Eliot Courtney
2026-09-14 12:06 ` Alexandre Courbot
2026-09-17 5:02 ` Eliot Courtney
2026-09-17 7:53 ` Gary Guo
2026-09-17 8:37 ` Alexandre Courbot
2026-09-17 8:52 ` Gary Guo
2026-09-17 11:42 ` Eliot Courtney
2026-09-17 14:12 ` Gary Guo [this message]
2026-08-27 14:12 ` [PATCH v2 8/8] gpu: nova-core: add NVKV GSP_INIT schemas Eliot Courtney
2026-08-27 14:34 ` sashiko-bot
2026-09-14 4:11 ` Alexandre Courbot
2026-09-14 5:42 ` Eliot Courtney
2026-09-14 12:12 ` Alexandre Courbot
2026-09-18 13:55 ` Eliot Courtney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DLHNICVL6Z3P.RRA9TJ2P7XHC@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=apopple@nvidia.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dri-devel-bounces@lists.freedesktop.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=ecourtney@nvidia.com \
--cc=jhubbard@nvidia.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=lossin@kernel.org \
--cc=nova-gpu@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=ttabi@nvidia.com \
--cc=urezki@gmail.com \
--cc=vbabka@kernel.org \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.