Skip to content

Commit 9915f32

Browse files
committed
Optimize add()
1 parent f5922f5 commit 9915f32

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

umplot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ typedef struct
3131
typedef struct
3232
{
3333
UmkaDynArray(Point) points;
34+
int64_t actLen;
3435
char *name;
3536
Style style;
36-
int64_t reserved;
3737
} Series;
3838

3939

umplot.um

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ type (
1616

1717
Series* = struct {
1818
points: []Point
19+
actLen: int
1920
name: str
2021
style: Style
21-
reserved: int
2222
}
2323

2424
Grid* = struct {
@@ -48,30 +48,25 @@ type (
4848
)
4949

5050
fn (s: ^Series) clear*() {
51-
s.points = []Point{}
52-
}
53-
54-
fn (s: ^Series) reserve*(size: int) {
55-
s.points = make([]Point, size)
56-
s.reserved = size
51+
s.points = make([]Point, 256)
52+
s.actLen = 0
5753
}
5854

5955
fn (s: ^Series) trim() {
60-
if s.reserved > 0 {
61-
s.points = slice(s.points, 0, len(s.points) - s.reserved)
62-
s.reserved = 0
56+
if s.actLen < len(s.points) {
57+
s.points = slice(s.points, 0, s.actLen)
6358
}
6459
}
6560

6661
fn (s: ^Series) add*(x, y: real) {
6762
if len(s.points) == 0 {
68-
s.points = []Point{Point{x, y}}
69-
} else if s.reserved > 0 {
70-
s.points[len(s.points) - s.reserved] = Point{x, y}
71-
s.reserved--
72-
} else {
73-
s.points = append(s.points, Point{x, y})
63+
s.clear()
64+
}
65+
if s.actLen >= len(s.points) {
66+
s.points = append(s.points, make([]Point, len(s.points)))
7467
}
68+
s.points[s.actLen] = Point{x, y}
69+
s.actLen++
7570
}
7671

7772
fn init*(numSeries: int = 1, kind: int = STYLE_LINE): Plot {

0 commit comments

Comments
 (0)