Show HN: Samchika – A Java Library for Fast, Multithreaded File Processing
63 points - last Friday at 1:39 PM
Hi HN,
I built a Java library called SmartFileProcessor to make high-performance, multi-threaded file processing simpler and more maintainable.
Most Java file processing solutions either involve a lot of boilerplate or don’t handle concurrency, backpressure, or metrics well out of the box. I needed something fast, clean, and production-friendly — so I built this.
Key features:
Multi-threaded line/batch processing using a configurable thread pool
Producer/consumer model with built-in backpressure
Buffered, asynchronous writing with optional auto-flush
Live metrics: memory usage, throughput, thread times, queue stats
Simple builder API — minimal setup to get going
Output metrics to JSON, CSV, or human-readable format
Use cases:
Large CSV or log file parsing
ETL pre-processing
Line-by-line filtering and transformation
Batch preparation before ingestion
I’d really appreciate your feedback — feature ideas, performance improvements, critiques, or whether this solves a real problem for others. Thanks for checking it out!
SourceCalzifer
last Friday at 4:04 PM
for(int i=0;i<10000; ++i){
// do nothing just compute hash again and again.
hash = str.hashCode();
}
https://github.com/MayankPratap/Samchika/blob/ebf45acad1963d..."do nothing" is correct, "again and again" not so much. Java caches the hash code for Strings and since the JIT knows that (at least in recent version[1]) it might even remove this loop entirely.
[1] https://news.ycombinator.com/item?id=43854337
hyperpape
last Friday at 5:29 PM
Even in older versions, if the compiler can see that there are no side-effects, it is free to remove the loop and simply return the value from the first iteration.
I'm actually pretty curious to see what this method does on versions that don't have the optimization to treat hashCodes as quasi-final.
A quick test using Java 17 shows it's not being optimized away _completely_, but it's taking...~1 ns per iteration, which is not enough to compute a hash code.
Edit: I'm being silly. It will just compute the hashcode the first time, and then repeatedly check that it's cached and return it. So the JIT doesn't have to do any _real_ work to make this skip the hash code calculation.
So most likely, the effective code is:
computeHashCode();
for (int i = 0; i < 10000; i++) {
if (false) { // pretend this wouldn't have dead code elimination, and the boolean is actually checked
computeHashCode();
}
}
mprataps
last Friday at 7:05 PM
You are write. This code does not recalculate. However, it was written just as a sample. Mainly user will provide his own method to process the file.
mprataps
last Friday at 7:04 PM
Guys. I love you all. I did not expect such quality feedback.
I will try to incorporate most of your feedback. Your commments have given me much to learn.
This project was started to just learn more about multithreading in a practical way. I think I succeeded with that.
sieve
last Friday at 6:52 PM
A note on the name.
The nasal "m" takes on the form of the nasal in the row/class of the letter that follows it. As "ñ" is the nasal of the "c" class, the "m" becomes "ñ"
Writing Sanskrit terms using the roman script without using something like IAST/ISO-15919 is a pain in the neck. They are going to be mispronounced one way or the other. I try to get the ISO-15919 form and strip away everything that is not a-z.
So, सञ्चिका (sañcikā) = sancika
You probably want to keep the "ch," as the average English speaker is not going to remember that the "c" is the "ch" of "cheese" and not "see."
arnsholt
last Friday at 7:05 PM
It’s been ages since I did Sanskrit last, but wouldn’t sam-cika typically have the m realized as an anusvara rather than ñ?
sieve
last Friday at 7:18 PM
Not unless it precedes a classless letter or it is actually "m."
All nasals becoming anusvaras is something Hindi/Marathi and other languages using the Devanagari script do. Sanskrit uses the specific form of the nasal when available.
mprataps
yesterday at 5:43 PM
I have CONTRIBUTING.md with guidelines regarding Pull Requests if any of you would take out your precious time to make some changes in the library.
sidcool
last Friday at 3:58 PM
It would be even more amazing if it had tests. It's already pretty good.
mprataps
last Friday at 7:07 PM
I will add unit tests next.
DannyB2
last Friday at 4:12 PM
Should the tests include some 10 GB files?
VWWHFSfQ
last Friday at 4:49 PM
Should include a script for generating 10GB files maybe
diggan
yesterday at 11:34 AM
Use tmpfs (/dev/shm) and it doesn't even have to hit the disk, all in memory but with filepaths as the library API might expect :)
sidcool
last Friday at 6:55 PM
Naah. I meant unit tests. Not load tests.
sureglymop
last Friday at 3:34 PM
Perhaps I misunderstand something but doesn't reading from a file require a system call? And when there is a system call, the context switches? So wouldn't using multiple threads to read from a file mean that they can't really read in parallel anyway because they block each other when executing that system call?
mike_hearn
last Friday at 4:15 PM
System calls aren't context switches. They flip a permission bit in the CPU but don't do the work a context switch involves like modifying the MMU, flushing the TLBs, modifying kernel structures, doing scheduling etc.
Also, modern filing systems are all thread safe. You can have multiple threads reading and even writing in parallel on different CPU cores.
xxs
last Friday at 9:03 PM
What all other siblings said - syscalls are not context switch, they are called 'mode switch' and it has significantly less impact.
bionsystem
last Friday at 4:18 PM
If you open() read-only I don't think it blocks (some other process writing to it might block though).
porridgeraisin
last Friday at 6:31 PM
> system call, the context switches
No, there is no separate kernel "executing". When you do a syscall, your thread becomes kernel mode and it executes the function behind the syscall, then when it's done, your thread reverts to user mode.
A context switch is when one thread is being swapped out for another. Now the syscall could internally spawn a thread and context switch to that, but I'm not sure if this happens in read() or any syscall for that matter.
stopthe
yesterday at 2:30 PM
Does it handle line breaks inside quotes in CSV? Frankly, I don't think its possible to reliably process CSV in а multi-threaded manner.
drob518
yesterday at 3:34 PM
At least not without an initial scan. You could do post processing (e.g. parsing numbers and dates and things) in parallel after you’ve done correct line break processing.
codetiger
last Friday at 3:23 PM
Do you have a benchmark comparison with other similar tools?
VWWHFSfQ
last Friday at 4:00 PM
Am I wrong in thinking that this is duplicating lines in memory repeatedly when buffering lines into batches, and then submitting batches to threads? And then again when calling the line processor? Seems like it might be a memory hog
Calzifer
last Friday at 8:50 PM
Since most things in Java are handled by reference, including Strings there should be not that much memory overhead. From a quick look I could not find any actual line duplication.
gavinray
last Friday at 3:14 PM
Please don't do this.
Have the OS handle memory paging and buffering for you and then use Java's parallel algorithms to do concurrent processing.
Create a "MappedByteBuffer" and mmap the file into memory.
If the file is too large, use an "AsynchronousFileChannel" and asynchronously read + process segments of the buffer.
papercrane
last Friday at 3:53 PM
If you're using a newer JVM you can also map a "MemorySegment", which doesn't have the 2GiB limit that byte buffers have.
90s_dev
last Friday at 3:23 PM
Knowing nothing about Java or compsci, I am very curious to see the in depth discussion by all you Java/compsci experts that your comment invites.
switchbak
last Friday at 4:51 PM
Memory mapping is fun, but shouldn't we have some kind of async IO / uring support by now? If you're looking at really high-perf I/O, mmaping isn't really state of the art right now.
Then again, if you're in Java/JVM land you're probably not building bleeding edge DBs ala ScyllaDB. But I'm somewhat surprised at the lack of projects in this space. One would think this would pair well with some of the reactive stream implementations so that you wouldn't have to reimplement things like backpressure, etc.
threeseed
last Friday at 8:04 PM
a) There have been libraries supporting io_uring on the JVM for many years now.
b) SycllaDB is not bleeding edge. It uses the relatively old now DPDK.
c) There are countless reactive stream implementations e.g. https://vertx.io/docs/vertx-reactive-streams/java/
hawk_
last Friday at 9:34 PM
I thought DPDK would still be faster than io_uring.
jlokier
last Friday at 8:53 PM
Last time I measured on Linux (a few years ago), with NVMe, mmap + calling out to a thread pool to async-page-touch (so the main thread didn't block) was faster than io_uring (from the main thread) for random access reads.
exabrial
last Friday at 6:38 PM
try not to be a dick
SillyUsername
last Friday at 5:09 PM
Better caveat that with, "but watch memory consumption, given the nature of the likes of CopyOnWriteArraylist". GC will be a bitch.
mprataps
last Friday at 7:10 PM
Thanks for this comment. This will be an interesting aspect to explore.
johnisgood
last Friday at 3:50 PM
[flagged]
pritambarhate
last Friday at 4:19 PM
What's wrong in it? LLM is a tool which makes one more productive.
johnisgood
last Friday at 5:08 PM
I said "I am not saying it is wrong", but it is getting a bit tiring that every single README.md is the same. All I wanted to know is if it is wrong to assume.
It is not wrong, but at least put yourself into it a bit.
ldjkfkdsjnv
last Friday at 5:57 PM
[flagged]
mprataps
last Friday at 7:09 PM
May be. I just started this with the intention to learn about multithreading. I learnt a lot of concepts which I had earlier only learnt in theory. I learnt how to use VisualVM to see my thread performance. I learnt to use builder design pattern. No LLM can take away this learning.
And this project is just a start.
threeseed
last Friday at 8:10 PM
No different to cheating off someone at school.
You didn't learn anything. You didn't accomplish anything. And no one including you respects it.
bogeholm
last Friday at 7:10 PM
I could probably do an Ironman if I really wanted to
apwell23
yesterday at 3:59 AM
omg its you again. I want to beat the shit out of this guy for spamming these AI comments everywhere.
ldjkfkdsjnv
yesterday at 4:09 AM
:-)
SillyUsername
last Friday at 5:05 PM
An ArrayList for huge numbers of add operations is not performant. LinkedList will see your list throughput performance at least double. There are other optimisations you can do but in a brief perusal this stood out like a sore thumb.
Calzifer
last Friday at 9:00 PM
Arrays are fast and ArrayList is like a fancy array with bound check and auto grows. Only the grow part can be problematic if it has to grow very often. But that can be avoided by providing an appropriate initial size or reusing the ArrayList by using clear() instead of creating a new one.
Both is used by OP in this project.
Especially since the code copies lists quite often I would expect LinkedList to perform way worse.
SillyUsername
yesterday at 10:25 AM
Wrong. In fact downvoters are wrong too I'm guessing most are junior devs who don't want to be proven wrong.
LinkedList is much faster for inserts and slow for retrieval. ArrayLists are the opposite.
To the downvoters; I say try it, this is why LinkedList is in the standard library.
When you find I'm right, please consider re-upvoting for the free education.
pkulak
last Friday at 5:35 PM
I've literally never seen a linked list be faster than an array list in a real application, so if you're right, this is kinda huge for me.
SillyUsername
yesterday at 10:27 AM
LinkedList => use when adds total more than reads
ArrayList => use when reads total more than adds.
stopthe
yesterday at 2:17 PM
Did you count an allocation of LinkedList.Node<E> on every add operation?
You may say it's negligible thanks to TLAB, and I will agree that fast allocation is Java's strength, but in practice I've seen that creating new objects gives order-of-magnitude perf degradation.
SillyUsername
yesterday at 5:16 PM
I have seen it for millions of add/del operations, an analytics framework actually for a big American games company (first guess and you'll probably say it), which is where I originally did the analysis about 10 years ago.
I've also written a a video processor around that time too that was bottle necked using ArrayLists - typically a decode, store and read once op.
It was at this point I looked at other collections, other list implementations and blocking deques (ArrayList was the wrong collection type to use, but I'd been in a rush
for MVP) and ultimately came across https://github.com/conversant/disruptor and used that instead.
The ArrayList Vs Linkedlist was a real eye opener for me in two different systems this same behaviour was replicated when using ArrayLists like queues or incorrect sizing of the buffer increments as load increases.
fedsocpuppet
last Friday at 5:19 PM
Huh? It'll be slower and eat a massive amount of memory too.
SillyUsername
yesterday at 10:29 AM
It's holding a reference on each element, but it no longer has to add large chunks of memory on insert when the current array size is exceeded, just single elements.
So reads are slower and a small amount of reference memory is used per node. Writes however are much faster particularly when the lists are huge (as in this case).
Also I've written video frame processors so I am experienced in this area.