Skip to content

Commit e1d5893

Browse files
Emyrkjohnstcn
authored andcommitted
Reuse a buffer
1 parent 3ab32da commit e1d5893

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

coderd/authz/authztest/iterator.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ type iterator interface {
1616

1717
// SetIterator is very primitive, just used to hold a place in a set.
1818
type SetIterator struct {
19-
i int
20-
set Set
19+
i int
20+
set Set
21+
buffer Set
2122
}
2223

2324
func union(sets ...Set) *SetIterator {
@@ -26,8 +27,9 @@ func union(sets ...Set) *SetIterator {
2627
all = append(all, set...)
2728
}
2829
return &SetIterator{
29-
i: 0,
30-
set: all,
30+
i: 0,
31+
set: all,
32+
buffer: make(Set, 1),
3133
}
3234
}
3335

@@ -37,7 +39,8 @@ func (si *SetIterator) Next() bool {
3739
}
3840

3941
func (si *SetIterator) Permissions() Set {
40-
return Set{si.set[si.i]}
42+
si.buffer[0] = si.set[si.i]
43+
return si.buffer
4144
}
4245

4346
func (si *SetIterator) Permission() *Permission {
@@ -61,18 +64,21 @@ func (si *SetIterator) Iterator() iterator {
6164
}
6265

6366
type productIterator struct {
64-
i, j int
65-
a Set
66-
b Set
67+
i, j int
68+
a Set
69+
b Set
70+
buffer Set
6771
}
6872

6973
func product(a, b Set) *productIterator {
70-
return &productIterator{
74+
i := &productIterator{
7175
i: 0,
7276
j: 0,
7377
a: a,
7478
b: b,
7579
}
80+
i.buffer = make(Set, i.ReturnSize())
81+
return i
7682
}
7783

7884
func (s *productIterator) Next() bool {
@@ -88,7 +94,9 @@ func (s *productIterator) Next() bool {
8894
}
8995

9096
func (s productIterator) Permissions() Set {
91-
return Set{s.a[s.i], s.b[s.j]}
97+
s.buffer[0] = s.a[s.i]
98+
s.buffer[1] = s.b[s.j]
99+
return s.buffer
92100
}
93101

94102
func (s *productIterator) Reset() {
@@ -105,4 +113,4 @@ func (s *productIterator) Size() int {
105113

106114
func (s *productIterator) Iterator() iterator {
107115
return s
108-
}
116+
}

0 commit comments

Comments
 (0)