LongCodeZip: Compress Long Context for Code Language Models
Yuling Shi, Yichun Qian, Hongyu Zhang, Beijun Shen, Xiaodong Gu
TL;DR
LongCodeZip addresses the challenge of long-context code understanding by introducing a training-free, model-agnostic two-stage compression framework tailored for code LLMs. It first performs coarse-grained function-level pruning via instruction-aware perplexity, then applies fine-grained intra-function pruning using perplexity-based block segmentation and adaptive budget allocation solved by 0/1 knapsack optimization. Across code completion, summarization, and code QA benchmarks, it achieves up to 5.6x compression without degrading task performance and demonstrates strong cross-model transferability, including to closed-source models. The approach significantly reduces input token costs and generation time, enabling code intelligence applications to scale to real-world, large-scale software projects.
Abstract
Code generation under long contexts is becoming increasingly critical as Large Language Models (LLMs) are required to reason over extensive information in the codebase. While recent advances enable code LLMs to process long inputs, high API costs and generation latency remain substantial bottlenecks. Existing context pruning techniques, such as LLMLingua, achieve promising results for general text but overlook code-specific structures and dependencies, leading to suboptimal performance in programming tasks. In this paper, we propose LongCodeZip, a novel plug-and-play code compression framework designed specifically for code LLMs. LongCodeZip employs a dual-stage strategy: (1) coarse-grained compression, which identifies and ranks function-level chunks using conditional perplexity with respect to the instruction, retaining only the most relevant functions; and (2) fine-grained compression, which segments retained functions into blocks based on perplexity and selects an optimal subset under an adaptive token budget to maximize relevance. Evaluations across multiple tasks, including code completion, summarization, and question answering, show that LongCodeZip consistently outperforms baseline methods, achieving up to a 5.6x compression ratio without degrading task performance. By effectively reducing context size while preserving essential information, LongCodeZip enables LLMs to better scale to real-world, large-scale code scenarios, advancing the efficiency and capability of code intelligence applications.
