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 <nefelim4ag@gmail.com>
This commit is contained in:
Timofey Titovets 2026-02-05 20:26:35 +01:00
parent 6d7d3403e4
commit 86da9c846d

View file

@ -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;