Skip to content

abi: a type may name the form it takes in a register - #170

Merged
psiha merged 1 commit into
masterfrom
feat/reg-form
Sep 12, 2026
Merged

abi: a type may name the form it takes in a register#170
psiha merged 1 commit into
masterfrom
feat/reg-form

Conversation

@psiha

@psiha psiha commented Sep 12, 2026

Copy link
Copy Markdown
Owner

pass_in_reg<T> decides by value versus by reference on can_be_passed_in_reg<T>, and then carries T itself. That is the right call for a type whose in-memory form is also its natural register form. It is not for a type that is a byte array in memory and a plain word in a register - a packed integer, say: passing it by value moves the bytes, and every comparator or lookup that receives it re-assembles the word per compare.

This adds one customization point:

  • reg_form<T> names the form a T takes in a register. It defaults to T, so nothing changes for any existing type. A type specializes it to name its register form; the contract is round-trip: T must convert to the form and be constructible from it. pass_in_reg<T> then stores reg_form_t<T>, decides by-value on that, converts on construction and offers the conversion back (operator value_type(), only when the two differ), so the boundary converts once in each direction and a consumer that wants the word gets the word.
  • reg_form_wrapper: a trivial wrapper inherits its value's register form when it names its value_type, can rebind itself to another value type, has its value's size and converts to and from the wrapper of the register form. A wrapper whose narrowing conversion is deliberately explicit does not qualify and keeps travelling as itself - opting in is one conversion function on the wrapper.

The primary pass_in_reg constructor is unchanged; brace-init of the stored form from the argument covers the conversion. The deduction guide is unchanged.

Measured downstream on an LTO Release build with a 3-byte packed integer as the specialized type: the change is neutral to a few percent better on the workloads where the packed key is probed in a hot loop, and neutral elsewhere. On the SysV ABI a 3-byte trivially-copyable struct already travels in a register, so the gain is the removed per-compare re-assembly rather than the calling convention.

🤖 Generated with Claude Code

`reg_form<T>` is the customization point: it defaults to T, and a type whose
storage form is not its register form - a packed integer that is a byte array
in memory and a plain word in a register - specializes it to name that word.
`pass_in_reg` then carries the register form and converts at the boundary,
once in each direction, so a comparator or a lookup receives the word and
never re-assembles it per compare.

A trivial wrapper inherits its value's register form when it names its
`value_type`, can `rebind` to another value type, has its value's size and
converts to and from the wrapper of the register form (`reg_form_wrapper`).
A wrapper whose narrowing is deliberately explicit does not qualify and keeps
travelling as itself.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@psiha
psiha merged commit 044fd0d into master Sep 12, 2026
10 checks passed
@psiha
psiha deleted the feat/reg-form branch September 12, 2026 21:15
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