Repository navigation
Clip width 1 lines to the image - #10610
Conversation
Only step through the part of a line inside the image, using 64-bit arithmetic for the line deltas. Keep the dash phase of a clipped dashed line, and draw nothing for a dash pattern of zero length. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
|
@codspeedbot explain |
The regression comes from the What the profile shows (
Why The benchmark draws a 16-segment zigzag that stays inside the image, about 20,000 pixels per call. Clipping removes nothing here, so only the cost per pixel matters. On On this branch, one loop handles both cases ( How to fix it Keep the clipping and the 64-bit setup in Comment this and I'll do it: Summary
Was this response helpful? React with 👍 or 👎 to help us improve CodSpeedBot. |
|
Why is 64-bit arithmetic needed here? |
|
I've pushed commits to clip the lines in Python instead, but only if any go outside the image. That should address this edge case without affecting normal performance. I've also adjusted the validation to raise an error if a dash value is zero or negative. |
| def coord_at_angle( | ||
| coord: Sequence[float], angle: float | ||
| ) -> tuple[float, ...]: | ||
| x, y = coord | ||
| angle -= 90 | ||
| distance = width / 2 - 1 | ||
| return tuple( | ||
| p + (math.floor(p_d) if p_d > 0 else math.ceil(p_d)) | ||
| for p, p_d in ( | ||
| (x, distance * math.cos(math.radians(angle))), | ||
| (y, distance * math.sin(math.radians(angle))), | ||
| ) | ||
| ) |
There was a problem hiding this comment.
Function declared inside a loop?
There was a problem hiding this comment.
That's already in main, this PR just adjusts the indent -
Line 277 in af804a5
There was a problem hiding this comment.
Ah. Still... maybe something to fix?
There was a problem hiding this comment.
I've created #10621
Could you explain a bit why this is problematic?
There was a problem hiding this comment.
Python will create new function objects needlessly in the loop (and I suspect PyPy won't JIT them as well, not that it matters much here).
That PR works, but just lifting this up to e.g. global or class scope (and adding the width it captured from the closure as an argument) would do the trick too.
Restricting lines to the image in Python (python-pillow#10610) moves their end points, so that Bresenham's algorithm sets different pixels inside the image than for the whole line, and the same line moved by whole pixels no longer gives the same pixels. Instead, keep the end points and skip the steps before and after the image in C. The error term for the first visible step follows from the minor axis offset k(i) = floor((2 * minor * i + major) / (2 * major)), computed with 64-bit integers, as the coordinates are 32-bit and the product of the smaller delta and the number of skipped steps can reach 2**64. The dash position is taken modulo the length of the pattern, so that long dashed lines take no time either. Solid lines that lie inside the image take the loops they had before, so ordinary lines are no slower; only a line that reaches outside the image uses the new calculation. Python now only restricts lines that reach outside the range of C ints. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Lines of width 1 were not clipped to the image before stepping through them, so the time grew with the length of the line. For example,
line((0, 0, 2**30, 1))on a 16x16 image took 1.24 s. The line deltas were also calculated inint, which could overflow for very distant coordinates.This clips the line to the image before drawing it, and uses 64-bit arithmetic for the deltas. The example now takes no measurable time.
For dashed lines (#9490, not yet released):
should_draw_dash()walked the pattern from the start of the line for each pixel.dash=(0,), now draws nothing, rather than looping forever. Negative values are treated as 0.🤖 Generated with Claude Code