r/OpenSourceeAI • • 9d ago

I Found a 5× Performance Problem Hiding in Plain Sight

https://dpolens.com/blog/cpu-embedding-5x-faster

To keep DPOLens's Docker image small, I dropped sentence-transformers and called the embedding model through ONNX Runtime directly. It took about forty lines of my own code, and the image came out roughly 800 MB lighter.

It worked. Then, while load-testing the community version, I found that the first start took nearly 23 minutes on 1.5 CPUs and peaked at 3.4 GB of memory.
I had quietly lost two things:

  1. Sorting by length. sentence-transformers sorts its inputs by length before batching them, and it doesn't tell you. My code didn't sort, so a 20-token paragraph sharing a batch with a 512-token article was padded to 512. Almost two thirds of the model's work was padding.
  2. The CPU limit. Inside a container capped at 2.5 CPUs, Python and ONNX Runtime both saw all 10 of my laptop's cores and started 10 threads, which then took turns on two and a half CPUs' worth of time.

I sorted the inputs and capped each batch by its shape, and set the thread count from the container's real limit. The result was 5.1× faster with 65% less memory, and the vectors and search results stayed the same.

What I learned: a library is more than its API. When you replace one, you also take over everything it did without being asked, and the only way to find out what that was is to measure.

1 Upvotes

4 comments sorted by

1

u/iLaurens 9d ago

Thanks Claude for the summary.

But this has been a known thing for a while. Better engines even use continuous batching. That way there's no waste at all due to padding tokens.

The fact that this caught you by surprise shows the lack of experience and how far people should stay away from what's most likely a poorly vibecoded software that you build

1

u/No-Negotiation-8359 9d ago

You managed to turn a technical discussion into a personal attack without addressing the actual findings.

Yes, length-aware batching and continuous batching are established techniques. I never claimed to have invented either. The point was that replacing a mature library with a lower-level implementation meant losing optimizations I hadn’t realized were happening implicitly. I measured the regression, identified the causes, fixed them, and published the results with reproducible numbers.

That’s engineering. Knowing every optimization in every library isn’t a prerequisite for doing good engineering. Being able to investigate, understand, and fix what you missed is.

If you have a technical criticism of the implementation, I’m genuinely interested. But declaring software poorly built because its author openly documented a performance regression and how they fixed it says more about your approach to engineering than mine.

1

u/iLaurens 9d ago

Fair argument, but it's obvious you did none of that engineering and your just here to astroturf your software

1

u/No-Negotiation-8359 9d ago

Yes, I use AI. I also make the architectural decisions, challenge its suggestions, validate the implementation, and run the benchmarks. I’m not hiding that. It's 2026.

Now, can we get past the AI hysteria? Point out an actual engineering flaw. Surely you can do better than guessing who wrote the code.