[Paper Review] Source Forager: A Search Engine for Similar Source Code
Source Forager is a code search engine that retrieves semantically and syntactically similar C/C++ functions by analyzing multiple code feature-classes—such as control flow, data dependencies, and AST structures—using a unified vector-based similarity model. It achieves high accuracy in ranking relevant code even in large databases with few relevant results, outperforming text-based search and enabling better developer productivity in code completion and debugging tasks.
Developers spend a significant amount of time searching for code: e.g., to understand how to complete, correct, or adapt their own code for a new context. Unfortunately, the state of the art in code search has not evolved much beyond text search over tokenized source. Code has much richer structure and semantics than normal text, and this property can be exploited to specialize the code-search process for better querying, searching, and ranking of code-search results. We present a new code-search engine named Source Forager. Given a query in the form of a C/C++ function, Source Forager searches a pre-populated code database for similar C/C++ functions. Source Forager preprocesses the database to extract a variety of simple code features that capture different aspects of code. A search returns the $k$ functions in the database that are most similar to the query, based on the various extracted code features. We tested the usefulness of Source Forager using a variety of code-search queries from two domains. Our experiments show that the ranked results returned by Source Forager are accurate, and that query-relevant functions can be reliably retrieved even when searching through a large code database that contains very few query-relevant functions. We believe that Source Forager is a first step towards much-needed tools that provide a better code-search experience.
Motivation & Objective
- To address the limitations of text-based code search, which often misses relevant code due to ignoring syntax and semantics.
- To unify the specification of code semantics in queries and corpus elements using a consistent feature-observation framework.
- To enable extensibility by allowing new feature-classes to be added without redesigning the core search mechanism.
- To automatically select or learn optimal feature-class weights for different query domains, improving search relevance without prior knowledge.
- To provide a scalable, accurate, and developer-friendly code search experience that supports program repair and synthesis.
Proposed method
- Preprocesses a C/C++ code corpus to extract multiple feature-classes (e.g., control flow, data dependencies, comments, AST patterns) into vectorized feature-observations.
- Uses a vector-based similarity model that combines multiple feature-classes to compute overall code similarity between query and corpus functions.
- Employs a dynamic feature-weight selection mechanism (dyn-select) to identify the most relevant feature-classes for a given query without prior domain knowledge.
- Leverages supervised learning (SVM) on labeled training data to pre-compute optimal feature-class weights for specific domains.
- Applies a similarity-based neighbor search (e.g., k-NN) to retrieve the k most similar functions from the database.
- Uses a hybrid query mechanism where the query is a code fragment, and similarity is computed using shared feature-extraction techniques on both query and corpus elements.
Experimental results
Research questions
- RQ1Can a unified, feature-based approach improve code search accuracy beyond traditional text-based methods?
- RQ2How effective is the dynamic feature-weight selection (dyn-select) mechanism in identifying relevant feature-classes without prior domain knowledge?
- RQ3To what extent do multiple feature-classes collectively improve retrieval performance compared to using individual features?
- RQ4How well does the SVM-based weight learning technique generalize across different code domains?
- RQ5How does Source Forager scale in terms of retrieval accuracy when the number of distractor functions increases?
Key findings
- Source Forager achieved a mean average precision (MAP) score of 0.96 on the libc-qs query set using the svm-weights configuration, indicating high retrieval accuracy.
- The dyn-select configuration achieved a MAP of 0.95 on the same libc-qs set, demonstrating strong performance without requiring labeled training data.
- When the number of distractor functions increased from 100 to 204,800, the MAP score remained above 0.70, showing robustness in large-scale codebases.
- The combination of multiple feature-classes (e.g., control flow, data dependencies, comments) significantly outperformed individual feature-classes, with the 'cross-weights' configuration achieving the highest overall MAP.
- The 'equal-all' configuration, which assigns equal weights to all feature-classes, still achieved a MAP of 0.84, indicating that even unweighted multi-feature search improves over single-feature or text-based methods.
- The study shows that feature-classes such as 'Type–Operation Coupling' and 'Control-Flow Graph' contribute significantly to similarity ranking, while others like 'Comments' have lower impact but still improve overall results.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.