Source Forager: A Search Engine for Similar Source Code

Vineeth Kashyap, David Bingham Brown, Ben Liblit, David Melski, Thomas W. RepsarXiv 2017

TL;DR

This paper introduces Source Forager, a novel search engine that finds similar C/C++ source code functions by leveraging a rich set of structural and semantic features, going beyond traditional text-based search. It demonstrates that combining these features, especially with domain-specific weighting, significantly improves search accuracy, even in large codebases with many irrelevant "distractor" functions.

What Problem Does This Paper Address?

Developers frequently need to search for existing code to understand, reuse, or adapt it. Current tools primarily rely on text-based search, which is often imprecise because it ignores the inherent structure and semantics of code. This can lead to missing relevant code snippets or being overwhelmed by irrelevant results. The paper aims to address this by creating a more sophisticated code search engine that can understand and compare code based on its deeper characteristics.

Methods

Source Forager employs a two-stage approach:

  1. Offline Phase (Database Population): A code corpus is preprocessed to extract various "feature-classes" for each function. These feature-classes capture different aspects of the code, such as:
  2. Structural: Skeleton Trees (loop/conditional structure), Control Flow Graph (CFG) subgraphs.
  3. Semantic: Type-Operation Coupling, Library Calls (modeled, unmodeled, user-defined), Type Signatures, Local Types, Numeric/String Literals, Comments.
  4. Natural Language: Weighted Natural Language Terms (function names, comments, variable names, etc., processed with TF-IDF). Each function is represented by a "feature-vector" containing observations for each feature-class. Similarity functions are defined for each feature-class.

  5. Online Phase (Query Search): A query function is also processed to generate its feature-vector. A combined similarity score is calculated for the query against each function in the database by taking a weighted average of the per-feature-class similarities. The top k most similar functions are returned.

The paper explores three main configurations for combining feature-classes:

  • equal-all: All feature-classes are given equal weight.
  • dyn-select: A subset of feature-classes is dynamically selected for each query based on their distinctiveness in the dataset.
  • svm-weights: Feature-class weights are pre-computed using a Support Vector Machine (SVM) trained on labeled similar/dissimilar code pairs from a specific domain.

Datasets

The paper uses two primary query ground-truth sets for evaluation:

  • algo-qs: Represents "algorithmic" code queries, comprising 38 functions curated from GitHub across seven task categories (e.g., Binary Search, Insertion Sort). These functions were manually vetted for similarity.
  • libc-qs: Represents "systems programming" code queries, derived from three implementations of the standard C library (musl libc, diet libc, uClibc). This set defines 88 function categories, with functions within a category considered similar.

Distractor functions were sourced from the Fedora source packages (SRPMs) within the MUSE corpus, totaling over 200,000 compilable code files. For most experiments, 10,000 distractors were used.

Key Insights and Ideas

  • Code is More Than Text: The core idea is that exploiting code's inherent structure and semantics is crucial for effective code search, moving beyond simple string matching.
  • Feature Diversity is Key: No single feature-class is universally best. Different feature-classes excel for different types of queries, highlighting the need for a multi-faceted approach. Weighted NL Terms showed strong performance individually, but combining features is superior.
  • Dynamic Selection Works: When domain knowledge is absent, dynamically selecting relevant feature-classes for each query (dyn-select) significantly outperforms using all features equally (equal-all) or random selection (rand-select). This suggests that not all features are always helpful.
  • Domain-Specific Tuning is Powerful: When the domain of the query is known, training feature-class weights using supervised learning (svm-weights) yields the best results, demonstrating the value of domain expertise.
  • Robustness to Noise: Source Forager maintains high performance even when the search space is dominated by irrelevant "distractor" functions, indicating its ability to pinpoint relevant code amidst noise.

Key Results

  • Individual Feature Performance: Weighted NL Terms performed best among solo feature-classes, achieving MAP scores of 0.70 (algo-qs) and 0.86 (libc-qs).
  • Combined Feature Performance:
    • dyn-select significantly outperformed equal-all (0.67, 0.73) and rand-select (0.57, 0.63), achieving MAP scores of 0.84 (algo-qs) and 0.89 (libc-qs).
  • svm-weights achieved the highest MAP scores: 0.86 (algo-qs) and 0.95 (libc-qs), outperforming all other configurations.
  • Distractor Impact: MAP scores declined as the number of distractor functions increased, but remained resilient, with relevant sets containing only a few items competing against distractors orders of magnitude larger.
  • Performance: The Pliny-DB implementation can search a database of 500,000 functions in under 2 seconds.

Limitations and Future Work

  • Threats to Validity: The authors acknowledge that the appropriateness of evaluation benchmarks is a potential threat. They mitigate this by using two distinct domains, verifying that similar functions are not trivial clones (using MOSS), and sourcing data from real-world code.
  • Future Work: The paper suggests that Source Forager's architecture is extensible and not limited to C/C++ or functions as program elements. They also mention ongoing work to develop a distributed version of Pliny-DB for searching larger codebases more efficiently. The authors also note that the dyn-select configuration's runtime overhead could be reduced by parallelizing the selection process.

Summary updated

BibTeX
@misc{kashyap2017source,
  author = {Vineeth Kashyap and David Bingham Brown and Ben Liblit and David Melski and Thomas W. Reps},
  title = {{Source Forager: A Search Engine for Similar Source Code}},
  year = {2017},
  eprint = {1706.02769},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1706.02769},
}
Figure