Skip to content

Commit e21b8a8

Browse files
committed
Reduce object creation during substitution
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.
1 parent a7178b1 commit e21b8a8

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

lib/rbs/ast/members.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def update(annotations: self.annotations, method_type: self.method_type)
3030
end
3131

3232
def sub(subst)
33+
return self if subst.empty?
34+
3335
update(method_type: self.method_type.sub(subst))
3436
end
3537

lib/rbs/definition.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def initialize(parent_variable:, type:, declared_in:)
1414
end
1515

1616
def sub(s)
17+
return self if s.empty?
18+
1719
self.class.new(
1820
parent_variable: parent_variable,
1921
type: type.sub(s),
@@ -142,6 +144,8 @@ def private?
142144
end
143145

144146
def sub(s)
147+
return self if s.empty?
148+
145149
self.class.new(
146150
super_method: super_method&.sub(s),
147151
defs: defs.map {|defn| defn.update(type: defn.type.sub(s)) },
@@ -347,6 +351,8 @@ def type_params_decl
347351
end
348352

349353
def sub(s)
354+
return self if s.empty?
355+
350356
definition = self.class.new(type_name: type_name, self_type: _ = self_type.sub(s), ancestors: ancestors, entry: entry)
351357

352358
definition.methods.merge!(methods.transform_values {|method| method.sub(s) })

lib/rbs/method_type.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def to_json(state = _ = nil)
3333
def sub(s)
3434
sub = s.without(*type_param_names)
3535

36+
return self if sub.empty?
37+
3638
self.class.new(
3739
type_params: type_params.map do |param|
3840
param.map_type do |bound|

lib/rbs/types.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ def to_json(state = _ = nil)
329329
end
330330

331331
def sub(s)
332+
return self if s.empty?
333+
332334
self.class.new(name: name,
333335
args: args.map {|ty| ty.sub(s) },
334336
location: location)
@@ -371,6 +373,8 @@ def to_json(state = _ = nil)
371373
end
372374

373375
def sub(s)
376+
return self if s.empty?
377+
374378
self.class.new(name: name,
375379
args: args.map {|ty| ty.sub(s) },
376380
location: location)
@@ -413,6 +417,8 @@ def to_json(state = _ = nil)
413417
end
414418

415419
def sub(s)
420+
return self if s.empty?
421+
416422
Alias.new(name: name, args: args.map {|ty| ty.sub(s) }, location: location)
417423
end
418424

@@ -469,6 +475,8 @@ def to_json(state = _ = nil)
469475
end
470476

471477
def sub(s)
478+
return self if s.empty?
479+
472480
self.class.new(types: types.map {|ty| ty.sub(s) },
473481
location: location)
474482
end
@@ -574,6 +582,8 @@ def to_json(state = _ = nil)
574582
end
575583

576584
def sub(s)
585+
return self if s.empty?
586+
577587
self.class.new(
578588
all_fields: all_fields.transform_values {|ty, required| [ty.sub(s), required] },
579589
location: location
@@ -664,6 +674,8 @@ def to_json(state = _ = nil)
664674
end
665675

666676
def sub(s)
677+
return self if s.empty?
678+
667679
self.class.new(type: type.sub(s), location: location)
668680
end
669681

@@ -752,6 +764,8 @@ def to_json(state = _ = nil)
752764
end
753765

754766
def sub(s)
767+
return self if s.empty?
768+
755769
self.class.new(types: types.map {|ty| ty.sub(s) },
756770
location: location)
757771
end
@@ -841,6 +855,8 @@ def to_json(state = _ = nil)
841855
end
842856

843857
def sub(s)
858+
return self if s.empty?
859+
844860
self.class.new(types: types.map {|ty| ty.sub(s) },
845861
location: location)
846862
end
@@ -1093,6 +1109,8 @@ def to_json(state = _ = nil)
10931109
end
10941110

10951111
def sub(s)
1112+
return self if s.empty?
1113+
10961114
map_type {|ty| ty.sub(s) }
10971115
end
10981116

@@ -1272,6 +1290,8 @@ def to_json(state = _ = nil)
12721290
end
12731291

12741292
def sub(subst)
1293+
return self if subst.empty?
1294+
12751295
map_type { _1.sub(subst) }
12761296
end
12771297

@@ -1346,6 +1366,8 @@ def to_json(state = _ = nil)
13461366
end
13471367

13481368
def sub(s)
1369+
return self if s.empty?
1370+
13491371
self.class.new(
13501372
type: type.sub(s),
13511373
required: required,
@@ -1415,6 +1437,8 @@ def to_json(state = _ = nil)
14151437
end
14161438

14171439
def sub(s)
1440+
return self if s.empty?
1441+
14181442
self.class.new(
14191443
type: type.sub(s),
14201444
block: block&.sub(s),

0 commit comments

Comments
 (0)