Skip to content

[Fusion] Look up input object fields by UTF-8 name during variable coercion - #10522

Merged
tobias-tengler merged 3 commits into
mainfrom
tte/fusion-utf8-input-field-lookup
Oct 13, 2026
Merged

tobias-tengler merged 3 commits into
mainfrom
tte/fusion-utf8-input-field-lookup

Conversation

@tobias-tengler

@tobias-tengler tobias-tengler commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Variable coercion allocated several objects for every field of an input object variable: JsonVariableCoercion read JsonProperty.Name up to three times per field (field lookup, error path segment and the coerced ObjectFieldNode), and each read decodes a new UTF-16 string. Every coerced field also got a new NameNode.

This PR looks input fields up by their raw UTF-8 property name. FusionFieldDefinitionCollection<T> gets a TryGetField(ReadOnlySpan<byte>) overload: collections with up to eight accessible fields are scanned with Ascii.Equals, larger ones widen the name into a stack buffer and use the ReadOnlySpan<char> alternate lookup of the existing frozen dictionary. FusionEnumValueCollection does the same and drops its sorted byte index, so no collection keeps a second copy of its names. The coerced ObjectFieldNode reuses a NameNode cached on FusionInputFieldDefinition, and the error path uses the schema's field name. JSON allows escaping any character of a property name ({"\u0069d": 1} is a valid way to send id), so a property name containing an escape sequence falls back to the unescaped lookup, only when the UTF-8 lookup misses.

JsonMarshal.GetRawUtf8PropertyName exists since .NET 10, so net8.0 and net9.0 keep the string lookup. Per schema this adds the cached NameNode (40 B per input field definition) and one lookup struct per collection (16 B on net9.0 and later), nothing else.

Benchmarks

VariableCoercionBenchmark (added in this PR), net11 in-process.

Small input (2 objects, 5 fields)

Method Version Mean Ratio Allocated Alloc Ratio
Variable coercion main 737.2 ns 1.00 1.67 KB 1.00
Variable coercion PR 555.8 ns 0.75 0.98 KB 0.59

Large input with a list of 20 input objects (22 objects, 66 fields)

Method Version Mean Ratio Allocated Alloc Ratio
Variable coercion main 8.262 μs 1.00 18.27 KB 1.00
Variable coercion PR 5.696 μs 0.69 9.48 KB 0.52

Variable coercion allocates 41% less for the small input and 48% less for the large input, and is 25% and 31% faster.

@tobias-tengler
tobias-tengler force-pushed the tte/fusion-utf8-input-field-lookup branch 2 times, most recently from bd53aa7 to 4ca43dc Compare October 9, 2026 10:40
@tobias-tengler
tobias-tengler marked this pull request as ready for review October 9, 2026 10:41
Copilot AI balanced review requested due to automatic review settings October 9, 2026 10:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The enum refactor introduces an avoidable array allocation for every enum collection.

1 open finding
What changed in this PR

Optimizes Fusion input-object variable coercion by matching raw UTF-8 property names and reusing cached syntax nodes.

Changes:

  • Adds a shared UTF-8 name index.
  • Uses UTF-8 lookup with escaped-name fallback.
  • Adds lookup and coercion coverage.
File Description
VariableCoercionHelperTests.cs Tests escaped JSON field names.
FusionInputFieldDefinitionCollectionTests.cs Tests UTF-8 field lookup.
JsonVariableCoercion.cs Uses UTF-8 lookup and cached names.
FusionInputFieldDefinition.cs Caches each field’s NameNode.
Utf8NameIndex.cs Implements shared UTF-8 indexing.
FusionInputFieldDefinitionCollection.cs Adds span-based field lookup.
FusionEnumValueCollection.cs Adopts the shared index.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Patch coverage

100.0% of changed lines covered (47/47)

File Covered Changed Patch %
…/Collections/FusionEnumValueCollection.cs 8 8 100.0% 🟢
…/Collections/FusionFieldDefinitionCollection.cs 23 23 100.0% 🟢
…/src/Fusion.Execution.Types/FusionInputFieldDefinition.cs 2 2 100.0% 🟢
…/src/Fusion.Execution/Execution/JsonVariableCoercion.cs 14 14 100.0% 🟢

Project coverage: 58.7% (315520/537948 lines)

…ercion

Variable coercion read `JsonProperty.Name` up to three times per input object
field and created a new `NameNode` for every coerced field. Fields are now looked
up by their raw UTF-8 property name and the coerced `ObjectFieldNode` reuses a
`NameNode` cached on the input field definition. Escaped property names fall
back to the unescaped lookup.
@tobias-tengler
tobias-tengler force-pushed the tte/fusion-utf8-input-field-lookup branch from 4ca43dc to 2fe2abb Compare October 9, 2026 10:55
JsonMarshal.GetRawUtf8PropertyName only exists since .NET 10, so the guard
now targets NET10_0_OR_GREATER and net8.0 and net9.0 keep the string lookup.
The input field and enum value collections no longer build a sorted byte
index per collection. Collections with up to eight accessible entries are
scanned with Ascii.Equals, larger ones widen the name into a stack buffer and
use the ReadOnlySpan<char> alternate lookup of the existing frozen
dictionary. The byte-span lookup lives on the field collection base class.

Adds a variable coercion benchmark for the small and large input shapes.
@tobias-tengler
tobias-tengler merged commit aa09c3e into main Oct 13, 2026
158 checks passed
@tobias-tengler
tobias-tengler deleted the tte/fusion-utf8-input-field-lookup branch October 13, 2026 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants