From 4565a73e91b4beef08e76b2d23c58529f8203a91 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 5 Dec 2016 13:45:39 -0500 Subject: [PATCH] stepcompress: Optimize push_delta_const() for common XY or Z only moves Most moves are on the XY plane - avoid a few multiplications in the inner loop in this case. When there is a Z move, it is almost always entirely a Z move - avoid the sqrt() call in the inner loop in this case. Signed-off-by: Kevin O'Connor --- klippy/stepcompress.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/klippy/stepcompress.c b/klippy/stepcompress.c index 0af0672e..643e0083 100644 --- a/klippy/stepcompress.c +++ b/klippy/stepcompress.c @@ -465,12 +465,31 @@ stepcompress_push_delta_const( clock_offset += 0.5; start_pos += movexy_r*closestxy_d; height += .5 * step_dist; - while (qn < end) { - double relheight = movexy_r*height - movez_r*closestxy_d; - double v = safe_sqrt(closest_height2 - relheight*relheight); - double pos = start_pos + movez_r*height + (step_dist > 0. ? -v : v); - *qn++ = clock_offset + pos * inv_velocity; - height += step_dist; + if (!movez_r) { + // Optmized case for common XY only moves (no Z movement) + while (qn < end) { + double v = safe_sqrt(closest_height2 - height*height); + double pos = start_pos + (step_dist > 0. ? -v : v); + *qn++ = clock_offset + pos * inv_velocity; + height += step_dist; + } + } else if (!movexy_r) { + // Optmized case for Z only moves + double v = (step_dist > 0. ? -end_height : end_height); + while (qn < end) { + double pos = start_pos + movez_r*height + v; + *qn++ = clock_offset + pos * inv_velocity; + height += step_dist; + } + } else { + // General case (handles XY+Z moves) + while (qn < end) { + double relheight = movexy_r*height - movez_r*closestxy_d; + double v = safe_sqrt(closest_height2 - relheight*relheight); + double pos = start_pos + movez_r*height + (step_dist > 0. ? -v : v); + *qn++ = clock_offset + pos * inv_velocity; + height += step_dist; + } } sc->queue_next = qn; return step_dist > 0. ? count : -count;