From 86da9c846dd89001f2c88930f0f88e8bca1eb2ae Mon Sep 17 00:00:00 2001 From: Timofey Titovets Date: Thu, 5 Feb 2026 20:26:35 +0100 Subject: [PATCH 1/2] stepcompress: precompute and cache max error Max error is mostly a static value. Defined by either external value or inter-step time. Procompute it once to reduce the time cost on each compressor loop. It works the same as before. Signed-off-by: Timofey Titovets --- klippy/chelper/stepcompress.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/klippy/chelper/stepcompress.c b/klippy/chelper/stepcompress.c index 0fe515ee5..78863e010 100644 --- a/klippy/chelper/stepcompress.c +++ b/klippy/chelper/stepcompress.c @@ -32,6 +32,7 @@ // optimization to reduce memory, improve cache usage, and reduce 64 bit ops) struct qstep { uint32_t clock32; + uint32_t max_error; }; // Main stepcompress object storage @@ -96,11 +97,8 @@ struct points { static inline struct points minmax_point(struct stepcompress *sc, struct qstep *pos) { - uint32_t lsc = sc->last_step_clock, point = pos->clock32 - lsc; - uint32_t prevpoint = pos > sc->queue_pos ? (pos-1)->clock32 - lsc : 0; - uint32_t max_error = (point - prevpoint) / 2; - if (max_error > sc->max_error) - max_error = sc->max_error; + uint32_t max_error = pos->max_error; + uint32_t point = pos->clock32 - sc->last_step_clock; return (struct points){ point - max_error, point }; } @@ -374,6 +372,23 @@ add_move(struct stepcompress *sc, uint64_t first_clock, struct step_move *move) list_add_head(&hs->node, &sc->history_list); } +static void +step_init_max_error(struct stepcompress *sc, struct qstep *pos) +{ + uint32_t lsc = sc->last_step_clock, point = pos->clock32 - lsc; + uint32_t prevpoint = pos > sc->queue_pos ? (pos-1)->clock32 - lsc : 0; + uint32_t max_error = (point - prevpoint) / 2; + if (max_error > sc->max_error) + max_error = sc->max_error; + pos->max_error = max_error; +} + +static void +step_update_max_error(struct stepcompress *sc, struct qstep *pos) +{ + step_init_max_error(sc, pos); +} + // Convert previously scheduled steps into commands for the mcu static int queue_flush(struct stepcompress *sc, uint64_t move_clock) @@ -381,6 +396,7 @@ queue_flush(struct stepcompress *sc, uint64_t move_clock) if (sc->queue_pos >= sc->queue_next) return 0; while (sc->last_step_clock < move_clock) { + step_update_max_error(sc, sc->queue_pos); struct step_move move = compress_bisect_add(sc); int ret = check_line(sc, move); if (ret) @@ -439,6 +455,7 @@ queue_append_far(struct stepcompress *sc) if (step_clock >= sc->last_step_clock + CLOCK_DIFF_MAX) return stepcompress_flush_far(sc, step_clock); sc->queue_next->clock32 = step_clock; + step_init_max_error(sc, sc->queue_next); sc->queue_next++; return 0; } @@ -477,6 +494,7 @@ queue_append_extend(struct stepcompress *sc) } sc->queue_next->clock32 = sc->next_step_clock; + step_init_max_error(sc, sc->queue_next); sc->queue_next++; sc->next_step_clock = 0; return 0; @@ -496,6 +514,7 @@ queue_append(struct stepcompress *sc) if (unlikely(sc->queue_next >= sc->queue_end)) return queue_append_extend(sc); sc->queue_next->clock32 = sc->next_step_clock; + step_init_max_error(sc, sc->queue_next); sc->queue_next++; sc->next_step_clock = 0; return 0; From 3f728d5505ca84f4cd747be299cda8597fbe181b Mon Sep 17 00:00:00 2001 From: Timofey Titovets Date: Sat, 7 Feb 2026 16:29:11 +0100 Subject: [PATCH 2/2] stepcompress: don't increase max error On every cycle last step happens at the time or before. New max_error is equal to or larger than before. Simplify logic by avoiding re-computing it. It have insegnificant impact on the compressed output. Signed-off-by: Timofey Titovets --- klippy/chelper/stepcompress.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/klippy/chelper/stepcompress.c b/klippy/chelper/stepcompress.c index 78863e010..0c6e6f5ad 100644 --- a/klippy/chelper/stepcompress.c +++ b/klippy/chelper/stepcompress.c @@ -383,12 +383,6 @@ step_init_max_error(struct stepcompress *sc, struct qstep *pos) pos->max_error = max_error; } -static void -step_update_max_error(struct stepcompress *sc, struct qstep *pos) -{ - step_init_max_error(sc, pos); -} - // Convert previously scheduled steps into commands for the mcu static int queue_flush(struct stepcompress *sc, uint64_t move_clock) @@ -396,7 +390,6 @@ queue_flush(struct stepcompress *sc, uint64_t move_clock) if (sc->queue_pos >= sc->queue_next) return 0; while (sc->last_step_clock < move_clock) { - step_update_max_error(sc, sc->queue_pos); struct step_move move = compress_bisect_add(sc); int ret = check_line(sc, move); if (ret)