Abstract
Pre-trained Large Language Models (PLLMs) for code are computationally intensive, with resource demands scaling alongside input length. Code simplification—selectively removing unimportant tokens—is a key strategy for improving efficiency. We present LEANCODE, a framework that leverages task-specific, context-aware attention scores to identify and prune redundant code. By grouping tokens by statement category and utilizing task-relevant attention (CLS for classification, Encoder-Decoder for generation), LEANCODE significantly outperforms existing rule-based and global-average methods. Our evaluation on the CodeSearchNet Java corpus shows performance gains of up to 60% in code search and 29% in summarization over state-of-the-art baselines, while reducing inference time by up to 40% with minimal accuracy loss.
Introduction
The deployment of powerful PLLMs like CodeBERT and CodeT5 is often limited by high computational costs. Code simplification aims to mitigate this by reducing the input size. However, traditional methods like DietCode (global attention averages) and SlimCode (human-defined rules) often fail to capture the context-dependent importance of code tokens. LEANCODE addresses these limitations by aligning simplification with the model's internal attention mechanisms within specific syntactic contexts.
Methodology
LEANCODE introduces three technical shifts to improve code pruning:
1. Context-Aware Attention
Instead of a single global score, LEANCODE calculates "category-local" averages by grouping tokens based on the statement type they appear in (e.g., If Condition, Method Signature). This captures how a token's importance shifts depending on its structural role.
2. Task-Specific Scoring
The framework tailors its metrics to the model architecture:
- Classification (e.g., Code Search): Uses the attention scores of the
[CLS]token, which represents the model's final decision vector. - Generation (e.g., Code Summarization): Uses Encoder-Decoder attention scores to identify which input parts the model prioritizes during text generation.
3. Greedy Removal
Tokens are ranked by their context-aware scores and iteratively removed until a target simplification ratio is reached.
Key Insights: The Importance of Context
A core finding is that token importance is highly variable. Relying on global averages ignores the fact that a keyword's utility depends on its surroundings.
By focusing on what the model actually "attends" to, LEANCODE identifies "computational dead weight" that human-defined rules might overlook. Furthermore, the [CLS] token acts as a natural filter, allowing LEANCODE to ignore noise that the model was already internally disregarding.
Experimental Results
Evaluated on the Java subset of the CodeSearchNet Corpus, LEANCODE demonstrated clear superiority:
- Performance Gains: LEANCODE improved results by up to 60% and 16% for code search, and 29% and 27% for code summarization, compared to DietCode and SlimCode respectively.
- Efficiency: Removing 50% of input tokens reduced inference time by approximately 37% to 40%.
- Robustness: Even with 50% of the code removed, the performance drop was remarkably small (~5.5% for code search), proving much of the input is redundant for these tasks.
- Transferability: Code simplified using smaller models (e.g., CodeT5) remained effective when processed by massive models like GPT-4o, suggesting a shared cross-model understanding of code importance.
Conclusion
LEANCODE provides a model-driven approach to code simplification that significantly enhances efficiency without sacrificing substantial performance. Future work will extend this framework to other programming languages and tasks such as vulnerability detection and automated bug fixing.
Summary updated