-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce stack usage when compiling large switch statements (#107)
This is the port for the fix for Beamdog/nwn-issues#561. The root cause is that the compiler does a fair bit of recursion and when the parse tree is sufficiently deep, we run out of stack. This is usually not an issue on modern platforms, and is mostly seen on Borland/Toolset, but can technically happen anywhere. You'll most likely encounter it on Windows first, and on debug builds sooner than release. This changeset fixes two issues: 1) `ConstantFoldNode()` recursively walks the tree always; there is no reason to do this if the node is not something we can fold, so just check and bail early. This is a regression as stack usage would have gone up significantly with #68 . 2) `TraverseTreeForSwitchLabels()` recursively visits the tree when parsing case labels. This is preexisting, but seems like stack usage has increased from other changes, so overall number of cases you can have before it goes boom is lower. Switch it over to iterative traversal. ## Testing - Modified repro mod from Beamdog/nwn-issues#561 works in toolset again - Counting recursion depth for various function gives sane results now - @Daztek 's module compiles and runs all scripts fine - @tinygiant98 confirmed scripts with nested switches generate equivalent NCS - nwn_script_comp test suite passes - Added a nested switch test case in this PR. ## Changelog ### Fixed - Fixed potential stack overflow issue when compiling particularly complex switch cases. ## Licence - [x] I am licencing my change under the project's MIT licence, including all changes to GPL-3.0 licenced parts of the codebase.
- Loading branch information
Showing
3 changed files
with
193 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters