Skip to content

Commit deac8af

Browse files
committed
refactor(batch): improve perf, remove Expr.segs, fill Expr.Expr
1 parent 609673f commit deac8af

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

batch.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gronx
22

33
import (
4+
"strings"
45
"time"
56
)
67

@@ -9,7 +10,6 @@ type Expr struct {
910
Expr string
1011
Due bool
1112
Err error
12-
segs []string
1313
}
1414

1515
// BatchDue checks if multiple expressions are due for given time (or now).
@@ -18,20 +18,34 @@ func (g *Gronx) BatchDue(exprs []string, ref ...time.Time) []Expr {
1818
ref = append(ref, time.Now())
1919
g.C.SetRef(ref[0])
2020

21-
batch := make([]Expr, len(exprs))
21+
var segs []string
22+
23+
cache, batch := map[string]Expr{}, make([]Expr, len(exprs))
2224
for i := range exprs {
23-
if batch[i].segs, batch[i].Err = Segments(exprs[i]); batch[i].Err != nil {
25+
batch[i].Expr = exprs[i]
26+
segs, batch[i].Err = Segments(exprs[i])
27+
key := strings.Join(segs, " ")
28+
if batch[i].Err != nil {
29+
cache[key] = batch[i]
2430
continue
2531
}
32+
33+
if c, ok := cache[key]; ok {
34+
batch[i] = c
35+
batch[i].Expr = exprs[i]
36+
continue
37+
}
38+
2639
due := true
27-
for pos, seg := range batch[i].segs {
40+
for pos, seg := range segs {
2841
if seg != "*" && seg != "?" {
2942
if due, batch[i].Err = g.C.CheckDue(seg, pos); !due || batch[i].Err != nil {
3043
break
3144
}
3245
}
3346
}
3447
batch[i].Due = due
48+
cache[key] = batch[i]
3549
}
3650
return batch
3751
}

0 commit comments

Comments
 (0)