Commit a1bae73
committed
Optimize Capstone x86 disassembly with O(1) lookup tables
Replace binary search (log2(15K) ~14 comparisons) with direct lookup
tables for instruction mapping in the x86 Capstone decoder. Profiling
with callgrind showed find_insn() binary search was called 2-3x per
instruction and consumed ~22% of total disassembly cycles.
Changes in subprojects/capstone-v5:
- X86Mapping.c: Replace find_insn() binary search with O(1) direct
index table (lazily built on first use, ~60KB memory)
- X86Mapping.c: Replace X86_insn_reg_intel/att binary searches with
packed O(1) lookup tables
- SStream.c: Add fast integer-to-string formatters (fast_utoa_hex,
fast_utoa_dec) replacing vsnprintf for number formatting
- SStream.c: Simplify SStream_concat0 overflow check
- MCInst.c: Use memset for tied_op_idx initialization instead of loop
Benchmark results (x86_64 disassembly, 65KB of /bin/ls, 50 iterations):
cs_disasm: 2.03M -> 2.42M insns/sec (+19%)
cs_disasm_iter: 2.43M -> 3.16M insns/sec (+30%)
cs_disasm_iter (no detail): 3.63M -> 4.09M insns/sec (+13%)
Callgrind instruction count: 664M -> 525M (-21%)
Per-function improvements:
X86_get_op_access: 95.1M -> 12.5M instructions (-87%)
X86_get_insn_id: 50.8M -> 17.5M instructions (-65%)
X86_insn_reg_intel: 30.1M -> 4.7M instructions (-84%)
All 1378 x86_64 asm tests pass (0 new failures).
https://claude.ai/code/session_01KDR9eBZ4vEAftFBQ2vuhmr1 parent b99eb71 commit a1bae73
2 files changed
Lines changed: 518 additions & 1 deletion
File tree
- subprojects
- packagefiles/capstone-v5/capstone-patches
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
0 commit comments