Hierarchical Context Pruning: Optimizing Real-World Code Completion with Repository-Level Pretrained Code LLMs

Lei Zhang,Yunshui Li,Jiaming Li,Xiaobo Xia,Jiaxi Yang,Run Luo,Minzheng Wang,Longze Chen,Junhao Liu,Min Yang
2024-06-27
Abstract:Some recently developed code large language models (Code LLMs) have been pre-trained on repository-level code data (Repo-Code LLMs), enabling these models to recognize repository structures and utilize cross-file information for code completion. However, in real-world development scenarios, simply concatenating the entire code repository often exceeds the context window limits of these Repo-Code LLMs, leading to significant performance degradation. In this study, we conducted extensive preliminary experiments and analyses on six Repo-Code LLMs. The results indicate that maintaining the topological dependencies of files and increasing the code file content in the completion prompts can improve completion accuracy; pruning the specific implementations of functions in all dependent files does not significantly reduce the accuracy of completions. Based on these findings, we proposed a strategy named Hierarchical Context Pruning (HCP) to construct completion prompts with high informational code content. The HCP models the code repository at the function level, maintaining the topological dependencies between code files while removing a large amount of irrelevant code content, significantly reduces the input length for repository-level code completion. We applied the HCP strategy in experiments with six Repo-Code LLMs, and the results demonstrate that our proposed method can significantly enhance completion accuracy while substantially reducing the length of input. Our code and data are available at <a class="link-external link-https" href="https://github.com/Hambaobao/HCP-Coder" rel="external noopener nofollow">this https URL</a>.
Computation and Language
What problem does this paper attempt to address?
### The Problem Addressed by This Paper This paper aims to address the issues encountered in real-world development scenarios when using repository-level pre-trained code large language models (Repo-Code LLMs) for code completion. Specifically: 1. **Context Window Limitation**: Simply concatenating the entire codebase as input often exceeds the context window size of these Repo-Code LLMs, leading to significant performance degradation. 2. **Improving Completion Accuracy**: Research has found that preserving the topological dependencies between files and including the content of code files in the completion prompts can improve completion accuracy; meanwhile, removing the specific implementations of functions in all dependent files does not significantly reduce completion accuracy. Based on these findings, the authors propose a method called Hierarchical Context Pruning (HCP) to construct high-quality completion prompts, thereby improving code completion accuracy without exceeding the model's context window limitation. Experimental results show that the HCP method can significantly enhance completion accuracy and greatly reduce input length.