Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash with huge number of array concats. #808

Open
i-kudryavtsev opened this issue Apr 18, 2024 · 3 comments
Open

Crash with huge number of array concats. #808

i-kudryavtsev opened this issue Apr 18, 2024 · 3 comments

Comments

@i-kudryavtsev
Copy link

Hi!

I'm doing MiniZinc code generation, and I found that it crashes during compilation if I try to contact more than 866 arrays :-)

That's especially strange because I see this issue only in some of the latest versions; 2.6.2 works well. Please take a look at the attachment.

Thanks a lot in advance!
concats.txt

@cyderize cyderize transferred this issue from MiniZinc/MiniZincIDE Apr 19, 2024
@cyderize
Copy link
Member

I think this is because of the deep nesting of the ++ operators - at the moment expressions like a ++ b ++ c ++ d are parsed as (((a ++ b) ++ c) ++ d). If you have 1024 of these, the call stack runs out of space and overflows. It may work with other versions because the stack frames may have previously been smaller in size (in general the maximum depth achievable varies on each platform/C++ compiler).

You can work around this by using parentheses to reduce the maximum depth (parenthesising them to be a balanced binary tree). Even just putting parentheses around the first half and the last half of the terms will reduce the depth by half, which should succeed here.

I did notice that the error message for me seems to mention a stack depth of 2, which makes it look rather strange, since we're actually not considering the flattening of the arguments as new call stack entries - so maybe that should be changed to at least give a more useful number.

@i-kudryavtsev
Copy link
Author

It looks like parentheses do not work. Dividing into two aux vars works fine.

~900 vars does not look enormously huge to me. Any chances it would be fixed in future versions?

Thanks!

@cyderize
Copy link
Member

cyderize commented Apr 22, 2024

Interestingly for me the parentheses work (i.e. parentheses around the first 512 terms, and then another set of parentheses around the last 512 terms).

Unfortunately it's not really possible to fix this generally in the current compiler - I suppose we could add compiler flags when building the compiler on supported platforms to increase the stack size, or maybe we could specially recognise chained ++ operations and handle them separately.

It seems odd that you would need to do this though - I would have imagined with a generated model it would probably make more sense to create the one big 2D array with everything in it and then use array slicing to get the smaller ones if you need them (which should also perform better than array concatenation).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants