Maximal Min() and Max()
Recent changes to the min() and max() macros in the Linux kernel have increased compilation times, prompting discussions on reverting to simpler macros to improve efficiency and reduce code expansion.
Read original articleRecent changes to the min() and max() macros in the Linux kernel have led to significant increases in compilation time, raising concerns among developers. The kernel, which heavily utilizes the C preprocessor, faced issues when the complexity of these macros grew due to attempts to enhance type safety and flexibility. The original min() and max() macros, which were simple comparisons, evolved into more complex forms that could handle various types but resulted in excessive code expansion during compilation. A specific line of code in the kernel was identified as particularly problematic, expanding to an enormous 47MB of preprocessor output due to nested calls and multiple evaluations.
In response to the compilation delays, developers discussed potential solutions, with some suggesting a reversion to simpler macros. Linus Torvalds expressed discomfort with the complexity of the current macros and proposed introducing new, simpler macros for constant expressions, which would not perform type checking but would be more efficient. These new macros were implemented to alleviate some of the compilation time issues, although they do not completely resolve the underlying problems associated with the existing min() and max() macros. The situation highlights the challenges of using the C preprocessor extensively and the need for careful consideration of macro complexity in kernel development. Further adjustments to the macros are anticipated as developers continue to address the compilation efficiency concerns.
Related
Own Constant Folder in C/C++
Neil Henning discusses precision issues in clang when using the sqrtps intrinsic with -ffast-math, suggesting inline assembly for instruction selection. He introduces a workaround using __builtin_constant_p for constant folding optimization, enhancing code efficiency.
A Type for Overload Set
The article explores C++ overload set challenges, discussing issues with standard functions encountering problems due to overloading. It introduces proposal P3312 for a unique type to address these limitations, emphasizing the need for a more efficient solution.
Background of Linux's "file-max" and "nr_open" limits on file descriptors (2021)
The Unix background of Linux's 'file-max' and 'nr_open' kernel limits on file descriptors dates back to early Unix implementations like V7. These limits, set during kernel compilation, evolved to control resource allocation efficiently.
What are the ways compilers recognize complex patterns?
Compilers optimize by recognizing patterns like popcount, simplifying code for efficiency. LLVM and GCC use hardcoded patterns to match common idioms, balancing compile-time speed with runtime gains in critical code sections.
Tail Recursion for Macros in C
The blog discusses tail recursion for macros in C, introducing __VA_TAIL__() to enable real recursion, overcoming inefficiencies of traditional recursive macro calls. It showcases how tail recursion improves processing efficiency in macros.
Related
Own Constant Folder in C/C++
Neil Henning discusses precision issues in clang when using the sqrtps intrinsic with -ffast-math, suggesting inline assembly for instruction selection. He introduces a workaround using __builtin_constant_p for constant folding optimization, enhancing code efficiency.
A Type for Overload Set
The article explores C++ overload set challenges, discussing issues with standard functions encountering problems due to overloading. It introduces proposal P3312 for a unique type to address these limitations, emphasizing the need for a more efficient solution.
Background of Linux's "file-max" and "nr_open" limits on file descriptors (2021)
The Unix background of Linux's 'file-max' and 'nr_open' kernel limits on file descriptors dates back to early Unix implementations like V7. These limits, set during kernel compilation, evolved to control resource allocation efficiently.
What are the ways compilers recognize complex patterns?
Compilers optimize by recognizing patterns like popcount, simplifying code for efficiency. LLVM and GCC use hardcoded patterns to match common idioms, balancing compile-time speed with runtime gains in critical code sections.
Tail Recursion for Macros in C
The blog discusses tail recursion for macros in C, introducing __VA_TAIL__() to enable real recursion, overcoming inefficiencies of traditional recursive macro calls. It showcases how tail recursion improves processing efficiency in macros.