News for hackers. Stuff that matters.
Re:An overengineered solution to `sort | uniq -c` with 25x t... by noamteyssier on Wednesday October 22, 2025 @06:26PM (#45675957)
Was sitting around in meetings today and remembered an old shell script I had to count the number of unique lines in a file. Gave it a shot in rust and with a little bit of (over-engineering)™ I managed to get 25x throughput over the naive approach using coreutils as well as improve over some existing tools.

Some notes on the improvements:

1. using csv (serde) for writing leads to some big gains

2. arena allocation of incoming keys + storing references in the hashmap instead of storing owned values heavily reduced the number of allocations and improves cache efficiency (I'm guessing, I did not measure).

There are some regex functionalities and some table filtering built in as well.

happy hacking

Re:An overengineered solution to `sort | uniq -c` with 25x t... by theemptiness on Monday October 27, 2025 @02:55AM (#45718093)
Small semantics nit: it is not overengineered, it is engineered. You wanted more throughput, the collection of coreutils tools was not designed for throughput but flexibility.

It is not difficult to construct scenarios where throughput matters but that IMHO that does not determine engineering vs overengineering. What matters is whether there are requirements that need to be met. Debating the requirements is possible but doesn't take away from whether a solution obtained with reasonable effort meets the spec. Overengineering is about unreasonable effort, which could lead to overshoot the requirements, not about unreasonable requirements.

Re:An overengineered solution to `sort | uniq -c` with 25x t... by mabster on Monday October 27, 2025 @03:01AM (#45718125)
We had similar thoughts about "premature optimisation" in the games industry. That is it's better to have prematurely optimised things than finding "everything is slow". But I guess in that context there are many many "inner-most loops" to optimise.
Re:An overengineered solution to `sort | uniq -c` with 25x t... by chii on Monday October 27, 2025 @03:07AM (#45718160)
> That is it's better to have prematurely optimised things than finding "everything is slow".

or you found that you've optimized a game that is unfun to play and thus doesn't sell, even tho it runs fast...

Re:An overengineered solution to `sort | uniq -c` with 25x t... by wongarsu on Monday October 27, 2025 @06:42AM (#45719435)
The best-practice solution would be to write a barely optimized ugly prototype to make sure the core idea is fun, then throw away the prototype and write the "real" game. But of course that's not always how reality works
Re:An overengineered solution to `sort | uniq -c` with 25x t... by chii on Monday October 27, 2025 @08:14AM (#45720090)
> not always how reality works

yep. The stakeholder (who is paying the money) asks why the prototype can't just be "fixed up" and be sold for money, instead of paying for more dev time to rewrite. There's no answer that they can be satisfied with.

Re:An overengineered solution to `sort | uniq -c` with 25x t... by mabster on Tuesday October 28, 2025 @05:38PM (#45739578)
Or the timeline just doesn't have capacity for experimentation so the expectations are clear right from the start!
Re:An overengineered solution to `sort | uniq -c` with 25x t... by mabster on Tuesday October 28, 2025 @05:37PM (#45739563)
In context most of the major optimisation work was on the engine. The game code can be and usually is slow but we do try to tame things in an O() sense.

I worked licensed titles for a while and that area the quality of a title and whether it sells were largely uncorrelated haha!

Re:An overengineered solution to `sort | uniq -c` with 25x t... by dbdr on Monday October 27, 2025 @02:59AM (#45718108)
> using csv (serde) for writing leads to some big gains

Could you explain that, if you have the time? Is that for writing the output lines? Is actual CSV functionality used? That crate says "Fast CSV parsing with support for serde", so I'm especially confused how that helps with writing.

Re:An overengineered solution to `sort | uniq -c` with 25x t... by LtdJorge on Monday October 27, 2025 @07:29AM (#45719737)
Yes, it’s used just for writing
Re:An overengineered solution to `sort | uniq -c` with 25x t... by noamteyssier on Tuesday October 28, 2025 @12:49PM (#45735305)
Yeah I'm using it to serialize the output lines as a TSV. Rust's `println!` is notoriously slow and using `csv` to serialize the output is a nice way to boost throughput
Re:An overengineered solution to `sort | uniq -c` with 25x t... by dbdr on Wednesday October 29, 2025 @03:07AM (#45743560)
Thanks. Nice find! Though it feels weird to have to use a csv crate for that. Ideally the `fast printing` part should be understood, and either used directly, or extracted as a separate, smaller crate.
Re:An overengineered solution to `sort | uniq -c` with 25x t... by xorcist on Monday October 27, 2025 @10:10AM (#45721215)
From a causal glance, isn't your code limited by the amount of available memory?

Which could be totally useful in itself, but not even close to what "sort" is doing.

Did you run sort with a buffer size larger than the data? Your specialized one-pass program is likely faster, but at least the numbers would mean something.

That said, I don't see what is over-engineered here. It's pretty straightforward and easy to read.

Re:An overengineered solution to `sort | uniq -c` with 25x t... by noamteyssier on Tuesday October 28, 2025 @12:47PM (#45735267)
Yes you're right, it's not trying to do what `sort` is doing, it's trying to reproduce the output of `sort | uniq -c | sort -n` which is a more specialized but common task.

But you're right - it will be limited by RAM in a way the unix tools are not.

I did add a test with sort using a larger buffer size to the benchmarks as well.

Re:An overengineered solution to `sort | uniq -c` with 25x t... by zahlman on Monday October 27, 2025 @10:42AM (#45721528)
How often is "count the unique lines of a file" a realistic task for others out there, and how big of files do y'all need to process and why?
Re:An overengineered solution to `sort | uniq -c` with 25x t... by Tostino on Monday October 27, 2025 @11:05AM (#45721822)
Reasonably often in ETL type tasks.
Re:An overengineered solution to `sort | uniq -c` with 25x t... by noamteyssier on Tuesday October 28, 2025 @12:52PM (#45735355)
Shows up a lot in bioinformatics actually - trying to identify sequences with a specific subsequence (grep) and how many of each unique sequence there are. The number of lines here could be massive (order of 1-10's of GB)

You don't really end up using these results in any specific analysis but it's super helpful for troubleshooting tools or edge-cases.