News for hackers. Stuff that matters.
Re:Strings Are Evil by dahart on Wednesday June 20, 2018 @01:06PM (#17357230)
For such a long deep dive, I'm surprised it made a big deal about memory while overlooking the obvious. Almost no memory was actually saved. The peak working set was 16Mb at version 1, and it ended at 12Mb. The temporary strings that are allocated are all immediately released. The timing went from 8.7 seconds to 6.7 seconds. So all that effort bought a total of a 25% improvement in memory usage and CPU time.

Great - 25% is nothing to sneeze at. Just noting that memory wasn't the problem.

And of course there's a hidden development cost to doing fancy indexing on strings rather than being comfortable with String.Split() making your parser go 25% slower. The code at the start is more functional, it's easier to look at and know it will do what's expected. The optimized code took longer to write, and it's harder to understand, and harder to modify.

Re:Strings Are Evil by commandlinefan on Wednesday June 20, 2018 @01:34PM (#17357569)
Yeah, you could easily argue (as I've had many, many bosses do) that the final version is objectively worse as it's doubtful that it will save as much time as it cost to develop (and debug - I doubt he got it right on the first pass!) and will drive in ongoing maintenance: the first version is simple and easy to understand (but inefficient) while the final version is almost impenetrable unless you read through the whole blog post, which is probably not part of source code control.
Re:Strings Are Evil by kemiller2002 on Wednesday June 20, 2018 @02:08PM (#17357978)
The perfect is the enemy of the good. For a few hundred dollars I can buy more memory etc. that I can use over and over again. The time that is sunk into building it to make it more efficient is gone forever.

Admittedly, I liked the write up. It's a fun exercise to go over, but I would ask my devs to go with the original program.

Re:Strings Are Evil by ant6n on Wednesday June 20, 2018 @04:07PM (#17359240)
You can't buy memory on your user's devices.
Re:Strings Are Evil by cityhomesteader on Wednesday June 20, 2018 @03:19PM (#17358777)
In a similar strain.

Premature optimization is the root of all evil. - Knuth

Re:Strings Are Evil by girvo on Wednesday June 20, 2018 @10:43PM (#17361675)
Here is the larger quote:

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.”

Re:Strings Are Evil by wahern on Wednesday June 20, 2018 @06:48PM (#17360578)
> The timing went from 8.7 seconds to 6.7 seconds. So all that effort bought a total of a 25% improvement in memory usage and CPU time.

AFAIK .NET has a concurrent garbage collector. For all we know there was a 2nd core running 100% to collect strings that is now almost entirely idle. It's possible the effort more than doubled overall throughput if you consider the ability to run multiple jobs.

That said, ad hoc parsing is a fundamentally messy approach in every language, not just languages like C with poor string handling.[1] Parser generators exist for a good reason--easier to read and great performance without even trying. Find a good one for your language of choice. The learning curve is steep but you'll never look back.

[1] Actually, I find C one of the best languages for parsing as you're much less likely to conflate string manipulation with parsing. Though that realization comes more quickly for some than for others.

Re:Strings Are Evil by AstralStorm on Thursday June 21, 2018 @09:49AM (#17365023)
Why are you parsing strings anyway instead of using an efficient binary format with introspection, say, something as simple as Google Protobuf?

If the GC is the problem, you can pool the message instances.

About the only place where you parse strings is if you're dealing with human input or something almost unstructured like aforementioned CSV.

Re:Strings Are Evil by jccooper on Wednesday June 20, 2018 @04:42PM (#17359562)
Explaining the circumstances behind this particular optimization target would certainly have been more helpful; as a standalone exercise, reducing allocations does look rather useless.

But the author presumably has some reason for focusing on allocations (and GC generations, though that never really comes up). Perhaps the subject code is causing excessive GC pauses in the larger program it is part of? If they're doing "real-time calculations" as part of financial services, I can see where adding latency for several seconds could be a bad thing.

Re:Strings Are Evil by dahart on Wednesday June 20, 2018 @05:11PM (#17359819)
I agree, I totally buy that it's possible there's an unstated reason to worry about the sum of allocations over time. Though, in an article of this length and depth, not spending a sentence or two and stating that reason seems like an obvious omission. And a reason is given, so I don't know why there'd be another separate hidden reason:

"Currently this import process has to take place outside of business hours because of the impact it has on memory usage."

That statement is pretty confusing to me; if the high watermark is 16MB, there's no memory usage problem to begin with. And if saving 25% CPU time is enough to go from after hours to during business hours, it won't take much to push the jobs back to after hours, right?

The hyperbole in the title & headlines combined with the vague issue of memory usage also leads me to believe this might be more about exposure for the blog post than fixing a problem. That's perfectly fine, but it's also okay to discuss and question what we're presented with, right?

It all feels to me like optimizing to the wrong metric. If you really want a parser to go fast, then eliminate line buffered input and block read the thing. Then eliminate all other memory allocations and process the file in memory. That will increase the memory usage in this case substantially, but it will be much faster than the improved version v11 they ended up with.