llvm / llvm/llvm-project

performance optimization: special-case deduction for variables of type `auto`

Open
#186,274 11 comments 0 reactions 0 assignees View on GitHub
c++11 clang:frontend enhancement good first issue quality-of-implementation slow-compile
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

General-purpose `auto` deduction involves some [heavy machinery](https://github.com/llvm/llvm-project/blob/2d70dbdb357d2b4080b30d35a105442337bc1fdd/clang/lib/Sema/SemaTemplateDeduction.cpp#L5193): we synthesize a template, perform a substitution on the type, perform deduction against the substituted type, perform another substitution, and so on. All of that adds up to [a significant fraction of the total compile time](https://hachyderm.io/@lritter@mastodon.gamedev.place/116216130998393300) if `auto` is used heavily.

But in places where `auto` is used heavily, the complete type is very frequently simply `auto` (or `const auto`), in which case we don't actually need any of that heavy machinery. Clang should detect the special case where the type is just `auto` (`Context.AutoDeductTy`) and the initializer is not an `InitListExpr`, and compute the decayed type of the initializer and directly build the deduced type.

```console
$ for i in $(seq 1000000); do echo "auto v$i = 0;"; done >! ~/auto.cpp
$ for i in $(seq 1000000); do echo "int v$i = 0;"; done >! ~/int.cpp
$ time clang++ ~/auto.cpp -fsyntax-only
clang++ ~/auto.cpp -fsyntax-only 6.57s user 0.36s system 99% cpu 6.935 total
$ time clang++ ~/int.cpp -fsyntax-only
clang++ ~/int.cpp -fsyntax-only 4.40s user 0.22s system 99% cpu 4.629 total
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.