Bug 19874 – imported CTFE static initializers are run for no reason

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-05-15T08:44:43Z
Last change time
2024-12-13T19:03:24Z
Keywords
CTFE, performance
Assigned to
No Owner
Creator
Steven Schveighoffer
Moved to GitHub: dmd#19566 →

Comments

Comment #0 by schveiguy — 2019-05-15T08:44:43Z
If you import a module that has a static initializer for a mutable variable that obviously cannot be used at compile time, and that imported module isn't being compiled, the compiler should NOT run the CTFE function, as long as it can know the type without running it. For example: ----- string buildModData() { string result; foreach(i; 0 .. 10000) result = result ~ "lots and lots and lots of data"; return result; } string moddata = buildModData(); ---- buildModData should not be run at compile time if this is simply imported, because the compiler isn't going to store it anywhere. My system consumes 1 second to import this file, regardless if I use moddata or not. In addition, even if the variable is type-inferred: --- static moddata = buildModData(); --- This should still not result in CTFE execution because the type is clear from the function definition. Essentially, only the TYPE is important here, not what the value is, since it's not readable at compile time anyway. I would also wish to have the value left uncalculated even if the value IS readable at compile time, but is not used. This may be a more difficult problem to solve, as it requires lazy evaluation of imported items. See forum thread: https://forum.dlang.org/post/[email protected]
Comment #1 by robert.schadek — 2024-12-13T19:03:24Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19566 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB