Introduction
Modern repository-level Large Language Models (LLMs) like DeepSeek-Coder and StarCoder2 are designed to leverage entire codebases for context. However, practical implementation faces significant hurdles: the "Context Window Wall," where repositories exceed token limits, and the "Lost in the Middle" effect, where excessive noise in long prompts degrades model performance and increases latency. Hierarchical Context Pruning (HCP) is a new strategy that optimizes repository-level code completion by intelligently "slimming down" the information sent to LLMs.
Methodology: Hierarchical Context Pruning (HCP)
To address these challenges, HCP treats a repository as a structured hierarchy rather than a flat list of files. Unlike traditional Retrieval-Augmented Generation (RAG) which treats code as plain text, HCP respects the structural topology of software repositories.
The HCP process involves three main steps:
- Fine-grained Modeling: Using
tree-sitter, the system parses the repository into Function, Class, and File nodes, allowing the pruner to act on specific code blocks. - Topological Dependency Analysis: The system identifies "Level 1" dependencies—files directly imported by the file currently being edited—which are prioritized as critical context.
- Hierarchical Pruning Strategy:
- Current File: Kept entirely intact and formatted using Fill-In-the-Middle (FIM) templates.
- Dependency Files: Global context (like docstrings) is removed, but function bodies are generally retained.
- Other Files: The system uses semantic search to find relevant functions but only keeps the function signatures (headers), discarding implementation details to save space.
Key Results
The researchers evaluated HCP using the CrossCodeEval benchmark across six prominent models: DeepSeek-Coder (1.3B/6.7B), StarCoder2 (3B/7B), and CodeGemma (2B/7B).
Performance and Efficiency
- Accuracy Boost: HCP outperformed previous state-of-the-art methods on five out of the six models tested. For example, on CodeGemma-7B, HCP improved the Exact Match (EM) score from 36.69% to 41.21%.
- Massive Compression: HCP successfully compressed repository context from an average of 50,000 tokens down to approximately 8,000 tokens without losing critical information. This removal of "distractor" code significantly reduced hallucinations.
- Increased Throughput: Due to the shorter prompts, the "Tokens per Second" (throughput) increased significantly across all models, making it much more viable for real-time IDE integration.
Conclusion
HCP demonstrates that for cross-file context, "less is more." By prioritizing function signatures over full implementations for distant files and respecting repository topology, HCP achieves higher accuracy with significantly lower computational overhead. Future work will focus on extending this approach to other programming languages and optimizing the integration of local embedding models to reduce external API reliance.
Summary updated