CodeShrink: Adaptive Visual Compression for Efficient Multimodal Code Understanding

Wenxin Tang, Jingyu Xiao, Zhenyu Liu, Zipeng Xie, Junliang Liu, Wang Luo, Yuan Jiang, Yintong Huo, Michael LyuarXiv 2026

TL;DR

CodeShrink is an adaptive visual compression framework that optimizes how Multimodal Large Language Models (MLLMs) process source code rendered as images. By eliminating layout whitespace, dynamically tuning image resolution per sample, and pruning task-irrelevant visual tokens, it reduces token costs by up to 71.2% while matching or exceeding the performance of uncompressed text inputs.

What Problem Does This Paper Address?

Large Language Models (LLMs) excel at code intelligence tasks, but source code is inherently long, repetitive, and structurally sparse. When code is fed into models as text, formatting like indentation and line breaks consumes massive amounts of tokens, driving up inference costs and hitting context length limits.

Recent alternative approaches render source code into images to process them with MLLMs, allowing continuous resolution scaling to manage token budgets. However, standard code images are highly inefficient: line breaks and indentations leave massive blank spaces that waste visual tokens on empty pixels. Furthermore, a one-size-fits-all resolution strategy fails because different code snippets and tasks require entirely different trade-offs between visual clarity and token efficiency.

Methods

CodeShrink introduces an adaptive visual compression framework consisting of three complementary components:

  1. Blank-Free Rendering (BFR): Instead of preserving literal whitespace, BFR flattens the 2D code layout into a dense format. It replaces indentations and line breaks with compact structural markers (like <INDENT> and <DEDENT>). This eliminates layout-induced blank tokens and maximizes font readability within a tight token budget.
  2. Dominant Token Selection (DTS): During inference, CodeShrink uses edge and color gradients to separate foreground code characters from background pixels. It then leverages the MLLM's early attention layers to dynamically prune visual tokens that are weakly related to the specific user instruction.
  3. Adaptive Compression Configuration (ACC): The optimal resolution varies by sample. CodeShrink employs a lightweight configuration agent (a tiny 0.8B MLLM trained via Reinforcement Learning) to analyze the input and predict a tailored, per-sample scale and pruning ratio.
The CodeShrink Framework

Datasets

The authors evaluated CodeShrink across Python and Java benchmarks using three representative code intelligence tasks:

  • Code Question Answering: Evaluated using a QA dataset proposed by CodeOCR.
  • Code Clone Detection: Evaluated using GPTCloneBench (focusing on challenging Type-4 semantic clones).
  • Code Completion: Evaluated using the LongCodeCompletion dataset.

Key Insights and Ideas

  • Visual redundancy is task-specific: Not all code characters are equally important for every prompt. For a specific QA task, the model's attention naturally concentrates on specific conditional branches; DTS exploits this by safely discarding the remaining task-irrelevant visual tokens.
  • Macro vs. Micro task demands: Different tasks have completely opposite resolution preferences. Code Clone Detection actually improves at lower resolutions (up to 8x compression) because the model can focus on macro-structural similarities rather than getting bogged down in character details. Conversely, Code QA and Completion require high resolution (1x) to preserve fine-grained syntax.
  • Layout compaction preserves legibility: Removing empty space allows the renderer to use larger fonts under strict token budgets, making aggressive compression viable without losing character readability.
Fig. 1. Preliminary study of code image compression. (a) Original code rendering contains substantial blank regions, shown in gray. (b) Attention visualization shows that task-relevant code regions receive higher attention than other regions. (c) The preferred scale ratio varies across code completion samples. (d) Dynamic selection (DS) surpasses the best fixed scale ratio across all three tasks. Code QA and code clone detection are evaluated by accuracy, and code completion is evaluated by edit similarity (ES).

Key Results

  • Superior Token Savings: CodeShrink cuts visual token usage by up to 71.2% while consistently outperforming text-based compression (e.g., LLMLingua) and fixed-ratio visual pruning methods.
  • Exceeding Text-Only Performance: On Python Code QA, CodeShrink achieved 82.3% accuracy, surpassing the uncompressed text-only baseline (81.0%) while reducing visual tokens by roughly 40%.
  • Model-Agnostic Benefits: The Blank-Free Rendering (BFR) module acts as a plug-and-play upgrade. When tested on black-box models like GPT-5-mini and Gemini-3-Flash, BFR consistently improved performance over vanilla rendering, especially on long-context code completion tasks where vanilla rendering suffers from shrinking font sizes.

Limitations and Future Work

  • Task Scope: The framework was only validated on three core code intelligence tasks (QA, clone detection, and completion). Its efficacy on broader software engineering tasks, such as repository-level refactoring or vulnerability detection, remains to be explored.
  • Language Generalization: Evaluation was restricted to Python and Java. Future work is needed to validate CodeShrink on a wider variety of programming languages and larger, multi-file real-world software repositories.

Summary updated

BibTeX
@misc{tang2026codeshrink,
  author = {Wenxin Tang and Jingyu Xiao and Zhenyu Liu and Zipeng Xie and Junliang Liu and Wang Luo and Yuan Jiang and Yintong Huo and Michael Lyu},
  title = {{CodeShrink: Adaptive Visual Compression for Efficient Multimodal Code Understanding}},
  year = {2026},
  eprint = {2607.29637},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2607.29637},
}
Figure