-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: RBS size reduction #3382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sichanyoo
wants to merge
4
commits into
version-3
Choose a base branch
from
feat/rbs-size-reduction
base: version-3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: RBS size reduction #3382
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
...tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/rbs/input_type_alias_collector.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module AwsSdkCodeGenerator | ||
| module RBS | ||
| # Collects structure shapes referenced more than once in input shape. | ||
| class InputTypeAliasCollector | ||
| def initialize(api:) | ||
| @api = api | ||
| @shape_usage_count = Hash.new(0) | ||
| @size_cache = {} | ||
| end | ||
| # Returns a topologically sorted array of shape names to render as type aliases. | ||
| # Leaf dependencies come first so aliases can reference each other. | ||
| # params.rbs uses aliases in this list to define RBS type aliases. | ||
| # client_class.rbs uses aliases in this list to deduplicate content. | ||
| def shapes_to_alias | ||
| count_shape_usage | ||
| aliased = @shape_usage_count.select do |shape_name, count| | ||
| shape = @api['shapes'][shape_name] | ||
| shape['type'] == 'structure' && | ||
| count > 1 && | ||
| rendered_rbs_line_count_heuristic(shape_name) > 5 | ||
| end.keys.to_set | ||
| topological_sort(aliased) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def topological_sort(shape_names) | ||
| sorted = [] | ||
| visited = Set.new | ||
| shape_names.each { |name| topo_visit(name, shape_names, visited, sorted) } | ||
| sorted | ||
| end | ||
|
|
||
| def topo_visit(name, shape_names, visited, sorted) | ||
| return if visited.include?(name) | ||
|
|
||
| visited << name | ||
| shape = @api['shapes'][name] | ||
| shape['members']&.each_value do |ref| | ||
| dep = ref['shape'] | ||
| topo_visit(dep, shape_names, visited, sorted) if shape_names.include?(dep) | ||
| end | ||
| sorted << name | ||
| end | ||
|
|
||
| def count_shape_usage | ||
| @api['operations'].each_value do |op| | ||
| input_shape_name = op.dig('input', 'shape') | ||
| next unless input_shape_name | ||
|
|
||
| walk_shape(input_shape_name, Set.new) | ||
| end | ||
| end | ||
|
|
||
| def walk_shape(shape_name, ancestors) | ||
| return if ancestors.include?(shape_name) | ||
|
|
||
| ancestors += [shape_name] | ||
|
|
||
| shape = @api['shapes'][shape_name] | ||
| return unless shape && shape['type'] == 'structure' | ||
|
|
||
| shape['members']&.each_value do |member_ref| | ||
| member_shape_name = member_ref['shape'] | ||
| member_shape = @api['shapes'][member_shape_name] | ||
| next unless member_shape | ||
|
|
||
| case member_shape['type'] | ||
| when 'structure' | ||
| @shape_usage_count[member_shape_name] += 1 | ||
| walk_shape(member_shape_name, ancestors) | ||
| when 'list' | ||
| walk_list(member_shape, ancestors) | ||
| when 'map' | ||
| walk_map(member_shape, ancestors) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def walk_list(list_shape, ancestors) | ||
| member_ref = list_shape['member'] | ||
| member_shape = @api['shapes'][member_ref['shape']] | ||
| return unless member_shape | ||
|
|
||
| case member_shape['type'] | ||
| when 'structure' | ||
| @shape_usage_count[member_ref['shape']] += 1 | ||
| walk_shape(member_ref['shape'], ancestors) | ||
| when 'list' | ||
| walk_list(member_shape, ancestors) | ||
| when 'map' | ||
| walk_map(member_shape, ancestors) | ||
| end | ||
| end | ||
|
|
||
| def walk_map(map_shape, ancestors) | ||
| value_ref = map_shape['value'] | ||
| value_shape = @api['shapes'][value_ref['shape']] | ||
| return unless value_shape | ||
|
|
||
| case value_shape['type'] | ||
| when 'structure' | ||
| @shape_usage_count[value_ref['shape']] += 1 | ||
| walk_shape(value_ref['shape'], ancestors) | ||
| when 'list' | ||
| walk_list(value_shape, ancestors) | ||
| when 'map' | ||
| walk_map(value_shape, ancestors) | ||
| end | ||
| end | ||
|
|
||
| def rendered_rbs_line_count_heuristic(shape_name, visited = Set.new) | ||
| return 0 if visited.include?(shape_name) | ||
|
|
||
| # Cache results to deduplicate calculation for nested structure types that get used multiple times in the model. | ||
| return @size_cache[shape_name] if @size_cache.key?(shape_name) | ||
|
|
||
| visited += [shape_name] | ||
| shape = @api['shapes'][shape_name] | ||
| return @size_cache[shape_name] = 1 unless shape['members'] | ||
|
|
||
| @size_cache[shape_name] = shape['members'].sum do |_, ref| | ||
| member_line_count(ref, visited) | ||
| end | ||
| end | ||
|
|
||
| def member_line_count(ref, visited) | ||
| child = @api['shapes'][ref['shape']] | ||
| case child['type'] | ||
| # If it's a structure, add 2 lines then recurse (one line each for '{' and '}' in rendered RBS) | ||
| when 'structure' then 2 + rendered_rbs_line_count_heuristic(ref['shape'], visited) | ||
| when 'list' | ||
| member_shape = @api['shapes'][child['member']['shape']] | ||
| # If it's a list with structure member, add 2 lines & recurse. | ||
| # (one line each for 'Hash[::String, {' and '}]' in rendered RBS) | ||
| # If it's a list with primitive member, add 1 and return | ||
| # (e.g., renders as 1-liner, like 'list: Array[::String]') | ||
| member_shape['type'] == 'structure' ? 2 + | ||
| rendered_rbs_line_count_heuristic(child['member']['shape'], visited) : 1 | ||
| when 'map' | ||
| value_shape = @api['shapes'][child['value']['shape']] | ||
| # If it's a map with structure member, add 2 lines & recurse. | ||
| # (one line each for 'Hash[::String, {' and '}]' in rendered RBS) | ||
| # If it's a map with primitive member, add 1 and return | ||
| # (e.g., renders as 1-liner, like 'map: Hash[::String, ::String]') | ||
| value_shape['type'] == 'structure' ? 2 + | ||
| rendered_rbs_line_count_heuristic(child['value']['shape'], visited) : 1 | ||
| else 1 | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/views/rbs/params.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module AwsSdkCodeGenerator | ||
| module Views | ||
| module RBS | ||
| class Params < View | ||
| include Helper | ||
|
|
||
| def initialize(options) | ||
| @service_name = options.fetch(:service_name) | ||
| @api = options.fetch(:api) | ||
| @aliased_shapes = options.fetch(:aliased_shapes) | ||
| end | ||
|
|
||
| def generated_src_warning | ||
| GENERATED_SRC_WARNING | ||
| end | ||
|
|
||
| def service_name | ||
| @service_name | ||
| end | ||
|
|
||
| def type_aliases | ||
| aliased_set = @aliased_shapes.to_set | ||
| @aliased_shapes.map do |shape_name| | ||
| shape = @api['shapes'][shape_name] | ||
| builder = AwsSdkCodeGenerator::RBS::KeywordArgumentBuilder.new( | ||
| api: @api, | ||
| shape: shape, | ||
| newline: true, | ||
| options: { aliased_shapes: aliased_set - [shape_name] } | ||
| ) | ||
| { | ||
| 'name' => underscore(shape_name), | ||
| 'definition' => builder.format_as_alias(indent: ' '), | ||
| } | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes are done for regular Client but what about AsyncClient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh oops, good catch. I didn't know Ruby published separate client type for bidirectional streaming lol. Added to async clients as well as pointed out.