Skip to content

Commit

Permalink
Used a pool of slice in order to speed up single value attribute conv…
Browse files Browse the repository at this point in the history
…ersions
  • Loading branch information
lkarlslund committed May 30, 2022
1 parent 1a99685 commit 881c409
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions modules/integrations/activedirectory/rawobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (r *RawObject) ToObject(onlyKnownAttributes bool) *engine.Object {
}

encodedvals := EncodeAttributeData(attribute, values)
if len(encodedvals) > 0 {
result.SetValues(attribute, encodedvals...)
if encodedvals != nil {
result.Set(attribute, encodedvals)
}
}

Expand All @@ -61,12 +61,25 @@ func (r *RawObject) IngestLDAP(source *ldap.Entry) error {
return nil
}

func EncodeAttributeData(attribute engine.Attribute, values []string) engine.AttributeValueSlice {
avs := make(engine.AttributeValueSlice, len(values))
// Performance hack
var avsPool sync.Pool

func init() {
avsPool.New = func() interface{} {
return make(engine.AttributeValueSlice, 0, 16)
}
}

func EncodeAttributeData(attribute engine.Attribute, values []string) engine.AttributeValues {
if len(values) == 0 {
return nil
}

avs := avsPool.Get().(engine.AttributeValueSlice)

var skipped int

for valindex, value := range values {
for _, value := range values {
var attributevalue engine.AttributeValue
switch attribute {
// Add more things here, like time decoding etc
Expand Down Expand Up @@ -175,10 +188,25 @@ func EncodeAttributeData(attribute engine.Attribute, values []string) engine.Att
}

if attributevalue != nil {
avs[valindex-skipped] = attributevalue
avs = append(avs, attributevalue)
} else {
skipped++
}
}
return avs[:len(values)-skipped]

var result engine.AttributeValues

switch len(avs) {
case 0:
return nil
case 1:
result = engine.AttributeValueOne{avs[0]}
default:
new := make(engine.AttributeValueSlice, len(avs))
copy(new, avs)
result = new
}

avsPool.Put(avs[:0])
return result
}

0 comments on commit 881c409

Please sign in to comment.