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

perf: proto memory alignment #106

Merged
merged 4 commits into from
Jan 22, 2024
Merged

perf: proto memory alignment #106

merged 4 commits into from
Jan 22, 2024

Conversation

muktihari
Copy link
Owner

Let's assume we compile our program for 64 bit machine, which memory block is every 8 bytes.
Previously, we have this proto.FieldBase struct having  112 bytes as follows:

type FieldBase struct {
	Name       string // 16
	Num        byte // 1
	Type       profile.ProfileType // 2
	Array      bool // 1
        // +4 padding bytes
	Scale      float64 // 8
	Offset     float64 // 8
	Units      string // 16
	Accumulate bool // 1
        // +7 padding bytes
	Components []Component // 24
	SubFields  []SubField // 24
}

By re-arrange the struct order, we can save 8 bytes, so the size is now 104 bytes per proto.FieldBase:

type FieldBase struct {
	Name       string // 16
	Num        byte // 1
	Type       profile.ProfileType // 2
	Array      bool // 1
	Accumulate bool // 1
        // +3 padding bytes 
	Scale      float64 // 8
	Offset     float64 // 8
	Units      string // 16
	Components []Component // 24
	SubFields  []SubField // 24
}

At the moment we have 394 predefined messages in the factory containing 1307 fields in total, we save 1307 * 8 = 10.456 bytes (10KB). We also aligning the proto.Component to save 8 bytes, we save approx. 600 bytes. And we also align proto.DeveloperField but this depend on user input. Total 11KB, sound small but it can accumulate if user add Product Profile (manufacturer specific message). In general, it's always good to save what we can save.

Aside from that change, we also make (*Decoder).readByte as inline call by reducing unnecessary condition cost:

  • from ./decoder.go:796:6: cannot inline (*Decoder).readByte: function too complex: cost 86 exceeds budget 80
  • to ./decoder.go:796:6: can inline (*Decoder).readByte with cost 79

It's ok to return the value of b[0] even if error occurs, since in our convention we will ignore the value when error occurs on reading bytes.

@muktihari muktihari added the performance Changes related to performance improvement label Jan 22, 2024
@muktihari muktihari self-assigned this Jan 22, 2024
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c658153) 99.90% compared to head (baf273b) 99.90%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #106      +/-   ##
==========================================
- Coverage   99.90%   99.90%   -0.01%     
==========================================
  Files          43       43              
  Lines        4089     4076      -13     
==========================================
- Hits         4085     4072      -13     
  Misses          4        4              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@muktihari muktihari merged commit 8f3753e into master Jan 22, 2024
1 check passed
@muktihari muktihari deleted the perf/proto-memory-alignment branch January 22, 2024 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Changes related to performance improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants