r/programming • u/Happycodeine • Jan 04 '19
Using Java to Read Really, Really Large Files
https://medium.com/@paigen11/a6f8a3f44649
7
Upvotes
r/programming • u/Happycodeine • Jan 04 '19
37
u/MrDOS Jan 04 '19
The file unpacks to 3.4GB. That's not really, really large. A typical 1.5-2hr 1080p movie is 10-12GB. Anyway, I usually like to try these sorts of record-processing challenges in AWK just to show how expressive and convenient it can be to use the right tool for the job, so here goes.
I'm invoking all of these examples with
awk -F '|'. (The default record separator will work just fine, so we only need to set the field separator.)awk --versionfor me says “GNU Awk 4.2.1”, but I don't think I'm doing anything GNU Awk-specific.Total number of lines in the file
We won't do anything at all with individual records. Once we hit the end of the input, we'll print the number of records evaluated. Easy-peasy. This returns the same number as
wc -l, but takes about six times as long to run (5.908s vs. 1.060s on my system).432nd and 43243rd names
43k records is not many. This took under a second to run.
Donations per month
I first tried a streaming approach of just keeping track of the previously-observed month and printing out the number of records between different values, but the input data isn't sorted, so I had to track each month in a map then print out all the values at the very end. This approach obviously consumes more memory, but there are not very many keys so the difference is probably minimal, and the result is a bit more readable.
AWK has no literal array syntax, so as dirty as it seems, we use the
donationsarray without any initialization. AWK figures out that you want an array and does the right thing.Also, AWK is nice enough to give us the keys in ascending, lexicographic order, so even though values weren't inserted in order, the final results are sorted.
This took ~35s to run.
Most common first name
I initially tried to use AWK's first-class regex patterns to lift out the person's first name (which, it should be noted, probably shouldn't be done because they're a Western social construct), but it took an excessively long time. Patterns are powerful, but slow. I replaced them with simple substring operations and cut down on the runtime considerably. After that, though, counting works pretty much the same as for months, but we'll keep track of the most common name as we go to avoid iterating over all entries again at the end. (If there are ties, this will return whichever name was last seen in the input.)
This one took the longest: ~59s.
Which makes more sense to me: there should be a ton of spread across runtimes for these problems, because they are very different problems, computationally. I'm not sure what's going on in the code shown in TFA that's causing her solutions to all run in such similar times. Obviously, our times can't be directly compared because our machines are different, but the spread should be much more pronounced (and it shouldn't take 2.5min to count all the lines in a file, regardless).
4
u/milad_nazari Jan 04 '19
After asking it, she will update the article with benchmarks of Files.lines (java.nio.file.Files).
3
u/AtomicStryker Jan 04 '19
Made a more efficient version which pipes the I/O read lines into an async fiber pool (CompletableFuture) for actual processing. Added it as PR to the repo, too.
https://github.com/paigen11/read-file-java/blob/cf74af0cf465dc8df0cdad1c457a44a56d6b7de2/src/main/java/com/example/readFile/readFileJava/FileReadingChallenge.java
I was surprised to find that reading the file into Strings was actually faster than what happens after, to the point that i have to pause reading the file to allow the worker threads to work off some of the piled up strings-to-parse.
6
u/Pandalicious Jan 04 '19
Something else to try would be to parallelize the reading of the file across a few threads. Split up the file into equal sized chunks based on file offset, then hand each chunk to a thread and have each thread do mostly the same data processing logic already in the post, and then combine the results once all the threads are done. Basically a map/reduce approach.
1
u/erad Jan 05 '19
Nice post!
Note that there is still some low-hanging fruit in terms of performance, most of the time is spent splitting lines with a regular expression (can be verified with the free VisualVM tool):
This regular expression is compiled for every line of the file. An easy fix without changing the code otherwise is to pre-compile the Regex pattern with Pattern.compile before the "while" loop:
and then use splitLine.split(readLine) instead of readLine.split(..regex...). This gives me a ~ 20% improvement on the big file. Trivia: "firstHalfOfName.split(" ")" is OK because String#split contains a optimization for splitting on a single character that is not a regex control character.
But this still does a lot of unnecessary work since a regex is overkill for this simple pattern - you can do e.g. the following with Apache commons-lang3 which cuts runtime compared to the original version in half:
Dependency:
But yeah, if performance is really an issue here this task is a good candidate for parallelization.
1
u/dejot73 Apr 06 '19
Nice idea, but if Java really is the right tool for text processing, not sure. My shot at https://gitlab.com/jhinrichsen/processing-large-files uses standard unix tooling, which gives you streaming, parallelization, and the ability to spread the work across multiple people for free, taking around 1:45 min on my old MacBook Pro.
Part of the 80 lines Makefile is downloading the data, downloading Paige's original version, building it, and creating a dynamic reference output (without the performance lines, they can never match).
Java OOMs, at least in the standard GitLab CI/ CD configuration using debian's `default-java`.
-8
u/Odd_Setting Jan 04 '19
Op, is that your article? It's pretty sad. Kudos for discovering that there's something else beyond node.js fuckupery, but why do you have to spew your shit left and right just because you've discovered something?
It's basics. You are reading a pretty small file and doing a quick rollup on a couple of dimensions. This got nothing to with Java but a simple realisation that one doesn't have to keep the whole dataset in memory.
You don't touch on any actual performance optimisations to speed up the processing, you fail to use appropriate data structures to store intermediates and you do unnecessary looping. Also you are horribly out of date - your whole code could be condensed to < 20 lines using functional streams. And be more performant.
It's a tiny, 10 second, job at most. Frankly I'd stick to python to avoid firing up a JVM here.
-15
u/[deleted] Jan 04 '19
r/programming never disappoints...mention java and some asshole will tell you why it sucks and why it cannot be done