tlopex opened a new pull request, #692: URL: https://github.com/apache/tvm-ffi/pull/692
This PR optimizes both Rust `match_any!` dispatch paths without changing its matching semantics: - The ordered path uses an internal lightweight `Result<T, ()>` conversion, avoiding `TypeError` construction for failed arms. - The lookup path uses a fixed call-site `TypeIndex → ArmId` table without heap allocation or sorting. - Lookup starts at 16 exact-leaf arms, the first size showing a stable T1 improvement across multiple shuffle seeds. - Smaller, guarded, non-leaf, parameterized, and ineligible matches continue using source-ordered dispatch. - The scrutinee is still evaluated once, duplicate patterns still select the first source arm, and misses still use the fallback. - The public `TryFrom<AnyView>` API and custom `TryInto` matcher behavior remain unchanged. ### Rust and C++ conversion comparison | Steady-state O3 workload | Rust | C++ `ObjectRef::as<T>()` | | --- | ---: | ---: | | Single conversion hit | 8.44 ns | 9.23 ns | | Single conversion miss | 1.90 ns | 1.63 ns | | Two-arm chain, first arm | 8.45 ns | 9.57 ns | | Two-arm chain, second arm | 8.44 ns | 10.90 ns | | Two-arm chain, miss | 1.97 ns | 1.61 ns | The Rust lightweight conversion path is in the same expected range as the equivalent C++ conversion. ### T1 steady-state arity sweep | Arms | Uniform hits: Ordered | Lookup | Change | All outcomes: Ordered | Lookup | Change | | ---: | ---: | ---: | ---: | ---: | ---: | ---: | | 2 | 8.127 ns | 8.418 ns | -3.6% | 6.825 ns | 7.338 ns | -7.5% | | 4 | 7.981 ns | 8.662 ns | -8.5% | 8.669 ns | 9.228 ns | -6.4% | | 8 | 9.627 ns | 10.302 ns | -7.0% | 9.945 ns | 10.525 ns | -5.8% | | 12 | 10.627 ns | 11.664 ns | -9.8% | 10.816 ns | 11.835 ns | -9.4% | | 16 | 12.706 ns | 12.524 ns | +1.4% | 13.055 ns | 12.530 ns | +4.0% | | 18 | 13.765 ns | 12.719 ns | +7.6% | 14.290 ns | 12.732 ns | +10.9% | | 19 | 14.430 ns | 13.216 ns | +8.4% | 15.148 ns | 13.391 ns | +11.6% | | 20 | 15.280 ns | 13.272 ns | +13.1% | 16.046 ns | 13.476 ns | +16.0% | | 21 | 16.138 ns | 13.509 ns | +16.3% | 17.039 ns | 13.659 ns | +19.8% | Negative values indicate that lookup is slower. Sixteen arms is the first measured size at which lookup consistently wins for both T1 workloads. ### T0 steady-state at the 16-arm threshold | Workload | Ordered | Lookup | Lookup change | | --- | ---: | ---: | ---: | | First-arm hit | 8.418 ns | 8.637 ns | -2.6% | | Middle-arm hit | 7.917 ns | 7.932 ns | approximately equal | | Last-arm hit | 13.754 ns | 8.951 ns | +34.9% | | Miss | 12.101 ns | 6.003 ns | +50.4% | The small first-arm regression is the expected lookup overhead when the ordered branch is perfectly predictable. Later arms and misses benefit from avoiding the ordered conversion chain. ### Cold first-call dispatch | Arms | Last arm: Ordered | Lookup | Change | Miss: Ordered | Lookup | Change | | ---: | ---: | ---: | ---: | ---: | ---: | ---: | | 16 | 330 ns | 301 ns | +8.8% | 311 ns | 261 ns | +16.1% | | 18 | 371 ns | 331 ns | +10.8% | 361 ns | 291 ns | +19.4% | | 20 | 411 ns | 331 ns | +19.5% | 401 ns | 291 ns | +27.4% | Measurements used pinned-CPU O3 builds, preconstructed objects, hot loops, no-op baselines, and 31 rotated samples. The Rust and C++ lightweight conversion paths have comparable costs. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
