It Takes Two to Tango: Accelerating Code Generation via Cache-Inspired Hierarchical Speculative Decoding
Open Access
- 7 October 2026
- conference paper
- Published by Association for Computing Machinery (ACM)
- p. 1998-2010
Abstract
While Large Language Models (LLMs) have demonstrated exceptional performance in code generation, their autoregressive decoding paradigm typically results in low inference efficiency. Speculative decoding has emerged as a solution to speed up inference through its draft–verification mechanism, where multiple draft tokens are generated efficiently and then verified in parallel by LLMs. Current speculative decoding approaches for code generation are broadly categorized into two types: model-based approaches that generate drafts using a lightweight draft model, and retrieval-based approaches that reuse code snippets from external corpora. In this work, we first conduct empirical experiments to analyze the strengths and weaknesses of retrieval-based and model-based drafting. We find that retrieval-based approach is efficient but usually low-quality, while model-based one is high-quality but the drafting time increases with inference steps of the draft model. Inspired by the cache mechanism in computer architecture, we propose a novel framework CacheCoder. In our design, retrieval acts as a high-speed Level-1 (L1) cache for rapid draft acquisition, while the draft model functions as a more capable Level-2 (L2) cache to compensate for L1 misses, enabling us to capitalize on the speed of reuse when possible and rely on the precision of generation when necessary. To combine these two hierarchical processes, we propose two customized strategies, i.e., Preliminary Prediction and Draft Expansion, which utilize the draft model to pre-filter retrieved contents and expand them with tokens generated by the draft model to form drafts with higher length. Experimental results show that CacheCoder achieves up to a 2.64× speedup compared to autoregressive decoding, significantly outperforming state-of-the-art approaches.Keywords
This publication has 50 references indexed in Scilit:
- FastCoder: Accelerating Repository-level Code Generation via Efficient Retrieval and Verification2025
- Fixing Large Language Models' Specification Misunderstanding for Better Code Generation2025
- Fusing Code SearchersIEEE Transactions on Software Engineering, 2024
- SpecInfer: Accelerating Large Language Model Serving with Tree-based Speculative Inference and Verification2024
- A Survey on Evaluation of Large Language ModelsACM Transactions on Intelligent Systems and Technology, 2024
- An empirical study of code reuse between GitHub and stack overflow during software developmentJournal of Systems and Software, 2024
- Natural Language to Code: How Far Are We?2023
- Two Birds with One Stone: Boosting Code Generation and Code Search via a Generative Adversarial NetworkProceedings of the ACM on Programming Languages, 2023
- On the naturalness of softwareCommunications of the ACM, 2016
- Efficient Global Optimization of Expensive Black-Box FunctionsJournal of Global Optimization, 1998