twilio / twilio-ruby

A Ruby gem for communicating with the Twilio API and generating TwiML
MIT License
1.35k stars 462 forks source link

feat: Autoload Twilio::REST and Twilio::HTTP #558

Closed gmcgibbon closed 3 years ago

gmcgibbon commented 3 years ago

Currently Twilio::REST and Twilio::HTTP are loaded with file globs. This works great, but it is slow. Especially for Twilio::REST which has lots of files. For applications using this gem, bundler will require "twilio-ruby" and load the entire gem at once.

Ruby's autoload feature can help with this. Autoload defers loading modules until they are actually needed/referenced. By autoloading Twilio::REST and Twilio::HTTP, the require-time of this library is reduced from ~400ms to ~25ms. You can verify this by wrapping the require in your spec helper with a benchmark block:

require "benchmark"
ms = 1000 * Benchmark.realtime do
  require 'twilio-ruby'
end
puts "Require took #{ms}ms"

This is what the gem require currently looks like in our application, with the majority of time taken by requiring Twilio::REST:

Screen Shot 2021-06-10 at 12 23 37 PM

After this patch, it did not show up in our profiles.

Checklist

If you have questions, please file a support ticket, or create a GitHub Issue in this repository.