Bug 23464 – Pathological compilation slowdown when mixin in large switches

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2022-11-05T21:37:10Z
Last change time
2024-12-13T19:25:29Z
Assigned to
No Owner
Creator
deadalnix
Blocks
21121
See also
https://issues.dlang.org/show_bug.cgi?id=21121
Moved to GitHub: dmd#20179 →

Attachments

IDFilenameSummaryContent-TypeSize
1859test.dSelf-contained test casetext/x-dsrc890522

Comments

Comment #0 by deadalnix — 2022-11-05T21:37:10Z
Unfortunately, because this bug require to mix in a large switch, I'm not able to share a small sample code. You can find an example able to trigger the problem here: https://github.com/snazzy-d/sdc/tree/compiletime The compilation time is dominated by the mixin in lexHtmlEntity in lexstring.d With this function, compiling the json lexer takes 45s on my system. Without it, it takes 0.3s . There seems to be something non linear somewhere, because similar switches are mixed in in other places, but the one in lexHtmlEntity is definitively the biggest one.
Comment #1 by dkorpel — 2022-11-08T15:34:34Z
There's a lot of things going on. First of all, you're mixing in 35000 lines full of switch statements into a single function. The backend needs a few seconds to generate code for that, I think it has quadratic slowdown when you have a function with lots of scopes. You're also using `format!"case '%s':"`, which is pretty complex to do in CTFE as opposed to simple string concatenation. But by far the most time consuming is instantiating `singleEntity!""` 2000 times in the switch cases. Inside that template you index an enum `HtmlEntities`, which is an associative array literal with 2000 entries. This seems to take 90% of the compilation time. I suppose the compiler could be smarter about re-using the associative array literal.
Comment #2 by dkorpel — 2022-11-08T15:38:14Z
Created attachment 1859 Self-contained test case I attached my reduction in expanded form in a single file.
Comment #3 by robert.schadek — 2024-12-13T19:25:29Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20179 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB