Skip to content

Zero-cost type family conversions - #3906

Merged
2kai2kai2 merged 2 commits into
canaryfrom
kai/unified-type-algebra
Jul 2, 2026
Merged

2kai2kai2 merged 2 commits into
canaryfrom
kai/unified-type-algebra

Conversation

@2kai2kai2

@2kai2kai2 2kai2kai2 commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

By ensuring relevant type family enums have a recursively identical memory layout:

  • RealizedTy -> RuntimeTy -> Ty conversions are now a zero-cost transmute
  • Ty -> RuntimeTy -> RealizedTy conversions are now a read-only walk followed by zero-cost transmute (no copy/reallocations)
  • Concrete type enums do a move at the top level, but since their child nodes are all RuntimeTy/RealizedTy we only have to move the type head.

Summary by CodeRabbit

  • New Features

    • Added zero-cost conversions between related type views for faster rendering and subtype checks.
    • Generated consistent explicit discriminants for related enum variants to keep in-memory tags stable.
    • Added layout guarantees for generated family types to support safe reinterpretation.
  • Bug Fixes

    • Improved narrowing and upcasting so invalid variants now fail cleanly instead of producing mismatched tags.
    • Updated tests to cover deep nesting, borrowed views, and in-memory versus wire-format discriminants.

2kai2kai2 added 2 commits July 2, 2026 10:15
By ensuring that the type family members have recursively identical
memory layouts, we can enable zero-cost conversions via
`::std::mem::transmute`.
Caveats:
- Concrete family members have a smaller total size, so the root node of
  their type tree must be converted. However, this is O(1) since all
  child nodes are `RuntimeTy` or `RealizedTy`, meaning we do not have to
  walk the children.
- Downcasts still require a walk to verify
- We rely on some de-facto sound but not formally guaranteed memory
  layouts from rust: primarily, that `Vec` and tuples should have a
  consistent memory layout based on element memory size. At a later
  date, we can switch to guaranteed-safe versions (but in practice this
  is fine)
Instead of walking and copying for narrowing, we instead walk to verify
(no reallocations) then transumte in-place if the narrowing is legal.
@vercel

vercel Bot commented Jul 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
beps Ready Ready Preview, Comment Jul 2, 2026 5:33pm
promptfiddle Ready Ready Preview, Comment Jul 2, 2026 5:33pm
promptfiddle2 Ready Ready Preview, Comment Jul 2, 2026 5:33pm

Request Review

@2kai2kai2
2kai2kai2 enabled auto-merge July 2, 2026 17:19
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

⏭️ Performance benchmarks were skipped

Perf benchmarks (CodSpeed) are opt-in on pull requests — they no longer run on every push. They always run automatically after merge to canary/main.

To run them on this PR, do any of the following, then push a commit (or re-run CI):

  • Add RUN_CODSPEED=1 to the PR description, or
  • Include run-perf or /perf in the PR title or any commit message.

@coderabbitai

coderabbitai Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: c11dd4a0-aab2-4e4f-86ae-bc7b486e161d

📥 Commits

Reviewing files that changed from the base of the PR and between 5ba654d and 7bdf4e2.

📒 Files selected for processing (4)
  • baml_language/crates/baml_type/src/family.rs
  • baml_language/crates/baml_type/src/runtime_ty.rs
  • baml_language/crates/baml_type_macros/src/convert.rs
  • baml_language/crates/baml_type_macros/src/emit.rs

📝 Walkthrough

Walkthrough

This PR introduces zero-cost transmute-based widening and narrowing conversions between type-family members with equal-size representations, replacing structural rebuild-based conversions for such pairs. It adds explicit discriminant tagging, layout assertions, satellite repr(C) layouts, RuntimeTy delegation via borrowed views, and new tests validating discriminant stability and narrowing correctness.

Changes

Zero-cost transmute conversions for type family

Layer / File(s) Summary
Discriminant and layout emission
baml_language/crates/baml_type_macros/src/emit.rs
Explicit u8 discriminants assigned to enum variants from master declaration order; satellite structs marked #[repr(C)].
Layout assertion and naming helpers
baml_language/crates/baml_type_macros/src/convert.rs
Adds layout_assert, snake_ident, as_method_name, enum_validator_name, sat_validator_name helpers, expanded module docs, and Satellite import.
Transmute widening/narrowing/validation generators
baml_language/crates/baml_type_macros/src/convert.rs
emit_conversions dispatches deep equal-size pairs to gen_widen_transmute, gen_as_upcast, gen_narrow_transmute with validators (gen_validators, validate_body/arm/expr, gen_sat_validator, reachable_satellites); satellite conversions updated, sat_widen_body removed.
RuntimeTy delegation to borrowed Ty views
baml_language/crates/baml_type/src/runtime_ty.rs
Rendering, subtyping, and Display now use self.as_ty() borrow instead of Ty::from(self) conversion.
Discriminant stability and narrowing/widening tests
baml_language/crates/baml_type/src/family.rs
New in_memory_tag helper and tests verifying discriminant consistency, upcast/widening equivalence, and narrowing validation/rejection paths.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant NarrowImpl as "TryFrom<&Sup> for &Sub"
  participant Validator as "validate_body"
  participant SatValidator as "gen_sat_validator"

  Caller->>NarrowImpl: try_from(&sup_value)
  NarrowImpl->>Validator: validate(&sup_value)
  Validator->>SatValidator: recurse into satellite fields
  SatValidator-->>Validator: Ok(()) or Not<Sub>
  Validator-->>NarrowImpl: Ok(()) or Not<Sub>
  alt validation passes
    NarrowImpl->>NarrowImpl: transmute borrow to &Sub
    NarrowImpl-->>Caller: Ok(&Sub)
  else validation fails
    NarrowImpl-->>Caller: Err(Not<Sub> variant)
  end
Loading

Possibly related PRs

  • BoundaryML/baml#3801: The main PR's transmute/layout conversion logic directly builds on this PR's ty_family! macro infrastructure and conversion matrix generation.
  • BoundaryML/baml#3887: The transmute/narrowing tests and RuntimeTy upcast rendering relate to this PR's Ty::Infer sentinel handling in the same family types.

Poem

A byte at the front, a tag held tight,
Transmuted borrows in the pale moonlight 🌙
Validate once, then reinterpret free,
No copies made — just you and me.
Hop, hop, through repr(C) rows,
This rabbit's code compactly grows! 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the PR’s main change: making type family conversions zero-cost.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch kai/unified-type-algebra

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 2, 2026 17:22 Inactive
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

Binary size checks passed

✅ 7 passed

Artifact Platform File Gzip Gated on Baseline Delta Status
✅ baml-cli Linux 🔒 21.6 MB 9.2 MB file 21.5 MB +90.2 KB (+0.4%) OK
✅ packed-program Linux 🔒 15.7 MB 6.6 MB file 15.6 MB +124.0 KB (+0.8%) OK
✅ baml-cli macOS 🔒 16.6 MB 8.0 MB file 16.6 MB +66.3 KB (+0.4%) OK
✅ packed-program macOS 🔒 12.2 MB 5.8 MB file 12.1 MB +83.1 KB (+0.7%) OK
✅ baml-cli Windows 🔒 18.2 MB 8.2 MB file 18.1 MB +71.7 KB (+0.4%) OK
✅ packed-program Windows 🔒 13.1 MB 5.9 MB file 13.0 MB +95.4 KB (+0.7%) OK
✅ bridge_wasm WASM 14.5 MB 🔒 4.1 MB gzip 4.1 MB +53.5 KB (+1.3%) OK

🔒 = the size this artifact is GATED on (ceiling + delta). Binaries gate on file size (installed binary); WASM gates on gzip (download size). The other size is shown for information only.


Generated by cargo size-gate · workflow run

@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 2, 2026 17:33 Inactive
@2kai2kai2
2kai2kai2 added this pull request to the merge queue Jul 2, 2026
Merged via the queue into canary with commit 4bdbea5 Jul 2, 2026
53 checks passed
@2kai2kai2
2kai2kai2 deleted the kai/unified-type-algebra branch July 2, 2026 18:06
@coderabbitai coderabbitai Bot mentioned this pull request Jul 14, 2026

This branch was previously deployed

3 inactive deployments
Preview – promptfiddle — 7bdf4e22 Deployed Jul 2, 2026 by vercel[bot]
Preview – promptfiddle2 — 7bdf4e22 Deployed Jul 2, 2026 by vercel[bot]
Preview – beps — 7bdf4e22 Deployed Jul 2, 2026 by vercel[bot]
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