Skip to content

Commit

Permalink
perf: resmap: Add AppendMany function
Browse files Browse the repository at this point in the history
Introduce a new function, AppendMany, which can be used to safely add
entries to a resmap.

The function handles ID conflict detection, which was the most expensive
operation causing significant performance issues.

The ID cache is local to this function as we cannot guarantee that all
mutations are performed using this function.
  • Loading branch information
chlunde committed Apr 16, 2024
1 parent 0990677 commit f5cdc84
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/resmap/resmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ type ResMap interface {
// in the cluster with the same Id.
Append(*resource.Resource) error

AppendMany(res ...*resource.Resource) error

// AppendAll appends another ResMap to self,
// failing on any CurId collision.
AppendAll(ResMap) error
Expand Down
21 changes: 21 additions & 0 deletions api/resmap/reswrangler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ func (m *resWrangler) Append(res *resource.Resource) error {
return nil
}

// Append implements ResMap.
func (m *resWrangler) AppendMany(resources ...*resource.Resource) error {
ids := m.AllIds()
seen := make(map[resid.ResId]struct{}, len(ids)+len(resources))
for _, id := range ids {
seen[id] = struct{}{}
}

for _, res := range resources {
newId := res.CurId()
if _, ok := seen[newId]; ok {
return fmt.Errorf(
"may not add resource with an already registered id: %s", newId)
}
seen[newId] = struct{}{}
m.append(res)
}

return nil
}

// append appends without performing an Id check
func (m *resWrangler) append(res *resource.Resource) {
m.rList = append(m.rList, res)
Expand Down

0 comments on commit f5cdc84

Please sign in to comment.