euanwm / OpenWeightlifting

Repository that is deployed for the OpenWeightlifting site
https://www.openweightlifting.org
BSD 3-Clause "New" or "Revised" License
31 stars 21 forks source link

feat(backend): Use fixed-point WeightKg for weights. Closes #363 #365

Open sstangl opened 1 month ago

sstangl commented 1 month ago
This patch ports the OpenPowerlifting weight representation to Go.

The new WeightKg type represents numbers like `123.45` by storing them
in a fixed-point integer representation, like `12345`. The advantage of
this representation is that integer comparisons and arithmetic are
significantly faster than the corresponding floating-point operations,
because integers do not require loads into XMM registers.
sstangl commented 1 month ago

This is the first non-trivial Go code I've written, so please be skeptical if something looks unidiomatic. It would be nice to have a benchmark for this in the test suite -- if there's a preferred entrypoint for loading the DB and performing searches, please let me know.

I benchmarked this by manually disabling the server's cache and investigating /leaderboard. Performance seems largely unchanged, maybe slightly faster, but the majority of overhead appears to be elsewhere. Response times are extremely variable in both development and this branch, between 7ms and 60ms, although I haven't investigated enough to blame GC yet.

euanwm commented 1 month ago

I'm going to leave this open while I crack on with the lower-hanging fruit this week as jet-lag will be seeping into my grey matter very shortly.

Check out the benchmarking suite in Go (testing.B), I've used it a bunch on other projects and it's super handy at measuring these sort of improvements.

I'll create an issue for myself to tidy some stuff up to allow for easier benchmarking without having to do it manually.