Skip to content

Commit

Permalink
⚡ use copy instead of proto.clone for property
Browse files Browse the repository at this point in the history
Instead of using reflections to copy this object, manually copy the object instead. We have a bit more maintenance in the code, but gain a good chunk of speed and memory-friendliness with this approach.

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
  • Loading branch information
arlimus committed Jan 26, 2024
1 parent b130500 commit a9a488c
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions explorer/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"go.mondoo.com/cnquery/v10/mrn"
"go.mondoo.com/cnquery/v10/types"
"go.mondoo.com/cnquery/v10/utils/multierr"
"google.golang.org/protobuf/proto"
)

// RefreshMRN computes a MRN from the UID or validates the existing MRN.
Expand Down Expand Up @@ -118,6 +117,33 @@ func (p *Property) Merge(base *Property) {
}
}

func (p *Property) Clone() *Property {
res := &Property{
Mql: p.Mql,
CodeId: p.CodeId,
Checksum: p.Checksum,
Mrn: p.Mrn,
Uid: p.Uid,
Type: p.Type,
Context: p.Context,
Title: p.Title,
Desc: p.Desc,
}

if p.For != nil {
res.For = make([]*ObjectRef, len(p.For))
for i := range p.For {
fr := p.For[i]
res.For[i] = &ObjectRef{
Mrn: fr.Mrn,
Uid: fr.Uid,
}
}
}

return res
}

type PropsCache struct {
cache map[string]*Property
uidOnlyProps map[string]*Property
Expand Down Expand Up @@ -148,7 +174,7 @@ func (c PropsCache) Add(props ...*Property) {
if base.Mrn != "" {
name, _ := mrn.GetResource(base.Mrn, MRN_RESOURCE_QUERY)
if uidProp, ok := c.uidOnlyProps[name]; ok {
p := proto.Clone(uidProp).(*Property)
p := uidProp.Clone()
p.Merge(base)
base = p
merged = p
Expand Down Expand Up @@ -186,7 +212,7 @@ func (c PropsCache) Get(propMrn string) (*Property, string, error) {
if uidProp, ok := c.uidOnlyProps[name]; ok {
// We have a property that was specified by uid only. We need to merge it in
// to get the full property.
p := proto.Clone(uidProp).(*Property)
p := uidProp.Clone()
p.Merge(res)
return p, name, nil
} else {
Expand Down

0 comments on commit a9a488c

Please sign in to comment.