Skip to content

Commit 1101cfc

Browse files
committed
allocate slice with known capacity in *ListImpl() functions
The current implementation of *ListImpl() functions allocate more memory than that is required. We can just allocate the slice with exact capacity as we know the number of the logical resources. Fixes issue (eBay#102) Signed-off-by: Yun Zhou <[email protected]>
1 parent 556b507 commit 1101cfc

12 files changed

+69
-92
lines changed

acl.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package goovn
1818

1919
import (
20+
"fmt"
2021
"github.com/ebay/libovsdb"
2122
)
2223

@@ -300,43 +301,44 @@ func (odbi *ovndb) rowToACL(uuid string) *ACL {
300301

301302
// Get all acl by lswitch
302303
func (odbi *ovndb) aclListImp(lsw string) ([]*ACL, error) {
303-
var listACL []*ACL
304-
305304
odbi.cachemutex.RLock()
306305
defer odbi.cachemutex.RUnlock()
307306

308307
cacheLogicalSwitch, ok := odbi.cache[TableLogicalSwitch]
309308
if !ok {
310309
return nil, ErrorNotFound
311310
}
312-
var lsFound bool
313311
for _, drows := range cacheLogicalSwitch {
314312
if rlsw, ok := drows.Fields["name"].(string); ok && rlsw == lsw {
315313
acls := drows.Fields["acls"]
316314
if acls != nil {
317315
switch acls.(type) {
318316
case libovsdb.OvsSet:
319317
if as, ok := acls.(libovsdb.OvsSet); ok {
318+
listACL := make([]*ACL, 0, len(as.GoSet))
320319
for _, a := range as.GoSet {
321320
if va, ok := a.(libovsdb.UUID); ok {
322321
ta := odbi.rowToACL(va.GoUUID)
323322
listACL = append(listACL, ta)
323+
} else {
324+
return nil, fmt.Errorf("type libovsdb.UUID casting failed")
324325
}
325326
}
327+
return listACL, nil
328+
} else {
329+
return nil, fmt.Errorf("type libovsdb.OvsSet casting failed")
326330
}
327331
case libovsdb.UUID:
328332
if va, ok := acls.(libovsdb.UUID); ok {
329333
ta := odbi.rowToACL(va.GoUUID)
330-
listACL = append(listACL, ta)
334+
return []*ACL{ta}, nil
335+
} else {
336+
return nil, fmt.Errorf("type libovsdb.UUID casting failed")
331337
}
332338
}
333339
}
334-
lsFound = true
335-
break
340+
return []*ACL{}, nil
336341
}
337342
}
338-
if !lsFound {
339-
return nil, ErrorNotFound
340-
}
341-
return listACL, nil
343+
return nil, ErrorNotFound
342344
}

address_set.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ func (odbi *ovndb) asDelImp(name string) (*OvnCommand, error) {
113113

114114
// Get all addressset
115115
func (odbi *ovndb) asListImp() ([]*AddressSet, error) {
116-
var listAS []*AddressSet
117-
118116
odbi.cachemutex.RLock()
119117
defer odbi.cachemutex.RUnlock()
120118

@@ -123,6 +121,7 @@ func (odbi *ovndb) asListImp() ([]*AddressSet, error) {
123121
return nil, ErrorSchema
124122
}
125123

124+
listAS := make([]*AddressSet, 0, len(cacheAddressSet))
126125
for uuid, drows := range cacheAddressSet {
127126
ta := &AddressSet{
128127
UUID: uuid,

chassis.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ func (odbi *ovndb) chassisDelImp(name string) (*OvnCommand, error) {
124124
}
125125

126126
func (odbi *ovndb) chassisListImp() ([]*Chassis, error) {
127-
var listChassis []*Chassis
128-
129127
odbi.cachemutex.RLock()
130128
defer odbi.cachemutex.RUnlock()
131129

@@ -135,6 +133,7 @@ func (odbi *ovndb) chassisListImp() ([]*Chassis, error) {
135133
return nil, ErrorSchema
136134
}
137135

136+
listChassis := make([]*Chassis, 0, len(cacheChassis))
138137
for uuid := range cacheChassis {
139138
ch, err := odbi.rowToChassis(uuid)
140139
if err != nil {

dhcp_options.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ func (odbi *ovndb) dhcpOptionsDelImp(uuid string) (*OvnCommand, error) {
144144

145145
// List all dhcp options
146146
func (odbi *ovndb) dhcpOptionsListImp() ([]*DHCPOptions, error) {
147-
var listDHCP []*DHCPOptions
148-
149147
odbi.cachemutex.RLock()
150148
defer odbi.cachemutex.RUnlock()
151149

@@ -154,6 +152,7 @@ func (odbi *ovndb) dhcpOptionsListImp() ([]*DHCPOptions, error) {
154152
return nil, ErrorSchema
155153
}
156154

155+
listDHCP := make([]*DHCPOptions, 0, len(cacheDHCPOptions))
157156
for uuid := range cacheDHCPOptions {
158157
listDHCP = append(listDHCP, odbi.rowToDHCPOptions(uuid))
159158
}

encap.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ type Encap struct {
3131
}
3232

3333
func (odbi *ovndb) encapListImp(chassisName string) ([]*Encap, error) {
34-
3534
odbi.cachemutex.RLock()
3635
defer odbi.cachemutex.RUnlock()
3736

@@ -40,8 +39,6 @@ func (odbi *ovndb) encapListImp(chassisName string) ([]*Encap, error) {
4039
return nil, ErrorNotFound
4140
}
4241

43-
var encaps []*Encap
44-
var chFound bool
4542
for _, drows := range cacheChassis {
4643
if ch, ok := drows.Fields["name"].(string); ok && ch == chassisName {
4744
if enc, ok := drows.Fields["encaps"]; ok {
@@ -52,35 +49,34 @@ func (odbi *ovndb) encapListImp(chassisName string) ([]*Encap, error) {
5249
if err != nil {
5350
return nil, err
5451
}
55-
encaps = append(encaps, cenc)
52+
return []*Encap{cenc}, nil
5653
} else {
5754
return nil, fmt.Errorf("type libovsdb.UUID casting failed")
5855
}
5956
case libovsdb.OvsSet:
6057
if en, ok := enc.(libovsdb.OvsSet); ok {
58+
encaps := make([]*Encap, 0, len(en.GoSet))
6159
for _, e := range en.GoSet {
6260
if euid, ok := e.(libovsdb.UUID); ok {
6361
enc, err := odbi.rowToEncap(euid.GoUUID)
6462
if err != nil {
6563
return nil, err
6664
}
6765
encaps = append(encaps, enc)
66+
} else {
67+
return nil, fmt.Errorf("type libovsdb.UUID casting failed")
6868
}
6969
}
70+
return encaps, nil
7071
} else {
7172
return nil, fmt.Errorf("type libovsdb.OvsSet casting failed")
7273
}
7374
}
7475
}
75-
chFound = true
76-
break
76+
return []*Encap{}, nil
7777
}
7878
}
79-
if !chFound {
80-
return nil, ErrorNotFound
81-
}
82-
return encaps, nil
83-
79+
return nil, ErrorNotFound
8480
}
8581

8682
func (odbi *ovndb) rowToEncap(uuid string) (*Encap, error) {

logical_router.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,8 @@ func (odbi *ovndb) rowToLogicalRouter(uuid string) *LogicalRouter {
213213
return lr
214214
}
215215

216-
// Get all logical switches
216+
// Get all logical routers
217217
func (odbi *ovndb) lrListImp() ([]*LogicalRouter, error) {
218-
var listLR []*LogicalRouter
219-
220218
odbi.cachemutex.RLock()
221219
defer odbi.cachemutex.RUnlock()
222220

@@ -225,6 +223,7 @@ func (odbi *ovndb) lrListImp() ([]*LogicalRouter, error) {
225223
return nil, ErrorNotFound
226224
}
227225

226+
listLR := make([]*LogicalRouter, 0, len(cacheLogicalRouter))
228227
for uuid := range cacheLogicalRouter {
229228
listLR = append(listLR, odbi.rowToLogicalRouter(uuid))
230229
}
@@ -296,31 +295,33 @@ func (odbi *ovndb) lrlbDelImp(lr string, lb string) (*OvnCommand, error) {
296295
}
297296

298297
func (odbi *ovndb) lrlbListImp(lr string) ([]*LoadBalancer, error) {
299-
var listLB []*LoadBalancer
300298
odbi.cachemutex.RLock()
301299
defer odbi.cachemutex.RUnlock()
302300

303301
cacheLogicalRouter, ok := odbi.cache[TableLogicalRouter]
304302
if !ok {
305303
return nil, ErrorSchema
306304
}
307-
var lrFound bool
308305
for _, drows := range cacheLogicalRouter {
309306
if router, ok := drows.Fields["name"].(string); ok && router == lr {
310307
lbs := drows.Fields["load_balancer"]
311308
if lbs != nil {
312309
switch lbs.(type) {
313310
case libovsdb.OvsSet:
314311
if lb, ok := lbs.(libovsdb.OvsSet); ok {
312+
listLB := make([]*LoadBalancer, 0, len(lb.GoSet))
315313
for _, l := range lb.GoSet {
316314
if lb, ok := l.(libovsdb.UUID); ok {
317315
lb, err := odbi.rowToLB(lb.GoUUID)
318316
if err != nil {
319317
return nil, err
320318
}
321319
listLB = append(listLB, lb)
320+
} else {
321+
return nil, fmt.Errorf("type libovsdb.UUID casting failed")
322322
}
323323
}
324+
return listLB, nil
324325
} else {
325326
return nil, fmt.Errorf("type libovsdb.OvsSet casting failed")
326327
}
@@ -330,20 +331,16 @@ func (odbi *ovndb) lrlbListImp(lr string) ([]*LoadBalancer, error) {
330331
if err != nil {
331332
return nil, err
332333
}
333-
listLB = append(listLB, lb)
334+
return []*LoadBalancer{lb}, nil
334335
} else {
335336
return nil, fmt.Errorf("type libovsdb.UUID casting failed")
336337
}
337338
default:
338339
return nil, fmt.Errorf("Unsupport type found in ovsdb rows")
339340
}
340341
}
341-
lrFound = true
342-
break
342+
return []*LoadBalancer{}, nil
343343
}
344344
}
345-
if !lrFound {
346-
return nil, ErrorNotFound
347-
}
348-
return listLB, nil
345+
return nil, ErrorNotFound
349346
}

logical_router_port.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ func (odbi *ovndb) rowToLogicalRouterPort(uuid string) *LogicalRouterPort {
183183
}
184184

185185
func (odbi *ovndb) lrpListImp(lr string) ([]*LogicalRouterPort, error) {
186-
var listLRP []*LogicalRouterPort
187-
188186
odbi.cachemutex.RLock()
189187
defer odbi.cachemutex.RUnlock()
190188

@@ -193,40 +191,39 @@ func (odbi *ovndb) lrpListImp(lr string) ([]*LogicalRouterPort, error) {
193191
return nil, ErrorNotFound
194192
}
195193

196-
var lrFound bool
197194
for _, drows := range cacheLogicalRouter {
198195
if rlr, ok := drows.Fields["name"].(string); ok && rlr == lr {
199196
ports := drows.Fields["ports"]
200197
if ports != nil {
201198
switch ports.(type) {
202199
case libovsdb.OvsSet:
203200
if ps, ok := ports.(libovsdb.OvsSet); ok {
201+
listLRP := make([]*LogicalRouterPort, 0, len(ps.GoSet))
204202
for _, p := range ps.GoSet {
205203
if vp, ok := p.(libovsdb.UUID); ok {
206204
tp := odbi.rowToLogicalRouterPort(vp.GoUUID)
207205
listLRP = append(listLRP, tp)
206+
} else {
207+
return nil, fmt.Errorf("type libovsdb.UUID casting failed")
208208
}
209209
}
210+
return listLRP, nil
210211
} else {
211212
return nil, fmt.Errorf("type libovsdb.OvsSet casting failed")
212213
}
213214
case libovsdb.UUID:
214215
if vp, ok := ports.(libovsdb.UUID); ok {
215216
tp := odbi.rowToLogicalRouterPort(vp.GoUUID)
216-
listLRP = append(listLRP, tp)
217+
return []*LogicalRouterPort{tp}, nil
217218
} else {
218219
return nil, fmt.Errorf("type libovsdb.UUID casting failed")
219220
}
220221
default:
221222
return nil, fmt.Errorf("Unsupport type found in ovsdb rows")
222223
}
223224
}
224-
lrFound = true
225-
break
225+
return []*LogicalRouterPort{}, nil
226226
}
227227
}
228-
if !lrFound {
229-
return nil, ErrorNotFound
230-
}
231-
return listLRP, nil
228+
return nil, ErrorNotFound
232229
}

logical_router_static_route.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,50 +195,47 @@ func (odbi *ovndb) rowToLogicalRouterStaticRoute(uuid string) *LogicalRouterStat
195195
}
196196

197197
func (odbi *ovndb) lrsrListImp(lr string) ([]*LogicalRouterStaticRoute, error) {
198-
var listLRSR []*LogicalRouterStaticRoute
199-
200198
odbi.cachemutex.RLock()
201199
defer odbi.cachemutex.RUnlock()
202200

203201
cacheLogicalRouter, ok := odbi.cache[TableLogicalRouter]
204202
if !ok {
205203
return nil, ErrorNotFound
206204
}
207-
var lrFound bool
208205
for _, drows := range cacheLogicalRouter {
209206
if rlr, ok := drows.Fields["name"].(string); ok && rlr == lr {
210207
staticRoutes := drows.Fields["static_routes"]
211208
if staticRoutes != nil {
212209
switch staticRoutes.(type) {
213210
case libovsdb.OvsSet:
214211
if sr, ok := staticRoutes.(libovsdb.OvsSet); ok {
212+
listLRSR := make([]*LogicalRouterStaticRoute, 0, len(sr.GoSet))
215213
for _, s := range sr.GoSet {
216214
if sruid, ok := s.(libovsdb.UUID); ok {
217215
rsr := odbi.rowToLogicalRouterStaticRoute(sruid.GoUUID)
218216
listLRSR = append(listLRSR, rsr)
217+
} else {
218+
return nil, fmt.Errorf("type libovsdb.UUID casting failed")
219219
}
220220
}
221+
return listLRSR, nil
221222
} else {
222223
return nil, fmt.Errorf("type libovsdb.OvsSet casting failed")
223224
}
224225
case libovsdb.UUID:
225226
if sruid, ok := staticRoutes.(libovsdb.UUID); ok {
226227
rsr := odbi.rowToLogicalRouterStaticRoute(sruid.GoUUID)
227-
listLRSR = append(listLRSR, rsr)
228+
return []*LogicalRouterStaticRoute{rsr}, nil
228229
} else {
229230
return nil, fmt.Errorf("type libovsdb.UUID casting failed")
230231
}
231232
default:
232233
return nil, fmt.Errorf("Unsupport type found in ovsdb rows")
233234
}
234235
}
235-
lrFound = true
236-
break
236+
return []*LogicalRouterStaticRoute{}, nil
237237
}
238238
}
239239

240-
if !lrFound {
241-
return nil, ErrorNotFound
242-
}
243-
return listLRSR, nil
240+
return nil, ErrorNotFound
244241
}

0 commit comments

Comments
 (0)