-
Notifications
You must be signed in to change notification settings - Fork 214
lsm6ds3tr: avoid unnecessary heap allocations #766
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
base: release
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit that I think would do wonders for readability. I'd love to feature this in a youtube video once it is done if y'all don't mind!
lsm6ds3tr/lsm6ds3tr.go
Outdated
d.buf[0] = CTRL2_G | ||
d.buf[1] = uint8(d.gyroRange) | uint8(d.gyroSampleRate) | ||
err = d.bus.Tx(d.Address, d.buf[0:2], nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we abstract these calls to something like d.writeReg8/readReg8
?
func (d *Device) writeReg8(addr, value byte) error {
d.buf[0] = addr
d.buf[1] = value
return err = d.bus.Tx(d.Address, d.buf[0:2], nil)
}
func (d *Device) readReg8(addr byte) (byte,error) {
d.buf[0] = addr
err := d.bus.Tx(d.Address, d.buf[0:1], d.buf[1:2])
return d.buf[1], err
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just one of many drivers that scream for such improvement, a trial balloon if you wish.
Each of these drivers will have to repeat the pattern.
Any ideas how could we implement these helper functions once and re-use them, instead of implementing in every driver anew?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've created regmap. Thanks for the suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add them for now and we can port it to use regmap after regmap has been merged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I agree with @soypat some helper methods would make the code a bit easier to read.
Nothing against from my side. All kudos to @aykevl for the idea, btw. |
Alright, I've implemented the helper functions. Had to implement not only pure read and write, but Also, I've decided to simply pass value size to An alternative could be we return read data w/o offset, i.e. read into the shared buffer from index 0. |
6e58af2
to
890aae3
Compare
890aae3
to
a6949c5
Compare
It is unclear to me what is gained from sharing the first byte, other than 1 byte of memory (likely not gained due to alignment requirements). If for some reason you want your data read to be at the first byte of the buffer you can declare two static buffers: type Dev struct { cmdbuf [1]byte; databuf [N]byte }
func (d *Dev) read(n int) error {
return d.bus.tx(addr, d.cmdbuf[:], d.databuf[:n])
} Now the intention is clear, there is a databuf which accumulates data read which is already quite self-documenting |
This re-uses internal buffer to avoid unnecessary heap allocations in lsm6ds3tr driver; switches away from the legacy I2C interface.