junrushao opened a new issue, #547:
URL: https://github.com/apache/tvm-ffi/issues/547

   ## Summary
   
   `tvm_ffi.Map.get()` on a key miss is **~100,000x slower** on the first call 
on **aarch64** (Grace Hopper) due to DWARF unwinding table parsing triggered by 
the `try/catch` pattern in `MapGetItemOrMissing`.
   
   ## Reproducer
   
   ```python
   import time, tvm_ffi
   
   m = tvm_ffi.Map({"a": 1})
   
   t0 = time.perf_counter()
   m.get("nonexistent")
   print(f"1st miss: {(time.perf_counter()-t0)*1000:.0f} ms")  # ~780ms on 
aarch64
   
   t0 = time.perf_counter()
   m.get("nonexistent")
   print(f"2nd miss: {(time.perf_counter()-t0)*1000:.3f} ms")  # ~0.07ms
   ```
   
   Output on aarch64 (Grace Hopper, Linux 6.14.0-1008-nvidia-64k):
   ```
   1st miss: 782 ms
   2nd miss: 0.075 ms
   ```
   
   ## Root Cause
   
   `MapGetItemOrMissing` in `src/ffi/container.cc:196` uses exception-based 
control flow for key misses:
   
   ```cpp
   .def("ffi.MapGetItemOrMissing",
        [](const ffi::MapObj* n, const Any& k) -> Any {
          try {
            return n->at(k);          // throws on miss
          } catch (const tvm::ffi::Error& e) {
            return GetMissingObject(); // caught here
          }
        })
   ```
   
   On aarch64, C++ exceptions use DWARF-based unwinding. The first `throw` in a 
process triggers lazy parsing of `.eh_frame` / `.eh_frame_hdr` sections across 
all loaded shared libraries. This is a one-time cost but it's ~800ms on a 
typical tilus installation (which loads `libtvm_ffi.so`, CUDA libraries, torch, 
etc.).
   
   For comparison, `Map.__contains__` uses `count()` (no exception) and takes 
0.014ms even on the first call.
   
   ## Impact
   
   In the tilus compilation pipeline, `bound_aware_simplify` calls `Map.get()` 
on `Analysis` metadata maps (which are `frozendict` stored in `@py_class` 
fields, converted to `tvm_ffi.Map` by the FFI layer). The first key miss adds 
~800ms to the Tilus IR optimization phase. We are currently working around this 
by converting to Python `dict` before lookups, but the upstream fix would 
benefit all `Map.get()` callers.
   
   ## Environment
   
   - Platform: aarch64 (NVIDIA Grace Hopper)
   - Python: 3.13
   - tvm-ffi: 0.1.11.dev19+g2987899a5%


-- 
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]

Reply via email to