News for hackers. Stuff that matters.
Re:Allocation is cheap in .NET until it is not by Shoothe on Wednesday January 24, 2018 @02:19PM (#16225480)
This pattern was also used by Java and .NET for implementing cheap String.substring calls where all substrings would use the same underlying array with just offsets changed. Unfortunately it turns out that people read entire files into a one big String and then have a reference to just a small piece of it (via substring) marking the big underlying array as reachable for the GC holding a lot of memory for no reason. That's why new implementations of substring always copy:)
Re:Allocation is cheap in .NET until it is not by kbsletten on Wednesday January 24, 2018 @02:49PM (#16225705)
I know this was changed recently-ish in Java, but I hadn't heard of anybody doing the old substring trick in .NET, do you know when they cut over?
Re:Allocation is cheap in .NET until it is not by matthewwarren on Wednesday January 24, 2018 @03:19PM (#16225940)
AFAIK this trick was never possible in .NET because Substring always 'deep copied' the relevant data into a completely new string
Re:Allocation is cheap in .NET until it is not by kkokosa on Wednesday January 24, 2018 @02:58PM (#16225760)
There is a lot of good things going on in .NET Core about this. Here is a good description of Span: http://adamsitnik.com/Span/#slicing-without-managed-heap-all...
Re:Allocation is cheap in .NET until it is not by kevingadd on Wednesday January 24, 2018 @06:04PM (#16227184)
Regex's implementation in the standard library used to do this, and I think maybe still does. Bonus points because they leaked a reference to the last string you ran a regex against (????) so if it was big you'd just eat up 50 mb of heap for a while.