Skip to content

Commit

Permalink
Reduce initial memory usage by auto-loading bundled gems (2) (#3088)
Browse files Browse the repository at this point in the history
  • Loading branch information
alextwoods committed Aug 26, 2024
1 parent 8a53163 commit dd8f329
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 15 deletions.
2 changes: 2 additions & 0 deletions gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Reduce initial memory usage by auto-loading bundled gems (STS, SSO, SSOOIDC).

3.201.5 (2024-08-15)
------------------

Expand Down
21 changes: 12 additions & 9 deletions gems/aws-sdk-core/lib/aws-sdk-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,7 @@
# defaults
require_relative 'aws-defaults'

# plugins
# loaded through building STS or SSO ..

# aws-sdk-sts is included to support Aws::AssumeRoleCredentials
require_relative 'aws-sdk-sts'

# aws-sdk-sso is included to support Aws::SSOCredentials
require_relative 'aws-sdk-sso'
require_relative 'aws-sdk-ssooidc'
# plugins - loaded through service clients as needed.

module Aws

Expand Down Expand Up @@ -205,3 +197,14 @@ def eager_autoload!(*args)

end
end

# Autoload bundled service gems used in credential providers
# need to ensure that the files are the local files from aws-sdk-core
# and not the installed, legacy/dummy service gems.

# aws-sdk-sts is included to support Aws::AssumeRoleCredentials
Aws.autoload :STS, File.join(__dir__, 'aws-sdk-sts.rb')

# aws-sdk-sso is included to support Aws::SSOCredentials
Aws.autoload :SSO, File.join(__dir__, 'aws-sdk-sso.rb')
Aws.autoload :SSOOIDC, File.join(__dir__, 'aws-sdk-ssooidc.rb')
6 changes: 5 additions & 1 deletion gems/aws-sdk-core/lib/aws-sdk-sso.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
#
# WARNING ABOUT GENERATED CODE


unless Module.const_defined?(:Aws)
require 'aws-sdk-core'
require 'aws-sigv4'
end

if Aws.autoload?(:SSO) &&
!(defined?(JRUBY_VERSION) && JRUBY_VERSION < '9.3')
Aws.autoload(:SSO, __FILE__)
end

require_relative 'aws-sdk-sso/types'
require_relative 'aws-sdk-sso/client_api'
require_relative 'aws-sdk-sso/plugins/endpoints.rb'
Expand Down
6 changes: 5 additions & 1 deletion gems/aws-sdk-core/lib/aws-sdk-ssooidc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
#
# WARNING ABOUT GENERATED CODE


unless Module.const_defined?(:Aws)
require 'aws-sdk-core'
require 'aws-sigv4'
end

if Aws.autoload?(:SSOOIDC) &&
!(defined?(JRUBY_VERSION) && JRUBY_VERSION < '9.3')
Aws.autoload(:SSOOIDC, __FILE__)
end

require_relative 'aws-sdk-ssooidc/types'
require_relative 'aws-sdk-ssooidc/client_api'
require_relative 'aws-sdk-ssooidc/plugins/endpoints.rb'
Expand Down
6 changes: 5 additions & 1 deletion gems/aws-sdk-core/lib/aws-sdk-sts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
#
# WARNING ABOUT GENERATED CODE


unless Module.const_defined?(:Aws)
require 'aws-sdk-core'
require 'aws-sigv4'
end

if Aws.autoload?(:STS) &&
!(defined?(JRUBY_VERSION) && JRUBY_VERSION < '9.3')
Aws.autoload(:STS, __FILE__)
end

require_relative 'aws-sdk-sts/types'
require_relative 'aws-sdk-sts/client_api'
require_relative 'aws-sdk-sts/plugins/endpoints.rb'
Expand Down
2 changes: 2 additions & 0 deletions gems/aws-sdk-resources/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Remove autoloads for service gems bundled in `aws-sdk-core` (STS, SSO, SSOOIDC).

3.201.0 (2024-08-12)
------------------

Expand Down
6 changes: 4 additions & 2 deletions gems/aws-sdk-resources/bin/aws-v3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ def env_bool key, default
# The Aws namespace used to check autoload requires aws-sdk-resources.
require 'aws-sdk-resources'

# Finally load all of the gems. Core is loaded a second time because of STS.
# Finally load all of the gems.
# Don't update the load path for gems bundled in core.
core_gems = [:STS, :SSO, :SSOOIDC]
if File.directory?(File.expand_path('../../../../build_tools', __FILE__))
gems = []
Aws.constants.each do |const_name|
if Aws.autoload?(const_name)
if Aws.autoload?(const_name) && !core_gems.include?(const_name)
gems << "aws-sdk-#{const_name.downcase}"
end
end
Expand Down
3 changes: 3 additions & 0 deletions tasks/build.rake
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ end
# It is need to provide session credentials and assume role support.
# Only building source, but not gemspecs, version file, etc.
task 'build:aws-sdk-sts' do
Aws::STS if Aws.autoload?(:STS) # force autoload from core
sts = BuildTools::Services.service('sts')
generator = AwsSdkCodeGenerator::CodeBuilder.new(
aws_sdk_core_lib_path: $CORE_LIB,
Expand All @@ -48,6 +49,7 @@ end
# It is need to provide SSO Credentials.
# Only building source, but not gemspecs, version file, etc.
task 'build:aws-sdk-sso' do
Aws::SSO if Aws.autoload?(:SSO) # force autoload from core
sso = BuildTools::Services.service('sso')
generator = AwsSdkCodeGenerator::CodeBuilder.new(
aws_sdk_core_lib_path: $CORE_LIB,
Expand All @@ -66,6 +68,7 @@ end
# Aws::SSOOIDC is generated directly into the `aws-sdk-core` gem.
# Only building source, but not gemspecs, version file, etc.
task 'build:aws-sdk-ssooidc' do
Aws::SSOOIDC if Aws.autoload?(:SSOOIDC) # force autoload from core
ssooidc = BuildTools::Services.service('ssooidc')
generator = AwsSdkCodeGenerator::CodeBuilder.new(
aws_sdk_core_lib_path: $CORE_LIB,
Expand Down
4 changes: 3 additions & 1 deletion tasks/update-aws-sdk-dependencies.rake
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ task 'update-aws-sdk-dependencies' do
)

# update the module autoloads
# Remove service gems bundled in core (eg, sts) that have gem_name of aws-sdk-core.
autoload_services = BuildTools::Services.select { |svc| svc.gem_name != 'aws-sdk-core' }
BuildTools.replace_lines(
filename: "#{$GEMS_DIR}/aws-sdk-resources/lib/aws-sdk-resources.rb",
start: /# service gems/,
stop: /# end service gems/,
new_lines: BuildTools::Services.map { |service|
new_lines: autoload_services.map { |service|
" autoload :#{service.name}, '#{service.gem_name}'\n"
}
)
Expand Down

0 comments on commit dd8f329

Please sign in to comment.