Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

perf(parts): the mask's three hot loops take every step in place — the same bytes, and no kernel time in a process earlier tests left slow

Soohong Kim35abedf9e578
diff --git a/sprite_gen/video/parts.py b/sprite_gen/video/parts.py
index af4edb8..4c7d5a2 100644
--- a/sprite_gen/video/parts.py
+++ b/sprite_gen/video/parts.py
@@ -91,11 +91,28 @@ WRAP = 4 # frames: the order is read from the clip's frames this near the loop'
NB = ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1))
-def _shift(m: np.ndarray, dy: int, dx: int, fill: Any = 0) -> np.ndarray:
- """`m` moved dy rows down and dx columns right, `fill` where nothing moved in."""
- h, w = m.shape[:2]
- out = np.full_like(m, fill)
- out[max(0, dy):h + min(0, dy), max(0, dx):w + min(0, dx)] = m[max(0, -dy):h - max(0, dy), max(0, -dx):w - max(0, dx)]
+def _moved(h: int, w: int, dy: int, dx: int) -> tuple[tuple[slice, slice], tuple[slice, slice]]:
+ """Where an h x w picture moved dy rows down and dx columns right lies, and where in it that came from."""
+ return ((slice(max(0, dy), h + min(0, dy)), slice(max(0, dx), w + min(0, dx))),
+ (slice(max(0, -dy), h - max(0, dy)), slice(max(0, -dx), w - max(0, dx))))
+
+
+def _shift(m: np.ndarray, dy: int, dx: int, fill: Any = 0, out: np.ndarray | None = None) -> np.ndarray:
+ """`m` moved dy rows down and dx columns right, `fill` where nothing moved in; written into `out` where given."""
+ dst, src = _moved(*m.shape[:2], dy, dx)
+ if out is None:
+ out = np.full_like(m, fill)
+ else:
+ out.fill(fill)
+ out[dst] = m[src]
+ return out
+
+
+def _planes_max(a: np.ndarray, out: np.ndarray) -> np.ndarray:
+ """`a.max(-1)` written into `out`, plane by plane: the same values, without a reduction over a short last axis."""
+ np.maximum(a[..., 0], a[..., 1], out=out)
+ for k in range(2, a.shape[-1]):
+ np.maximum(out, a[..., k], out=out)
return out
@@ -169,13 +186,19 @@ def _code(a: np.ndarray) -> np.ndarray:
def _least(seed: np.ndarray, cost_to: list[np.ndarray]) -> np.ndarray:
- """The least cost of a path from `seed` to every pixel, a step from a neighbour costing cost_to[k] of that pixel."""
- d = np.where(seed, 0.0, np.inf).astype(np.float32)
+ """The least cost of a path from `seed` to every pixel, a step from a neighbour costing cost_to[k] of that pixel.
+ Each step is taken in place, over the pixels the neighbour moves between (elsewhere it adds to inf and changes
+ nothing), in the costs' precision. No array is made and dropped a step: in a long-lived process whose heap earlier
+ work left scattered, each one costs kernel time, and a reading takes thousands of steps."""
+ d = np.where(seed, 0.0, np.inf).astype(np.result_type(np.float32, *cost_to))
+ step = np.empty_like(d)
+ moves = [_moved(*d.shape, dy, dx) for dy, dx in NB]
for k in range(ITERATIONS):
- old = d
- for c, (dy, dx) in zip(cost_to, NB):
- d = np.minimum(d, _shift(d, dy, dx, np.inf) + c)
- if k % 25 == 24 and np.array_equal(d, old):
+ old = d.copy() if k % 25 == 24 else None
+ for c, (dst, src) in zip(cost_to, moves):
+ np.add(d[src], c[dst], out=step[dst])
+ np.minimum(d[dst], step[dst], out=d[dst])
+ if old is not None and np.array_equal(d, old):
break
return d
@@ -220,25 +243,32 @@ class Masker:
def _feet(self, a: np.ndarray, b: np.ndarray) -> float:
"""How far apart two frames' feet are: the mean of the nearer three quarters of the differences below
- FEET_ROWS, at the best shift."""
+ FEET_ROWS, at the best shift. Only those rows are read, into arrays made once a call (see `_least`)."""
top = int(FEET_ROWS * a.shape[0])
best = math.inf
r = self.feet_shift
+ feet = a[top:]
+ on_a = feet[..., 3] > 0.05
+ moved, diff = np.empty_like(b), np.empty_like(feet)
+ d, on = np.empty(feet.shape[:2], feet.dtype), np.empty(feet.shape[:2], bool)
for dy in range(-r, r + 1):
for dx in range(-r, r + 1):
- bb = _shift(b, dy, dx)
- d = np.abs(a - bb).max(-1)[top:]
- on = ((a[..., 3] > 0.05) | (bb[..., 3] > 0.05))[top:]
+ bb = _shift(b, dy, dx, out=moved)[top:]
+ _planes_max(np.abs(np.subtract(feet, bb, out=diff), out=diff), d)
+ np.logical_or(on_a, np.greater(bb[..., 3], 0.05, out=on), out=on)
v = np.sort(d[on])
if v.size:
best = min(best, float(v[: max(1, int(0.75 * v.size))].mean()))
return best
def _far(self, a: np.ndarray, b: np.ndarray) -> np.ndarray:
- """Per pixel of `a`, the least distance of its patch to `b`'s within SEARCH."""
+ """Per pixel of `a`, the least distance of its patch to `b`'s within SEARCH, in arrays made once a call (see
+ `_least`)."""
best = np.full(a.shape[:2], np.inf, np.float32)
+ moved, diff, near = np.empty_like(b), np.empty_like(a), np.empty(a.shape[:2], a.dtype)
for dy, dx in _disk(self.search):
- best = np.minimum(best, np.abs(a - _shift(b, dy, dx)).max(-1))
+ _planes_max(np.abs(np.subtract(a, _shift(b, dy, dx, out=moved), out=diff), out=diff), near)
+ np.minimum(best, near, out=best)
return best
def _siblings(self, p: np.ndarray, at: float) -> list[int]:
diff --git a/tests/video/test_parts.py b/tests/video/test_parts.py
index 3433d25..515136f 100644
--- a/tests/video/test_parts.py
+++ b/tests/video/test_parts.py
@@ -2,7 +2,8 @@
"""A part on its own beat told apart from the body (sprite_gen/video/parts.py): the tail's mask read from the clip's
frames a cycle apart, on frames filmed and on frames made between two; whether it lies in front of the body or behind
it; the tail of another drawing laid behind the body in place of a frame's own without changing a pixel of the body; and
-what is not laid — a tail in front of the body, a frame or a tail with no mask, an order read from nothing.
+what is not laid — a tail in front of the body, a frame or a tail with no mask, an order read from nothing; and the
+loops a mask runs, taken in place, answering as they did when every step made new arrays.
Three walkers are drawn here, 26 frames a cycle with a bob, legs and arms swinging, over a clip of three cycles. Their
sleeves and hands are the tail's orange; every part is outlined in a darker shade of its colour, as cel art is, and
@@ -239,6 +240,78 @@ def test_a_tail_behind_the_body_is_read_where_it_is_drawn_on_every_frame():
assert sum(_crossed(t) for t in range(N)) >= 4
+# parts.py's three hot loops as first written, making new arrays every step: what they take each step in place now must
+# give, byte for byte.
+def _shift_as_written(m, dy, dx, fill=0):
+ h, w = m.shape[:2]
+ out = np.full_like(m, fill)
+ out[max(0, dy):h + min(0, dy), max(0, dx):w + min(0, dx)] = m[max(0, -dy):h - max(0, dy), max(0, -dx):w - max(0, dx)]
+ return out
+
+
+def _least_as_written(seed, cost_to):
+ d = np.where(seed, 0.0, np.inf).astype(np.float32)
+ for k in range(parts.ITERATIONS):
+ old = d
+ for c, (dy, dx) in zip(cost_to, parts.NB):
+ d = np.minimum(d, _shift_as_written(d, dy, dx, np.inf) + c)
+ if k % 25 == 24 and np.array_equal(d, old):
+ break
+ return d
+
+
+def _feet_as_written(self, a, b):
+ top = int(parts.FEET_ROWS * a.shape[0])
+ best = math.inf
+ r = self.feet_shift
+ for dy in range(-r, r + 1):
+ for dx in range(-r, r + 1):
+ bb = _shift_as_written(b, dy, dx)
+ d = np.abs(a - bb).max(-1)[top:]
+ on = ((a[..., 3] > 0.05) | (bb[..., 3] > 0.05))[top:]
+ v = np.sort(d[on])
+ if v.size:
+ best = min(best, float(v[: max(1, int(0.75 * v.size))].mean()))
+ return best
+
+
+def _far_as_written(self, a, b):
+ best = np.full(a.shape[:2], np.inf, np.float32)
+ for dy, dx in parts._disk(self.search):
+ best = np.minimum(best, np.abs(a - _shift_as_written(b, dy, dx)).max(-1))
+ return best
+
+
+def test_each_loop_a_mask_runs_gives_the_same_bytes_in_place_as_with_new_arrays_every_step(monkeypatch):
+ # Every call of the three loops a frame's mask makes, of a frame filmed and of one made between two, is answered as
+ # the loops first written answer it: the same dtype, the same bytes. The second least-cost reading's costs are
+ # float64 (each side's own colours), and its distances are read in that precision, as they were.
+ masker = _masker("side")
+ calls = []
+
+ def kept(name, loop):
+ def call(*args):
+ calls.append((name, args, loop(*args)))
+ return calls[-1][2]
+ return call
+
+ monkeypatch.setattr(parts, "_least", kept("least", parts._least))
+ monkeypatch.setattr(parts.Masker, "_feet", kept("feet", parts.Masker._feet))
+ monkeypatch.setattr(parts.Masker, "_far", kept("far", parts.Masker._far))
+ for t in (0, MADE[0]):
+ part, _, _ = masker.mask_read(masker._read(_frame(t, "side")), t)
+ assert part.any(), t
+ as_written = {"least": _least_as_written, "feet": _feet_as_written, "far": _far_as_written}
+ for name, args, got in calls:
+ want = as_written[name](*args)
+ if name == "feet":
+ assert got == want
+ else:
+ assert (got.dtype, got.shape, got.tobytes()) == (want.dtype, want.shape, want.tobytes()), name
+ assert {name for name, _, _ in calls} == {"least", "feet", "far"}
+ assert {args[1][0].dtype.name for name, args, _ in calls if name == "least"} == {"float32", "float64"}
+
+
@pytest.mark.parametrize("reading", ["clip", "wrap"])
@pytest.mark.parametrize("walker, rare", [("side", parts.RARE), ("back", parts.RARE), ("back_bushy", parts.RARE),
("side", parts.RARE / 2), ("side", 2 * parts.RARE),