|
|
|
|
|
by whelton
1683 days ago
|
|
I use some Go in Conjure's [1] Rails backend through FFI, for calculating Habit streaks and have had huge speed improvements. Rewriting from Ruby to Go, some benchmarks for calculating long streaks showed going from ~5 sec to ~0.04 sec [2]. Conjure's habits use the iCalendar Recurrence Rule format (RRULE [3]) for their frequency, along with one or two other considerations (like Habit Completion Types and streak effects), which makes evaluating streaks more expensive (as you have to work backwards through habit completions one by one based on the next occurrence). Data is still fetched from Postgres in Rails and passed to the compiled Go library and Ruby/Rails handles the results (persisting, notifying over web sockets, etc)[4]. I modified Conjure's Heroku buildpack to install Go and build on deploy. No doubt there is further optimizations that can be made, but this approach has allowed me to sprinkle Go into a Rails app and progressively optimize certain things when needed. [1] https://conjure.so/
[2] https://twitter.com/Whelton/status/1370482793558458370
[3] https://icalendar.org/iCalendar-RFC-5545/3-8-5-3-recurrence-...
[4] https://jondot.medium.com/ruby-and-go-sitting-in-a-tree-38c1... |
|
This is why sometimes it's worth looking for obscure methods in the core or standard library and use them instead of writing Ruby code, especially when iterating on Enumerables. Basically it's rewriting Ruby in C.
Entry level example: minmax_by [1] which does the obvious thing but it's not obvious that every Ruby developer knows about it (disclaimer: I went to the docs and looked for something with a fun name.)
[1] https://ruby-doc.org/core-3.0.1/Enumerable.html#method-i-min...