tqchen opened a new pull request, #599: URL: https://github.com/apache/tvm-ffi/pull/599
## Summary Optimize `Expected<T>` so the happy paths compile down to a handful of instructions. The previous implementation routed `is_ok` / `is_err` through `as<Error>()` (which refcount-bumped the Error tag just to test type), and `value()` went through `cast<T>()` which performed a redundant runtime type check on every read. ## Changes - `is_ok()` / `is_err()` read the type-index discriminator directly. No indirection. - `value()` and `error()` use `TVM_FFI_PREDICT_TRUE/FALSE` to hint the happy path; the type-traits read uses the `*AfterCheck` variants since the `Expected` invariant already guarantees the held type. - Add `TVM_FFI_UNSAFE_ASSUME` macro (alongside `TVM_FFI_PREDICT_*`) expanding to `__builtin_assume` / `__assume`. Applied inside non-Object `CopyFromAnyViewAfterCheck` to hint the compiler about the type-index invariant; serves as semantic documentation and defence-in-depth for older toolchains. - Add three codegen probes under `tests/cpp/bench/` (compile-only; not built by CMake) plus a README. These provide a regression target for future changes. ## Codegen (clang++-17 -O2, lines of asm, lower is better) | Probe | Before | After | |---|---|---| | is_ok / is_err (int) | 173 | 5 | | value() lvalue (int) | 696 | 37 | | value() rvalue (int) | 725 | 34 | | value() rvalue (String) | 810 | 96 | | value() rvalue (Array<int>) | 1763 | 107 | | make_ok().value() | 738 | 50 | | std::move(make_ok()).value() | 738 | 50 | `make_ok().value()` compiles bit-identical to `std::move(make_ok()).value()` — the prvalue temporary materializes an xvalue, binding the `&&` overload directly. No `std::move` needed at the call site. -- 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]
