CodeComp incorporates Code Property Graph (CPG) priors from Joern into KV cache compression for code tasks — span-level structural protection preserves call sites, branch predicates, and return statements that attention-only methods systematically mis-prune, recovering 91% of full-context accuracy at 60% KV retention on bug localization while adding negligible latency overhead.
Agentic code tasks (fault localization, patch generation) process repository-scale contexts where KV cache consumes up to 70% of GPU memory. Existing compression methods (StreamingLLM, H2O, SnapKV, ChunkKV, ParallelComp) all share a common design assumption: token importance is inferred from attention scores.
This assumption breaks down for source code due to two challenges:

Paper's Figure 1, verbatim (caption: "Attention-only compression discards structurally critical tokens such as function calls, branch predicates, and return statements. CodeComp uses CPG priors to explicitly preserve these tokens.").
The figure illustrates the core problem: in a locate_bug function, attention-only compression prunes fetch_files(repo), f.has_error(), get_trace(f), and return trace — exactly the tokens needed for bug localization.
CodeComp is a training-free, 4-step KV cache compression framework:
核心技术壁垒: The insight that for code, attention scores and program-structural importance are near-orthogonal (Jaccard = 0.0944), and that span-level protection dominates budget allocation in recovering accuracy. The ablation (Table 4) shows span-only achieves 0.617–0.783 accuracy while capacity-only achieves only 0.283–0.450. This means the primary bottleneck is retaining the right intra-chunk evidence, not identifying which chunk should receive more tokens.
Bug localization (Table 3, Qwen3-8B, cap=0.6):
Code generation (Table 2, SWE-bench Lite):
Ablation (Table 4):
Throughput: 112–118s stable latency across retention ratios — negligible Joern overhead.

Paper's Figure 3, verbatim (caption: "Overview of CodeComp, a structure-aware KV cache compression framework. Given a query and retrieved repository context, we first select relevant chunks via PPL-based scoring (Step 1). We then extract structural anchors from static program analysis using Joern and CPG (Step 2). These anchors are used to allocate chunk-level compression budgets and protect semantically critical spans (Step 3). Finally, attention-based compression fills the remaining capacity under these constraints (Step 4).").
The 4-step pipeline: PPL chunk selection → Joern CPG extraction (call/control/return/assign nodes) → structure-aware compression (budget allocation + span protection) → constrained attention-based KV selection. Structural priors determine the critical evidence that must be preserved; attention serves as a residual selector for remaining capacity.

Paper's Figure 2, verbatim (caption: "Motivation analysis for KV cache compression in code. (a) Attention–structure mismatch: attention-based importance is weakly aligned with CPG-derived structural importance. (b) Structural mis-pruning: under attention-only compression, a large fraction of critical structural tokens are incorrectly discarded. (c) Structure-aware retention: incorporating structural priors significantly improves the preservation of critical structural tokens.").
Three sub-analyses: (a) scatter plot showing weak alignment between attention and structural importance; (b) 52% of call sites, 42% of assignments pruned under attention-only compression; (c) structure-aware retention achieves 1.00 for callsite, branch, return, and signature tokens.
Within each chunk, span selection operates in two stages:
If protected tokens $|P_i| < B_i$, remaining budget filled with tokens nearest to $P_i$ in the original sequence — preserving local syntactic continuity around structurally important regions.
| Symbol | Meaning |
|---|---|
| $s_i^{\text{ppl}}$ | Perplexity score for chunk $i$ (lower = more relevant) |
| $N_{\text{call}}, N_{\text{control}}, N_{\text{return}}, N_{\text{assign}}$ | CPG node counts per chunk |
| $E_{\text{cfg}}, E_{\text{pdg}}$ | CPG edge counts (control-flow, program-dependency) |
| $s(z)$ | Span importance score (weighted combination of structural features) |
| $p^{\text{query}}(z)$ | Binary query-overlap indicator for span $z$ |
| $B_i^{\text{span}}$ | Span budget for chunk $i$ |
| $\rho_{\text{span}}$ | Span budget ratio (0.5) |
| $u(j)$ | Attention-based token importance score |
| $p_{\text{query}}$ | Starting position index for query tokens |
Setup: SGLang v0.4.9.post3. Llama-3-8B-Instruct, Qwen3-8B (bug localization); DS-Coder, Qwen-Coder (code generation). Benchmarks: InfiniteBench-CodeDebug, DebugBench, LongCodeQA, SWE-bench Lite, LCA CodeGen. Compression ratios: cap ∈ {0.4, 0.6}. Baselines: SnapKV, ParallelComp.

Paper's Figure 4, verbatim (caption: "(a) Feature ablation of span scoring. (b) End-to-end latency on SWE-bench Lite.").
Feature ablation (left) shows call and control-flow removal cause the most consistent degradation, confirming their role as primary structural signals. Latency (right) remains flat at 112–118s across all retention settings, demonstrating negligible Joern overhead in practice.
Key results summary:
| Task | Model | Cap | CodeComp | Best Baseline | Improvement |
|---|---|---|---|---|---|
| Bug localization (DebugBench) | Llama3-8B | 0.4 | 0.43 | 0.41 (SnapKV) | +5% |
| Bug localization (DebugBench) | Llama3-8B | 0.4 | 0.43 | 0.03 (ParallelComp) | 14× |
| Bug localization (avg) | Qwen3-8B | 0.6 | 0.53 | 0.50 (SnapKV) | +6% |
| Code generation (GF F1) | DS-Coder | 0.4 | 0.250 | 0.200 (SnapKV) | +25% |
| Code generation (GF F1) | DS-Coder | 0.4 | 0.250 | 0.021 (ParallelComp) | 12× |
| Code generation (GF F1) | Qwen-Coder | 0.6 | 0.613 | 0.700 (SnapKV) | -12% |
| LCA API F1 | DS-Coder | 0.6 | 0.234 | 0.142 (ParallelComp) | +65% |
Where CodeComp loses: Qwen-Coder SWE-bench at cap=0.6 — SnapKV achieves GF F1 = 0.700 vs CodeComp 0.613. SnapKV optimizes for localization F1, while CodeComp consistently achieves better generation fidelity (lower edit distance). On Qwen3-8B LongCodeQA at cap=0.4, SnapKV (0.69) > CodeComp (0.64). The two methods optimize for partially orthogonal objectives.
| Workload regime | CodeComp | Attention-only baseline | Why |
|---|---|---|---|
| Bug localization (structure-critical) | 91% full-context recovery | 40–86% (varies wildly) | Structural tokens essential for fault tracing |
| Code generation (GF localization) | 74–96% recovery | 6–110% (unstable) | Span protection preserves call/control tokens |
| Code generation (edit distance) | Matches uncompressed | Higher edit distance | Structure-aware compression preserves generation fidelity |
| File-level localization F1 only | Sometimes weaker than SnapKV | SnapKV can win | Attention-based observation window better for coarse localization |
| Step | Claim | Evidence | Depends on |
|---|---|---|---|
| 1 | Attention-based importance and program-structural importance are near-orthogonal for code | §3: Jaccard = 0.0944 between top-20% chunks ranked by each signal (Fig 2a) | — |
| 2 | This mismatch causes systematic mis-pruning of structurally critical tokens | §3: 52% callsite, 42% assignment tokens pruned (Fig 2b); ParallelComp collapses to 0.03 on DebugBench (Table 3) | Step 1 |
| 3 | CPG priors from Joern can identify structurally critical spans | §4.2: CPG unifies AST/CFG/PDG; extracted features ($N_{\text{call}}$, $N_{\text{control}}$, etc.) align with code semantics | — |
| 4 | Span-level protection is the dominant accuracy mechanism | §5.3: span-only (0.617–0.783) >> capacity-only (0.283–0.450); call + control-flow most critical (Fig 4a) | Steps 2, 3 |
| 5 | CodeComp recovers majority of full-context accuracy at reduced KV budget | §5.1–5.2: 91% recovery at cap=0.6 (Table 3); matches uncompressed edit distance (Table 5) | Step 4 |
Implemented on: SGLang v0.4.9.post3. Native integration (not Transformers library).
[实现未公开]
关键实现细节: