Skip to content

[Modules] Disappointing O(n^2) scaling of compile times with modules and optimizations #60996

Closed
@davidstone

Description

@davidstone

Given the following set of translation units

export module a;

#define REPEAT8(x) \
	(x), \
	(x), \
	(x), \
	(x), \
	(x), \
	(x), \
	(x), \
	(x)


export unsigned a() {
	unsigned x = 0;
	REPEAT8(REPEAT8(REPEAT8(REPEAT8(REPEAT8(REPEAT8(++x))))));
	return x;
}
export module b;

import a;

export unsigned b() {
	return a();
}
export module c;

import b;

unsigned c() {
	return b();
}

Compiling with optimizations turned on (say, -O3) takes a few seconds for a, which is reasonable (a is designed to be expensive to compile and optimize in this example). But then it takes a few seconds for b, and a few seconds for c. My expectation was that the compiler would compile and optimize the function a once, when compiling module a, and then compiling the importers of a would be fast. The net effect effect of this problem is that after converting a program to use modules (and not using any module partitions), the last few modules are each taking about 30 minutes to compile, as each high-level module is optimizing almost my entire program serially.

This is effectively quadratic scaling of compile times -- a does all the work of a, b does all the work of a + b, c does all the work of a + b + c, etc.

I understand how we got to this point -- we want the generation of the .pcm file to happen as quickly as possible, because then any downstream consumer that doesn't care about optimizations can get access to that data quickly (just precompiling b took about 300 milliseconds on my machine, and precompiling c took about 15 milliseconds). However, this seems to sell out the common case of compiling your program.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions