Given the following:
---
auto f = cast(char[2147483646]) "ab";
---
In the front-end, the initializer for this array consumes 2GB of memory (multiplied by the number of times it is copied, I count two or three times).
StringExp {
size = 2147483646;
value = "ab\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...";
}
When really, you could get away with only 32 bytes, if the data structure were smarter.
StringExp {
size = 2147483646;
values [
{
index = 0;
value = 97;
},
{
index = 1;
value = 98;
}
]
}
Comment #1 by iamthewilsonator — 2019-05-25T11:49:04Z
I think we should be fast and memory efficient for the common case, and most string aren't sparse. What use case is that?
Comment #2 by ibuclaw — 2019-06-10T14:05:25Z
(In reply to Nicholas Wilson from comment #1)
> I think we should be fast and memory efficient for the common case, and most
> string aren't sparse. What use case is that?
I'm willing to admit that StringExp may be the wrong target to try to fix. ArrayLiteralExp's however I won't give up on.
Casting a StringExp to any array type shouldn't create a new (potentially bigger) StringExp.
Comment #3 by robert.schadek — 2024-12-13T19:03:30Z