From a62e09b76d7a8f606878444a522d86abb8f3d384 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Thu, 25 Jan 2024 22:53:40 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20use=20copy=20instead=20of=20proto.c?= =?UTF-8?q?lone=20for=20property=20(#3126)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- explorer/property.go | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/explorer/property.go b/explorer/property.go index 7aa7c05212..1f3ffb6ff9 100644 --- a/explorer/property.go +++ b/explorer/property.go @@ -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. @@ -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 @@ -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 @@ -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 {