Reduce memory usage by implementing autoload for REST and HTTP modules #756
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The twilio-ruby library was consuming excessive memory due to eager loading of all REST API modules. When accessing
Twilio::REST
, the library would immediately load all 630+ REST API files viaDir.glob
statements, consuming ~73MB of memory even when only a few services were actually used.Solution
Implemented autoload for REST service modules and HTTP components to enable on-demand loading:
Changes Made
REST Module Autoload: Replaced eager loading in
lib/twilio-ruby/rest.rb
with autoload declarations for all service modules (Api, Messaging, Chat, etc.)Version Class Autoload: Added autoload for version classes within commonly used services:
Api::V2010
Messaging::V1
,Messaging::V2
Chat::V1
,Chat::V2
,Chat::V3
Notify::V1
Sync::V1
HTTP Module Autoload: Replaced eager loading in
lib/twilio-ruby/http.rb
with autoload for HTTP components (Client
,ClientTokenManager
,OrgTokenManager
)Results
Testing
This change significantly improves the memory footprint of applications using twilio-ruby, especially beneficial for containerized deployments and applications that only use a subset of Twilio's services.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.