Skip to content

codegen: defer extern message fields in lazy views via lazy_extern_path - #850

Open
abhimanyusinghgaur wants to merge 1 commit into
anthropics:mainfrom
abhimanyusinghgaur:abhimanyu/lazy-extern-views
Open

abhimanyusinghgaur wants to merge 1 commit into
anthropics:mainfrom
abhimanyusinghgaur:abhimanyu/lazy-extern-views

Conversation

@abhimanyusinghgaur

Copy link
Copy Markdown

Problem

A lazy view defers singular and repeated message fields, except those whose type comes from another crate through extern_path: is_lazy_field returns false for every extern type (buffa-codegen/src/lazy_view.rs:44-56, line 55), because "the target crate may not ship a lazy family". The guide says the same (docs/guide.md:1457-1458). There is no option to tell codegen that it does.

A crate generated with lazy_views=true ships the family, at the layout codegen already resolves for extern types (resolve_view_family_path, buffa-codegen/src/view.rs:2957). For example, substreams-ethereum 0.12 is generated by buffa with lazy views and ships LogLazyView (src/pb/sf.ethereum.type.v2.type.__lazy_view.rs). A Substreams package that forwards many logs in its own messages, and reads only some of them, still gets MessageFieldView<…::__buffa::view::LogView<'a>> and RepeatedView<…LogView<'a>> in its lazy views. A repeated field of those is parsed for every reader at decode_lazy, and a singular one whenever its element is touched, even though the extern crate could decode them on access.

Change

  • CodeGenConfig::lazy_extern_paths: Vec<String>: proto packages or types, named like the proto side of extern_paths, whose extern crate ships the lazy view family. Documented beside extern_paths; like it, generate doesn't validate entries.
  • is_lazy_field defers an extern type when it falls under an entry (CodeGenConfig::defers_extern_type, which reuses matches_proto_prefix). The field then becomes LazyMessageFieldView or LazyRepeatedView of <extern path>::__buffa::lazy_view::<Name>LazyView, through the existing lazy-field code paths.
  • Well-known types mapped to buffa-types by default stay eager, even under ., since buffa-types ships no lazy views. If .google.protobuf is mapped to another crate, the same exact-match test as effective_extern_paths applies, and they defer when listed.
  • protoc-gen-buffa: a repeatable lazy_extern_path=<proto> option, normalized with normalize_extern_path_proto_name, in the usage text and the parse_config docs.
  • buffa-build: Config::lazy_extern_path(proto_path), stored as given and normalized in compile, like extern_path.
  • docs/guide.md: the option's row in the plugin table, and a paragraph with an example in "Lazy views".
  • A changelog fragment, kind Added.

142 net lines outside tests.

Compatibility

  • Opt-in. With no entries, is_lazy_field returns what it did before, so generated output is unchanged, the checked-in generated code included.
  • Inert without lazy_views; no warning is emitted for that. I'm happy to add one like LazyViewsRequireViews if you'd prefer.
  • A listed crate must ship the lazy family at buffa's __buffa::lazy_view layout. If it doesn't, the consumer crate fails to compile with an unresolved path. extern_path's own misconfigurations surface the same way.
  • The lazy struct's generated doc still names extern-typed fields among the eagerly decoded ones. I left it unchanged so that output without the option stays byte-identical.

Tests

  • buffa-codegen, four unit tests in tests/view_codegen.rs:
    • an extern field stays eager by default;
    • with the option, a singular field becomes LazyMessageFieldView and a repeated one LazyRepeatedView of the extern ThingLazyView;
    • a buffa-types Timestamp stays eager under .;
    • a Timestamp mapped to another crate defers.
  • protoc-gen-buffa: the option is normalized and repeatable, and invalid names are rejected.
  • buffa-test, compiled and run: protos/lazy_extern.proto maps test.lazyviews (generated with lazy views) through extern_path and lazy_extern_path. lazy_extern_path_defers_extern_fields decodes through PairLazyView, singular and repeated, sees the Timestamp stay eager, and round-trips through to_owned_message.
  • cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings and cargo test --workspace pass.
  • Against a real crate: I applied the same change to v0.9.2 and generated a message with a singular and a repeated sf.ethereum.type.v2.Log. It compiles against substreams-ethereum 0.12.1 from crates.io, and a test decodes both fields through its LogLazyView and round-trips. This check ran on v0.9.2 because main's unreleased runtime requires ViewLifetimeParametric, which views generated by the 0.9.2 release (substreams 0.8's) don't implement.
  • Not run:
    • the Docker conformance suite, since this is a codegen-only change;
    • task lint-md;
    • the two pre-commit review agents in CLAUDE.md, which weren't available to the session that wrote this.

🤖 Generated with Claude Code

Lazy views keep every field of an extern type eager, since the extern
crate may not ship the lazy view family. A crate generated with
lazy_views does ship it, so the new lazy_extern_path option
(buffa_build::Config::lazy_extern_path, plugin lazy_extern_path=<proto>)
declares it: fields of its types then become LazyMessageFieldView and
LazyRepeatedView of the crate's FooLazyView, decoded on access. Without
the option, generated output is unchanged. Well-known types mapped to
buffa-types stay eager, as buffa-types ships no lazy views.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@abhimanyusinghgaur

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Nov 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant