Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve IPFIX Set implementation #390

Merged
merged 2 commits into from
Dec 20, 2024

Conversation

antoninbas
Copy link
Member

@antoninbas antoninbas commented Dec 19, 2024

  • Optimize ResetSet to avoid memory allocations
  • Add new interface method AddRecordV3 which takes a Record as a parameter (instead of a list of IEs). AddRecordV3 can avoid all memory allocations.

We also add a new GetRecordType method to the Record interface.

@antoninbas
Copy link
Member Author

Benchmark results without ResetSet improvements:

C02F16ASMD6R:go-ipfix abas$ go test -run=xxx -bench=BenchmarkSet -benchmem ./pkg/entities/
goos: darwin
goarch: amd64
pkg: github.com/vmware/go-ipfix/pkg/entities
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkSet-16    	 4014678	       303.4 ns/op	     500 B/op	       6 allocs/op
PASS
ok  	github.com/vmware/go-ipfix/pkg/entities	1.861s

And with the improvements:

C02F16ASMD6R:go-ipfix abas$ go test -run=xxx -bench=BenchmarkSet -benchmem ./pkg/entities/
goos: darwin
goarch: amd64
pkg: github.com/vmware/go-ipfix/pkg/entities
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkSet-16    	16009188	        74.63 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/vmware/go-ipfix/pkg/entities	1.631s

@antoninbas antoninbas force-pushed the improve-set-implementation branch from b7d06dd to 4f56128 Compare December 19, 2024 20:51
@@ -45,6 +46,8 @@ const (

type Set interface {
PrepareSet(setType ContentType, templateID uint16) error
// Call ResetSet followd by a new call to PrepareSet in order to reuse an existing Set,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Call ResetSet followd by a new call to PrepareSet in order to reuse an existing Set,
// Call ResetSet followed by a new call to PrepareSet in order to reuse an existing Set,

s.length = SetHeaderLen
}
s.setType = Undefined
s.records = nil
s.records = make([]Record, 0)
// Clear before shrinking the slice so that existing elements are eligible for garbage collection.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to reset the s.length here? I'm not quite sure what's the case when it isDecoding.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can reset it to 0, even though it's a change to the existing behavior. I agree that it would make more sense.
I am not a big fan of the isDecoding flag everywhere. We should have Marhsal / Unmarshal methods as appropriate, but that will have to be for a later time...

// Sanity check: we need to make sure that the record is allowed to be added.
recordType := record.GetRecordType()
if recordType != s.setType {
return fmt.Errorf("Record and Set types don't match")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("Record and Set types don't match")
return fmt.Errorf("record and Set types don't match")

return fmt.Errorf("Record and Set types don't match")
}
if recordType == Data && record.GetTemplateID() != s.templateID {
return fmt.Errorf("All Data Records in the same Data Set must have the same template ID")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("All Data Records in the same Data Set must have the same template ID")
return fmt.Errorf("all Data Records in the same Data Set must have the same template ID")

* Optimize ResetSet to avoid memory allocations
* Add new interface method AddRecordV3 which takes a Record as a
  parameter (instead of a list of IEs). AddRecordV3 can avoid all memory
  allocations.

We also add a new GetRecordType method to the Record interface.

Signed-off-by: Antonin Bas <antonin.bas@broadcom.com>
Signed-off-by: Antonin Bas <antonin.bas@broadcom.com>
@antoninbas antoninbas force-pushed the improve-set-implementation branch from 4f56128 to e18a3d8 Compare December 20, 2024 19:47
@antoninbas
Copy link
Member Author

@yuntanghsu Thanks for the review, I addressed comments

Copy link
Contributor

@yuntanghsu yuntanghsu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@antoninbas antoninbas merged commit a21ca40 into vmware:main Dec 20, 2024
7 checks passed
@antoninbas antoninbas deleted the improve-set-implementation branch December 20, 2024 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants