一句话:大规模实证表明移除代码格式化元素可平均减少 24.5% 输入 token,而 LLM 补全性能几乎不受影响。
源代码中的缩进、换行等格式化元素是为人类可读性服务的,但对按线性 token 序列处理代码的大语言模型(LLM)而言,这些元素并非必要,反而带来额外计算开销与更长的响应时间。为厘清格式化的作用,本文开展了一项大规模实证研究,在 Fill-in-the-Middle 代码补全任务上,覆盖 Java、Python、C++、C# 四种语言与十种商用及开源 LLM,系统分析移除格式化元素后的 token 数量与模型性能变化。结果表明,LLM 在格式化与未格式化代码上的表现基本持平,输入 token 平均减少 24.5%,输出 token 的减少可忽略不计,说明格式移除是一种切实可行的效率优化策略。进一步探索发现,通过提示(prompting)与微调可让输出代码长度最多缩短 36.1% 而不损害正确性。作者还实现了一个双向代码格式转换工具,可无缝集成到现有 LLM 推理流程中,兼顾人类可读性与 LLM 效率。
Source code is usually formatted with elements like indentation and newlines to improve readability for human developers. However, these visual aids do not seem to be beneficial for large language models (LLMs) in the same way since the code is processed as a linear sequence of tokens. Furthermore, these additional tokens can lead to increased computational costs and longer response times for LLMs. If such formatting elements are non-essential to LLMs, we can reduce such costs by removing them from the code. To figure out the role played by formatting elements, we conduct a comprehensive empirical study to evaluate the impact of code formatting on LLM performance and efficiency. Through large-scale experiments on Fill-in-the-Middle Code Completion tasks across four programming languages (Java, Python, C++, C#) and ten LLMs-including both commercial and open-source models-we systematically analyze token count and performance when formatting elements are removed. Key findings indicate that LLMs can maintain performance across formatted code and unformatted code, achieving an average input token reduction of 24.5% with negligible output token reductions. This makes code format removal a practical optimization strategy for improving LLM efficiency. Further exploration reveals that both prompting and fine-tuning LLMs can lead to significant reductions (up to 36.1%) in output code length without compromising correctness. To facilitate practical applications, we develop a bidirectional code transformation tool for format processing, which can be seamlessly integrated into existing LLM inference workflows, ensuring both human readability and LLM efficiency.
@inproceedings{abs-2508-13666,
title = {The Hidden Cost of Readability: How Code Formatting Silently Consumes Your LLM Budget},
author = {Dangfeng Pan and Zhensu Sun and Cenyuan Zhang and David Lo and Xiaoning Du},
booktitle = {ICSE},
year = {2026},
doi = {10.1145/3744916.3764556}
}