Skip to content

[BUG] Large files compile very slowly in the C compiler #4425

@da-woods

Description

@da-woods

Describe the bug

It takes a very long time for the C compiler to run on large files like ExprNodes.py and Nodes.py. This is seen in the CI -all tests repeatedly timing out (although partly this is because we can't compile them in parallel on Python 2.7)

One possible indicator is the warning from gcc:

note: variable tracking size limit exceeded with ‘-fvar-tracking-assignments’, retrying without

(although that could potentially be toggled independently or the limit increased)

Obviously it's inevitable that a big Cython file will generate a big C file and that a big C file will take a while to compile, but potentially we could do better.

There's a few gcc flags to try to profile compilation time (https://stackoverflow.com/questions/13559818/profiling-the-c-compilation-process), and they suggest that the module init function (where all the module-level user code goes) is the main culprit (unsurprisingly)

Environment (please complete the following information):

  • Linux, CI, most obviously in Python 2.7

Additional context

I tried a couple of approaches to fix the problem.

  1. First I created small sub-scopes within the module init function. https://github.com/da-woods/cython/tree/morelocaltemps. This didn't achieve any speedup or get rid of the warning, but may be worth using some of the change for other reasons (da-woods@c901416#commitcomment-57239451)
  2. Second, I tried to split each stat at module-level into a separate function (Split up module init function #4386). This gave appreciable speed-ups for large modules. However the PR was very intended as a proof of concept with little attention to code quality....

I think a variant of the second approach is probably worthwhile. My current thought that we shouldn't do it on a "per-stat" basis but maybe give each class creation a separate function (that's easy to do for cdef classes, slightly harder for regular classes). That would likely give the appropriate granularity and keep things grouped in logical units.

Improvements made to mitigate this in Cython 3.1

More efficient string constant storage:

Shorter code generation:

More efficient code object creation that no longer permanently stores tuples in the global module state:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions