Skip to content

Commit

Permalink
Reduce object creation during substitution
Browse files Browse the repository at this point in the history
The `#sub` method generates many objects, but in most classes, substitution does not occur at all.
By modifying it so that no objects are created when there are no variables to substitute, we will optimize performance.
  • Loading branch information
ksss committed Feb 12, 2025
1 parent a7178b1 commit e21b8a8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/rbs/ast/members.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def update(annotations: self.annotations, method_type: self.method_type)
end

def sub(subst)
return self if subst.empty?

update(method_type: self.method_type.sub(subst))
end

Expand Down
6 changes: 6 additions & 0 deletions lib/rbs/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def initialize(parent_variable:, type:, declared_in:)
end

def sub(s)
return self if s.empty?

self.class.new(
parent_variable: parent_variable,
type: type.sub(s),
Expand Down Expand Up @@ -142,6 +144,8 @@ def private?
end

def sub(s)
return self if s.empty?

self.class.new(
super_method: super_method&.sub(s),
defs: defs.map {|defn| defn.update(type: defn.type.sub(s)) },
Expand Down Expand Up @@ -347,6 +351,8 @@ def type_params_decl
end

def sub(s)
return self if s.empty?

definition = self.class.new(type_name: type_name, self_type: _ = self_type.sub(s), ancestors: ancestors, entry: entry)

definition.methods.merge!(methods.transform_values {|method| method.sub(s) })
Expand Down
2 changes: 2 additions & 0 deletions lib/rbs/method_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def to_json(state = _ = nil)
def sub(s)
sub = s.without(*type_param_names)

return self if sub.empty?

self.class.new(
type_params: type_params.map do |param|
param.map_type do |bound|
Expand Down
24 changes: 24 additions & 0 deletions lib/rbs/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ def to_json(state = _ = nil)
end

def sub(s)
return self if s.empty?

self.class.new(name: name,
args: args.map {|ty| ty.sub(s) },
location: location)
Expand Down Expand Up @@ -371,6 +373,8 @@ def to_json(state = _ = nil)
end

def sub(s)
return self if s.empty?

self.class.new(name: name,
args: args.map {|ty| ty.sub(s) },
location: location)
Expand Down Expand Up @@ -413,6 +417,8 @@ def to_json(state = _ = nil)
end

def sub(s)
return self if s.empty?

Alias.new(name: name, args: args.map {|ty| ty.sub(s) }, location: location)
end

Expand Down Expand Up @@ -469,6 +475,8 @@ def to_json(state = _ = nil)
end

def sub(s)
return self if s.empty?

self.class.new(types: types.map {|ty| ty.sub(s) },
location: location)
end
Expand Down Expand Up @@ -574,6 +582,8 @@ def to_json(state = _ = nil)
end

def sub(s)
return self if s.empty?

self.class.new(
all_fields: all_fields.transform_values {|ty, required| [ty.sub(s), required] },
location: location
Expand Down Expand Up @@ -664,6 +674,8 @@ def to_json(state = _ = nil)
end

def sub(s)
return self if s.empty?

self.class.new(type: type.sub(s), location: location)
end

Expand Down Expand Up @@ -752,6 +764,8 @@ def to_json(state = _ = nil)
end

def sub(s)
return self if s.empty?

self.class.new(types: types.map {|ty| ty.sub(s) },
location: location)
end
Expand Down Expand Up @@ -841,6 +855,8 @@ def to_json(state = _ = nil)
end

def sub(s)
return self if s.empty?

self.class.new(types: types.map {|ty| ty.sub(s) },
location: location)
end
Expand Down Expand Up @@ -1093,6 +1109,8 @@ def to_json(state = _ = nil)
end

def sub(s)
return self if s.empty?

map_type {|ty| ty.sub(s) }
end

Expand Down Expand Up @@ -1272,6 +1290,8 @@ def to_json(state = _ = nil)
end

def sub(subst)
return self if subst.empty?

map_type { _1.sub(subst) }
end

Expand Down Expand Up @@ -1346,6 +1366,8 @@ def to_json(state = _ = nil)
end

def sub(s)
return self if s.empty?

self.class.new(
type: type.sub(s),
required: required,
Expand Down Expand Up @@ -1415,6 +1437,8 @@ def to_json(state = _ = nil)
end

def sub(s)
return self if s.empty?

self.class.new(
type: type.sub(s),
block: block&.sub(s),
Expand Down

0 comments on commit e21b8a8

Please sign in to comment.