Skip to content

Commit

Permalink
perf: proto memory alignment (#106)
Browse files Browse the repository at this point in the history
* perf: proto memory alignment

* docs: fix comment on basetype for bool

* generate: factory

* perf: make readByte inline call
  • Loading branch information
muktihari authored Jan 22, 2024
1 parent c658153 commit 8f3753e
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 83 deletions.
9 changes: 3 additions & 6 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,8 @@ func (d *Decoder) expandComponents(mesg *proto.Message, containingField *proto.F

componentScaled := scaleoffset.Apply(val, component.Scale, component.Offset)
val = uint32(scaleoffset.Discard(componentScaled, componentField.Scale, componentField.Offset))
value := valueFromBits(val, componentField.Type.BaseType())
componentField.Value = valueFromBits(val, componentField.Type.BaseType())

componentField.Value = value
mesg.Fields = append(mesg.Fields, componentField)

// The destination field (componentField) can itself contain components requiring expansion.
Expand Down Expand Up @@ -795,10 +794,8 @@ func (d *Decoder) read(b []byte) error {
// readByte is shorthand for read([1]byte).
func (d *Decoder) readByte() (byte, error) {
b := d.bytesArray[:1]
if err := d.read(b); err != nil {
return 0, err
}
return b[0], nil
err := d.read(b)
return b[0], err
}

// readValue reads message value bytes from reader and convert it into its corresponding type.
Expand Down
40 changes: 37 additions & 3 deletions decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1862,13 +1862,15 @@ func BenchmarkDecodeMessageData(b *testing.B) {
b.StopTimer()
mesg := factory.CreateMesg(typedef.MesgNumRecord)
mesgDef := proto.CreateMessageDefinition(&mesg)
dec := New(nil, WithIgnoreChecksum(), WithNoComponentExpansion())
dec.localMessageDefinitions[0] = &mesgDef
mesgb, _ := mesg.MarshalBinary()
buf := bytes.NewBuffer(mesgb)
dec := New(buf, WithIgnoreChecksum(), WithNoComponentExpansion(), WithBroadcastOnly())
dec.localMessageDefinitions[0] = &mesgDef
b.StartTimer()

for i := 0; i < b.N; i++ {
dec.r = bytes.NewBuffer(mesgb)
buf.Reset()
buf.Write(mesgb)
err := dec.decodeMessageData(0)
if err != nil {
b.Fatal(err)
Expand All @@ -1891,11 +1893,43 @@ func BenchmarkDecode(b *testing.B) {
if err != nil {
panic(err)
}

buf := bytes.NewBuffer(all)
dec := New(buf)
b.StartTimer()

for i := 0; i < b.N; i++ {
buf.Reset()
buf.Write(all)
_, err = dec.Decode()
if err != nil {
b.Fatal(err)
}
dec.reset()
}
}

func BenchmarkDecodeWithFiledef(b *testing.B) {
b.StopTimer()
// This is not a typical FIT in term of file size (2.3M) and the messages it contains (200.000 messages)
// But since it's big, it's should be good to benchmark.
f, err := os.Open("../testdata/big_activity.fit")
// f, err := os.Open("../testdata/from_official_sdk/activity_lowbattery.fit")
if err != nil {
panic(err)
}
defer f.Close()

all, err := io.ReadAll(f)
if err != nil {
panic(err)
}

al := filedef.NewListener()
buf := bytes.NewBuffer(all)
dec := New(buf, WithMesgListener(al), WithBroadcastOnly())
b.StartTimer()

for i := 0; i < b.N; i++ {
buf.Reset()
buf.Write(all)
Expand Down
Loading

0 comments on commit 8f3753e

Please sign in to comment.