Skip to content

Commit a1bae73

Browse files
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_01KDR9eBZ4vEAftFBQ2vuhmr
1 parent b99eb71 commit a1bae73

2 files changed

Lines changed: 518 additions & 1 deletion

File tree

subprojects/capstone-v5.wrap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ url = https://github.com/capstone-engine/capstone.git
33
revision = 0bd647456e57ae17796937b9f296ef8bc1201b9e
44
patch_directory = capstone-v5
55
directory = capstone-v5
6-
diff_files = capstone-v5/capstone-patches/fix-x86-16.patch
6+
diff_files = capstone-v5/capstone-patches/fix-x86-16.patch, capstone-v5/capstone-patches/optimize-x86-lookup-tables.patch
77
depth = 1

0 commit comments

Comments
 (0)