355 GB of Memories and One Slow Hard Drive

I travel often and I take a lot of photos. Most of them end up on an external hard drive, next to a Google Takeout export that repeats the same pictures album after album. 94,886 files, 355 GB, and somewhere in there is the one shot where everybody from that trip is laughing.

The cloud services can find it. But they have to scan everything on their servers first, and I did not want to upload years of my life for something my own laptop should be able to do.

So I built Backthen. You type “waterfall in the mist” and it finds the photo, or the exact second inside a video. It draws your trips on a globe, and it brings back the ones worth remembering. Nothing is uploaded. The hardware is the same machine from my last two posts: an RTX 3050 laptop with 4GB VRAM, 16 GB RAM, and the archive sitting on a USB hard drive.

Backthen home screen

I expected the machine learning to be the hard part. It was not even close.

First Question: Where Does the Time Go?

Everyone calls it “ingestion” like it is one step. It is at least seven: find files, read bytes, parse metadata, decode, resize, run the model, write results. Before optimizing anything I wrote down six hypotheses and measured them one by one.

HypothesisVerdictWhat I saw
Many small files kill the filesystemRejected (on SSD)0.15 ms per file
Seeking inside video decodes the same frames again and againConfirmed69–75 frames decoded for each one kept
CPU work starves the GPUConfirmedGPU waiting 82–94% of the time
Copying frames off the GPU cancels hardware decodingRejectedcopy costs about 1%
Decoder start-up dominates short clipsRejected14% of decode time
A fixed worker split leaves stages idleNot settled3 CPU + 1 GPU decoder was slower than 2 + 1

That third row decided everything. Real CLIP inference on my little GPU is about 1 ms per photo. A thousand photos per second. The GPU could have finished my whole archive in a couple of minutes if only something could feed it.

So this was never a GPU problem. It was a feeding the GPU problem.

Things I Was Sure Would Work

GPU JPEG decoding. Obvious, right? The photos end up on the GPU anyway, so decode them there.

Photo decoderPhotos per second
CPU, reduced-size decode, 16 threads115.0
nvJPEG on the GPU, batch 818.7
nvJPEG on the GPU, batch 3218.9

Six times slower, and the batch size does not matter at all. My best guess is that a laptop GPU has no JPEG hardware, so the slow part quietly runs on one CPU thread. The CPU wins for a different reason: libjpeg-turbo can decode a 12 megapixel photo directly at quarter size. I only need a 512 px thumbnail and a 224 px model input. Why decode fifteen pixels to throw fourteen away?

That one change, plus parallel workers, took photos from 6.2 to 93 per second.

Keyframe-only video. 3.2x faster on the SSD, the kind of number you want to keep. But I had fixed a quality gate before running it: at least 95% of videos must keep 0.95 similarity to exact sampling. Keyframes passed on 67% of videos on one set and 27% on the other. Out.

What survived was a smaller idea. Keep the exact frames, but skip any stretch of video that has no target frame in it. 120,481 decoded frames became 40,913 and the output is bit for bit the same.

More threads. On the SSD, 16 readers more than double the throughput. On the hard drive, going from one reader to two made it 20% slower. There is one head, and it can be in one place. One sequential reader, everything else waits.

The Bug That Was Not in My Code

For a while my benchmarks made no sense. The same job took 45 seconds in one run and 110 in another.

First I blamed the power plan, because Balanced looked 2.7x slower than High Performance. Wrong. Once I ran the configurations interleaved, the pattern showed up.

It was Windows throttling the process on its own (EcoQoS, the power saving meant for background work). One API call opts out:

Windows planThrottlingWall time
Balanceddefault148.9 s
Balancedoff46.2 s
High Performancedefault109.0 s (44.7 to 112.7)
High Performanceoff49.3 s

Biggest speedup of the whole project, 3.2x, and it has nothing to do with machine learning, Python or my architecture. It also means some of my older single-run numbers were partly measuring Windows. I still have not found what triggers it.

The Real Archive

Everything above was on test folders. Then I pointed it at the 355 GB drive.

Result
Wall time, cold98.2 min
Reader busy90% of the run
GPU busy8%
Peak RAM / VRAM4.28 GB / 1.12 GB

The drive reads at about 95 MB/s on a good day. Just streaming the bytes would take 59 minutes, no matter how clever I am. The remaining 39 minutes were mine to fight for.

There were two things worth fixing. My archive had 18,883 byte-identical duplicates, mostly Takeout albums, and I was reading every copy in full to find out it was a copy. Now a file whose size matches something already seen gets only its first and last 64 KB read. That is 52.6 GB I never touch. And files are read in the order they physically sit on the disk, not in folder order.

The first version ran in 84.7 minutes. Then I compared its output to the slow run: 211 capture times and 1,444 video embeddings were different. The copy that got read first had become the “owner”, and it brought the wrong metadata with it.

I fixed the ownership rule and gave back 2.4 minutes for it.

98.2 → 87.1 minutes, with all 73,815 embeddings bit-identical.

I had projected 65 to 75. Disk-order reading was supposed to cut small-file latency to 10–15 ms and it only went from 29 to 25. The small files on that drive were written at different times, spread over the platter, and sorting them does not bring them much closer. I am leaving that miss in here.

No Rust

My plan from day one had a line about rewriting the core in Rust “if needed”. It was never needed. The time is inside libjpeg, FFmpeg and a spinning disk. A faster language would have made my Python wait faster.

Search Was the Easy Part

73,815 vectors of 512 floats is about 151 MB. A query is one matrix multiplication:

scores = vectors @ query                             # every photo and frame
best   = np.maximum.reduceat(scores, first_frame)    # a video is as good as its best frame

No FAISS, no vector database. Ranking takes 6 ms. The whole query takes 32 ms, and 26 of those are the text encoder turning my sentence into numbers. I will add an index on the day this gets slow.

I did check that my fast decoding was not quietly hurting quality. On public benchmarks the pipeline matches plain OpenCLIP (Flickr30k Recall@1 0.662 against 0.667), so the speed was free. Better models are not:

ModelFlickr30k R@1COCO R@1Query time on CPU
ViT-B/32 LAION-2B (default)0.6620.39432 ms
ViT-L/14 DFN-2B0.7510.48761 ms
ViT-H/14 DFN-5B0.7970.527228 ms

Remember the GPU that was idle 92% of the time? That is where the bigger model goes next.

Then It Cut Me Out of My Own Trip

Speed is nice, but the thing I wanted was to feel something when I open it. So I added Memories: a row of postcards from old trips.

Memories row

The screenshots use a sample library of CC0 photos with made-up trips, not my own.

The first version picked the prettiest photos. It was technically correct and completely dead. Nobody misses a sunset. You miss the people you watched it with.

The second version looked for people. And on one trip, where four of us went, it picked a lovely picture of three. The fourth one was me. My own app had removed me from my own trip.

It makes sense once you see it. The score rewarded photos with familiar faces, and never asked how many of us were in there. So I wrote down what a memory actually is, in rules a computer can check:

  1. A trip is three days or more. One or two days is a day out. You have not escaped anything yet.
  2. A memory is shared. If none of its best photos has two people in it, it does not make the row.
  3. Nobody gets left out. The group size is the largest headcount that repeats. Four people in two different photos means the group is four. Nine faces in a single photo is probably a queue.
  4. No day appears twice.

After that, zero of the twelve covers on my library showed fewer people than the actual group. Before, it was three.

Making It Fast Again

Faces brought back the old problem. Checking all 48,569 thumbnails took 31.8 minutes, and in one run the home screen’s covers took 36.5 seconds, because I was computing everything while the request waited.

Same lesson as AutoKaggle: compute is scarce, spend it deliberately.

The CLIP vectors already exist from indexing, so scoring every photo against a sentence like “friends posing together for a photo” is a dot product. Milliseconds. The expensive face model then looks only at the top candidates of each memory.

BeforeAfter
Photos the face model has to check48,5691,974
Time per photo34 ms14 ms
Memories on the next launchup to 36.5 s17 ms

The part I did not expect: for CLIP scoring the images are free and the text is expensive, 21 ms per prompt on CPU. So the prompts are encoded once and saved to disk. I had been optimizing the wrong side.

And one small bug that I liked. Sometimes the postcards came up with broken images. All the thumbnails were on disk, so it was not that. It was Python’s socketserver, which by default lets only five connections wait in line, and my home screen asks for about forty thumbnails at once. In a reproduction 32 of 72 requests were refused. I changed a 5 to a 128.

What I Would Tell Myself in the Beginning

  1. Measure before choosing the language, the library or the GPU. GPU JPEG decoding and parallel disk reads both made it slower.
  2. Fix the quality gate before you see the speedup. After you see 3.2x you will find a reason to accept it.
  3. Doing less beats doing it faster. Quarter-size decode, skipped frames, unread duplicates. Apart from fighting Windows, every big win was work I stopped doing.
  4. Interleave your benchmarks. Otherwise you are measuring your operating system’s mood.
  5. Publish what missed. I projected 65 minutes and got 87.

It is still not done. The face matching is weak on group photos because the faces are too small at thumbnail size, I have not labelled enough of my own searches to say how good the retrieval is on my photos, and it has only been tested on one Windows laptop, mine.

I started by asking why importing photos is slow. I ended up writing rules for what counts as a memory, and the answer I trust most has no benchmark: at least three days away, with people, and everyone in the frame.

If it makes me look at an old trip and plan the next one, that is the actual feature.

Until next time.