Skip to main content
QUICK REVIEW

[Paper Review] GraphCoder: Enhancing Repository-Level Code Completion via Code Context Graph-based Retrieval and Language Model

Wei Liu, Ailun Yu|arXiv (Cornell University)|Jun 11, 2024
Software Engineering ResearchComputer Science3 citations
TL;DR

GraphCoder enhances repository-level code completion by using a code context graph (CCG) to model control-flow and data/control dependencies for more accurate, structured retrieval of relevant code snippets. It achieves +6.06 in code exact match and +6.23 in identifier exact match over baselines, with reduced retrieval time and space usage via a coarse-to-fine graph-based retrieval process combined with pre-trained language models.

ABSTRACT

The performance of repository-level code completion depends upon the effective leverage of both general and repository-specific knowledge. Despite the impressive capability of code LLMs in general code completion tasks, they often exhibit less satisfactory performance on repository-level completion due to the lack of repository-specific knowledge in these LLMs. To address this problem, we propose GraphCoder, a retrieval-augmented code completion framework that leverages LLMs' general code knowledge and the repository-specific knowledge via a graph-based retrieval-generation process. In particular, GraphCoder captures the context of completion target more accurately through code context graph (CCG) that consists of control-flow, data- and control-dependence between code statements, a more structured way to capture the completion target context than the sequence-based context used in existing retrieval-augmented approaches; based on CCG, GraphCoder further employs a coarse-to-fine retrieval process to locate context-similar code snippets with the completion target from the current repository. Experimental results demonstrate both the effectiveness and efficiency of GraphCoder: Compared to baseline retrieval-augmented methods, GraphCoder achieves higher exact match (EM) on average, with increases of +6.06 in code match and +6.23 in identifier match, while using less time and space.

Motivation & Objective

  • To address the limitation of code LLMs in repository-level code completion due to lack of repository-specific knowledge.
  • To improve retrieval accuracy of relevant code snippets by modeling code context using structured code context graphs (CCGs) instead of sequence-based representations.
  • To reduce retrieval overhead and improve efficiency by leveraging statement-level graph indexing and decay-with-distance structural similarity.
  • To enhance code generation quality by integrating repository-specific context into LLM-based completion via retrieval-augmented generation (RAG).
  • To evaluate the effectiveness and efficiency of GraphCoder across diverse repositories and programming languages, particularly under low code duplication conditions.

Proposed method

  • Constructs a code context graph (CCG) as a statement-level multi-graph with three edge types: control flow, data dependence, and control dependence.
  • Employs a coarse-to-fine retrieval strategy: first filtering candidates based on sequence similarity, then re-ranking using decay-with-distance structural similarity to prioritize semantically relevant statements.
  • Uses a decay-with-distance measure to weight the importance of context statements based on their structural distance from the completion target.
  • Integrates retrieved code snippets into the prompt of pre-trained code LLMs (e.g., GPT-3.5, StarCoder, CodeGen) for code completion generation.
  • Stores and indexes code at the statement level rather than line level, reducing storage and improving retrieval efficiency.
  • Applies a unified prompt template across all methods to ensure fair comparison in token consumption and generation overhead.

Experimental results

Research questions

  • RQ1How does GraphCoder’s graph-based retrieval compare to sequence-based retrieval in terms of code completion accuracy?
  • RQ2To what extent does the use of code context graphs (CCGs) improve retrieval effectiveness and efficiency in repository-level code completion?
  • RQ3How does GraphCoder perform under varying levels of code duplication in repositories?
  • RQ4What is the impact of structural similarity (decay-with-distance) on retrieval quality and generation accuracy?
  • RQ5How does GraphCoder compare in terms of computational overhead (tokens, time, space) to existing RAG-based code completion methods?

Key findings

  • GraphCoder achieves a +6.06 absolute improvement in code exact match and +6.23 in identifier exact match compared to baseline RAG methods.
  • The framework reduces retrieval time and storage space usage by using statement-level graph indexing instead of line-level storage.
  • GraphCoder consumes fewer input and output tokens than RepoCoder and other RAG baselines, with no significant increase in token usage despite higher retrieval accuracy.
  • The decay-with-distance structural similarity measure improves retrieval quality by prioritizing semantically relevant, structurally proximate statements to the completion target.
  • GraphCoder maintains strong performance even in repositories with low code duplication, though recall decreases when duplication is very low, as shown in ablation studies.
  • The method generalizes well to different programming languages (Python and Java) and can be extended to others via tree-sitter-based AST parsing for CCG construction.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.