From 9915f325c8bafc1667d542bb91a8381cf71a1b10 Mon Sep 17 00:00:00 2001 From: Vasiliy Tereshkov Date: Mon, 20 Jun 2022 16:09:30 +0300 Subject: [PATCH] Optimize add() --- umplot.c | 2 +- umplot.um | 27 +++++++++++---------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/umplot.c b/umplot.c index cbcf277..50f7f42 100644 --- a/umplot.c +++ b/umplot.c @@ -31,9 +31,9 @@ typedef struct typedef struct { UmkaDynArray(Point) points; + int64_t actLen; char *name; Style style; - int64_t reserved; } Series; diff --git a/umplot.um b/umplot.um index d2fddf6..cfb6743 100644 --- a/umplot.um +++ b/umplot.um @@ -16,9 +16,9 @@ type ( Series* = struct { points: []Point + actLen: int name: str style: Style - reserved: int } Grid* = struct { @@ -48,30 +48,25 @@ type ( ) fn (s: ^Series) clear*() { - s.points = []Point{} -} - -fn (s: ^Series) reserve*(size: int) { - s.points = make([]Point, size) - s.reserved = size + s.points = make([]Point, 256) + s.actLen = 0 } fn (s: ^Series) trim() { - if s.reserved > 0 { - s.points = slice(s.points, 0, len(s.points) - s.reserved) - s.reserved = 0 + if s.actLen < len(s.points) { + s.points = slice(s.points, 0, s.actLen) } } fn (s: ^Series) add*(x, y: real) { if len(s.points) == 0 { - s.points = []Point{Point{x, y}} - } else if s.reserved > 0 { - s.points[len(s.points) - s.reserved] = Point{x, y} - s.reserved-- - } else { - s.points = append(s.points, Point{x, y}) + s.clear() + } + if s.actLen >= len(s.points) { + s.points = append(s.points, make([]Point, len(s.points))) } + s.points[s.actLen] = Point{x, y} + s.actLen++ } fn init*(numSeries: int = 1, kind: int = STYLE_LINE): Plot {