Abstract
Code clone is a serious problem in software and has the potential to software defects, maintenance overhead, and licensing violations. Therefore, clone detection is important for reducing maintenance effort and improving code quality during software evolution. A variety of clone detection techniques have been proposed to identify similar code in software. However, few of them can efficiently detect semantic clones (functionally similar code without any syntactic resemblance). Recently, several deep learning based clone detectors are proposed to detect semantic clones. However, these approaches have high cost in data labelling and model training. In this paper, we propose a novel approach that leverages word embedding and ensemble learning techniques to detect semantic clones. Our evaluation on a commonly used clone benchmark, BigCloneBench, shows that our approach significantly improves the precision and recall of semantic clone detection, in comparison to a token-based clone detector, SourcererCC, and another deep learning based clone detector, CDLH.
Introduction
In software engineering, "code clones"—duplicated code fragments—are a double-edged sword. While copying and pasting code can speed up development, it creates significant maintenance overhead: a bug found in one fragment must be manually fixed in every clone. Clones are generally categorized into four types, ranging from exact syntactic copies (Type-1) to semantic clones (Type-4). Type-4 clones share functionality but have no syntactic resemblance, making them the most challenging to detect.
Existing tools like SourcererCC excel at syntactic clones but fail at Type-4. Recent deep learning approaches show promise but are computationally expensive and require massive labeled datasets. This paper introduces a highly efficient approach to detecting semantic clones by combining Doc2Vec word embeddings with a Random Forest ensemble classifier, achieving a 96% F1-score on Type-4 clones while being significantly faster to train than complex deep learning models.
Methodology
The proposed framework moves away from heavy Recurrent Neural Networks (RNNs) in favor of robust feature engineering and ensemble learning. The process is divided into a model building phase and a prediction phase.
- Feature Engineering (Doc2Vec): Instead of treating code as a rigid tree, it is treated as a document. The Skip-gram model is used to transform code fragments into 100-dimensional feature vectors. This captures the "context" of tokens, which often serves as a better proxy for intent than raw syntax.
- Handling Class Imbalance: To address the fact that "non-clone" pairs vastly outnumber "clone" pairs in real-world data, over-sampling is used to balance the training set.
- Ensemble Learning (Random Forest): A Random Forest classifier is employed to rapidly isolate discriminative features. The use of bagging reduces variance, making the detector stable across different software projects.
Experimental Setup
The study utilized BigCloneBench, the industry-standard benchmark for clone detection. It contains over 8 million true clone pairs and 250,000 false clone pairs across 10 representative functionalities, such as Bubble Sort and Secure Hash. Its inclusion of a massive amount of "Weakly Type-3" and "Type-4" clones makes it an ideal stress test for semantic detection.
Key Insights
- Semantic Context over Structure: A surprising insight is that Doc2Vec outperformed AST-based LSTMs. Since Type-4 clones do not share structure by definition, treating code as a semantic "document" captures functional intent that structural models miss.
- Efficiency and Scalability: While deep learning models like CDLH can take over a day to train, the Random Forest approach required only 6 hours for training and 1.6 hours for predicting millions of pairs, making it viable for daily CI/CD integration.
Results
The approach was compared against SourcererCC (token-based) and CDLH (deep learning). The proposed method achieved a Precision of 99%, a Recall of 92%, and an F1-score of 96.3% for Type-4 clones. As shown in the performance comparison, the ensemble approach maintains high recall across all clone types, whereas SourcererCC's performance drops significantly for semantic clones.
Conclusion
While highly effective, the model can still be challenged by code that appears similar at a low level but possesses different high-level logic. Future work will explore different ensemble configurations like Boosting and the incorporation of code comments into feature vectors to further enhance the distinction between similar-looking operations.
Summary updated