Table of Contents
Fetching ...

SECRET: Towards Scalable and Efficient Code Retrieval via Segmented Deep Hashing

Wenchao Gu, Ensheng Shi, Yanlin Wang, Lun Du, Shi Han, Hongyu Zhang, Dongmei Zhang, Michael R. Lyu

TL;DR

SECRET introduces segmented deep hashing to scale code retrieval by splitting long hash codes into multiple short segments and using per-segment hash tables for rapid recall. The method combines an initial hashing stage with an iterative training loop that includes hash segmentation, adaptive bits relaxing, and dynamic matching objective adjustment to align code-query hash codes while reducing false positives. Empirical results show SECRET achieves at least a 95% reduction in recall time and maintains or surpasses the accuracy of prior deep hashing baselines, outperforming LSH under the same hash-table count. The approach offers scalable, efficient code retrieval suitable for industrial-scale codebases, with robust performance across datasets and ablations, and provides practical implications for real-time code search in large repositories.

Abstract

Code retrieval, which retrieves code snippets based on users' natural language descriptions, is widely used by developers and plays a pivotal role in real-world software development. The advent of deep learning has shifted the retrieval paradigm from lexical-based matching towards leveraging deep learning models to encode source code and queries into vector representations, facilitating code retrieval according to vector similarity. Despite the effectiveness of these models, managing large-scale code database presents significant challenges. Previous research proposes deep hashing-based methods, which generate hash codes for queries and code snippets and use Hamming distance for rapid recall of code candidates. However, this approach's reliance on linear scanning of the entire code base limits its scalability. To further improve the efficiency of large-scale code retrieval, we propose a novel approach SECRET (Scalable and Efficient Code Retrieval via SegmEnTed deep hashing). SECRET converts long hash codes calculated by existing deep hashing approaches into several short hash code segments through an iterative training strategy. After training, SECRET recalls code candidates by looking up the hash tables for each segment, the time complexity of recall can thus be greatly reduced. Extensive experimental results demonstrate that SECRET can drastically reduce the retrieval time by at least 95% while achieving comparable or even higher performance of existing deep hashing approaches. Besides, SECRET also exhibits superior performance and efficiency compared to the classical hash table-based approach known as LSH under the same number of hash tables.

SECRET: Towards Scalable and Efficient Code Retrieval via Segmented Deep Hashing

TL;DR

SECRET introduces segmented deep hashing to scale code retrieval by splitting long hash codes into multiple short segments and using per-segment hash tables for rapid recall. The method combines an initial hashing stage with an iterative training loop that includes hash segmentation, adaptive bits relaxing, and dynamic matching objective adjustment to align code-query hash codes while reducing false positives. Empirical results show SECRET achieves at least a 95% reduction in recall time and maintains or surpasses the accuracy of prior deep hashing baselines, outperforming LSH under the same hash-table count. The approach offers scalable, efficient code retrieval suitable for industrial-scale codebases, with robust performance across datasets and ablations, and provides practical implications for real-time code search in large repositories.

Abstract

Code retrieval, which retrieves code snippets based on users' natural language descriptions, is widely used by developers and plays a pivotal role in real-world software development. The advent of deep learning has shifted the retrieval paradigm from lexical-based matching towards leveraging deep learning models to encode source code and queries into vector representations, facilitating code retrieval according to vector similarity. Despite the effectiveness of these models, managing large-scale code database presents significant challenges. Previous research proposes deep hashing-based methods, which generate hash codes for queries and code snippets and use Hamming distance for rapid recall of code candidates. However, this approach's reliance on linear scanning of the entire code base limits its scalability. To further improve the efficiency of large-scale code retrieval, we propose a novel approach SECRET (Scalable and Efficient Code Retrieval via SegmEnTed deep hashing). SECRET converts long hash codes calculated by existing deep hashing approaches into several short hash code segments through an iterative training strategy. After training, SECRET recalls code candidates by looking up the hash tables for each segment, the time complexity of recall can thus be greatly reduced. Extensive experimental results demonstrate that SECRET can drastically reduce the retrieval time by at least 95% while achieving comparable or even higher performance of existing deep hashing approaches. Besides, SECRET also exhibits superior performance and efficiency compared to the classical hash table-based approach known as LSH under the same number of hash tables.

Paper Structure

This paper contains 37 sections, 17 equations, 4 figures, 6 tables.

Figures (4)

  • Figure 1: Illustration of recall with previous deep hashing approaches.
  • Figure 2: Illustration of recall with the combination of deep hashing approaches and SECRET.
  • Figure 3: Overall framework of SECRET. Initial Hashing Generation Training: train the code hashing model and query hashing model with representation vectors from the code encoder and query encoder; Iterative Training: consists of two sub-training steps which are code hashing alignment and query hashing alignment. In the sub-training step of hashing alignment, one of the hashing model will be fixed and provide the training objective for training of the other hashing model. These two steps will be performed alternately until the model training converges.
  • Figure 4: Steps in the iterative training strategy. Hash Segmentation: split the long hash code into several segmented hash codes and convert continuous output value into the discrete value; Adaptive Bits Relaxing: select the hash bits from each segmented hash code according to the absolute output value from the model and overwrite the hash value as "unknown" on these hash bits, which represented as 0; Dynamic Matching Objective Adjustment: Assign the suitable matching objective for each pair of query and code snippet. The hash code from the positive sample will be utilized as the ground-truth label and adjusted according to the hash collision condition with the negative samples from the same batch.