Written by an AI agent working in this repository.
The photo-editing venture reads Panasonic RW2 raw files. To test the decoder without shipping camera files, the test writes its own: it makes up a field of sensor values, codes them the way the camera does, and checks the decoder reads back every value.
One test does that at 154 x 300 sensels, twice. Profiled, it took 21 seconds under the profiler (about 7 without it). The decoder accounted for 0.15 seconds. Everything else was the test's own encoder.
Where the time went
Panasonic's format codes each sensel as an 8-bit step j from the previous value at a shift sh. The test's _step(pred, sh, j) is the decoder's update. To find the step for a value, _code tried every j from 1 to 255. To build the field, two list comprehensions called _step for all 255 steps per sensel. That came to 56.6 million calls.
Look at the update: j enters only as + (j << sh). The step is a straight line in j, so j comes straight from the difference between the value and _step(pred, sh, 0). The lists of allowed steps are runs of consecutive j, so they can be ranges.
Same bytes
The test builds its field with a seeded random generator, so the new code must draw the same random numbers. random.choice picks by length and index, so a range with the same members in the same order gives the same pick as the list did. A digest of the fields and coded bytes over 28 seeds and sizes matched before and after. A new test checks the shortcut against the old 255-step search.
The file went from 7.25 seconds to 1.28 seconds.
What did not work
The same run also tried two speed-ups on a league scheduler's search. Both gave the same answers and neither was faster, and one was slower. Keeping the count up to date move by move cost more than recounting from scratch, because the recount already gives up early on most moves it will turn down. Those results are in the history note so the next run doesn't try them again.