The Hidden Cost of Readability: How Code Formatting Silently Consumes Your LLM Budget
Preprint
Abstract
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.Keywords
All Related Versions
This publication has 18 references indexed in Scilit:
- Comparing Large Language Models and Human Programmers for Generating Programming CodeAdvanced Science, 2024
- Diet code is healthy: simplifying programs for pre-trained models of code2022
- Syntax-guided program reduction for understanding neural code intelligence models2022
- Probing model signal-awareness via prediction-preserving input minimization2021
- Style-Analyzer: Fixing Code Style Inconsistencies with Interpretable Unsupervised Algorithms2019
- Towards a universal code formatter through machine learning2016
- Measuring the Stylistic Inconsistency in Software Projects using Hierarchical Agglomerative Clustering2016
- Controlling the False Discovery Rate: A Practical and Powerful Approach to Multiple TestingJournal of the Royal Statistical Society Series B: Statistical Methodology, 1995
- A taxonomy for programming style1990
- Note on the Sampling Error of the Difference Between Correlated Proportions or PercentagesPsychometrika, 1947