Skip to content

Optimize NativeToValue call paths - #1400

Merged
TristonianJones merged 3 commits into
cel-expr:masterfrom
TristonianJones:native-types-opt
Aug 5, 2026
Merged

Optimize NativeToValue call paths#1400
TristonianJones merged 3 commits into
cel-expr:masterfrom
TristonianJones:native-types-opt

Conversation

@TristonianJones

Copy link
Copy Markdown
Collaborator

Consolidate the NativeToValue code blocks

Minor code cleanup on native.go to simplify reflected type handling.
Reflected values are always unwrapped to their non-pointer struct value,
simplifying comparisons and logic related to type testing.

- **Unified `NativeToValue` Dispatch**: Consolidated private `nativeToValue` and `Registry.NativeToValue` into a single authoritative receiver method. `DefaultTypeAdapter` delegates directly to `emptyRegistry.NativeToValue`.
- **Reflection & Allocation Reduction**:
  - Replaced dynamic `.Convert().Interface().(T)` reflection conversions for type aliases with direct zero-allocation primitive getters (`.Int()`, `.Uint()`, `.Float()`, `.Bool()`, `.String()`).
  - Integrated custom struct descriptor lookups directly into the reflection branch using `reflect.TypeOf(value)` for instant map matching.
  - Eliminated duplicate `reflect.ValueOf()` instantiations and redundant nil pointer checks across conversion paths.
---

| Benchmark Target | `master` Baseline | Refactored Branch | Delta | Allocs / Op |
| :--- | :---: | :---: | :---: | :---: |
| `proto/TestAllTypes` | 177.0 ns/op | **87.91 ns/op** | 🚀 **+50.3% faster** | 1 alloc |
| `nativeStruct/pointer` | 20.59 ns/op | **13.18 ns/op** | 🚀 **+36.0% faster** | 0 allocs |
| `nativeStruct/value` | 17.25 ns/op | **13.52 ns/op** | 🚀 **+21.6% faster** | 0 allocs |
| `ref.Val/String` | 4.81 ns/op | **2.62 ns/op** | 🚀 **+45.5% faster** | 0 allocs |
| `ref.Val/Int` | 3.73 ns/op | **2.69 ns/op** | 🚀 **+27.9% faster** | 0 allocs |
| `int/1` | 3.86 ns/op | **2.55 ns/op** | 🚀 **+33.9% faster** | 0 allocs |
| `bool/true` | 2.91 ns/op | **2.11 ns/op** | 🚀 **+27.5% faster** | 0 allocs |

| Benchmark Target | `master` Baseline | Refactored Branch | Delta |
| :--- | :---: | :---: | :---: |
| `nested_proto_field` | 141.3 ns/op | **124.7 ns/op** | 🚀 **+11.7% faster** |
| `nested_proto_field_with_index` | 577.5 ns/op | **540.6 ns/op** | 🚀 **+6.4% faster** |
| `index` | 122.3 ns/op | **108.1 ns/op** | 🚀 **+11.6% faster** |
| `index_list_int_uint_type_index` | 42.87 ns/op | **38.22 ns/op** | 🚀 **+10.8% faster** |
| `index_cross_type_float_uint` | 264.4 ns/op | **240.0 ns/op** | 🚀 **+9.2% faster** |
| `select_subsumed_field` | 14.27 ns/op | **13.05 ns/op** | 🚀 **+8.5% faster** |
| `select_custom_pb3_optional_field` | 43.96 ns/op | **41.71 ns/op** | 🚀 **+5.1% faster** |
| `complex_qual_vars` | 338.4 ns/op | **323.1 ns/op** | 🚀 **+4.5% faster** |
@TristonianJones
TristonianJones merged commit f10a2e6 into cel-expr:master Aug 5, 2026
8 checks passed
@TristonianJones
TristonianJones deleted the native-types-opt branch August 5, 2026 21:11
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.

2 participants