Skip to content

Commit

Permalink
Merge pull request #1102 from ruby/gc
Browse files Browse the repository at this point in the history
Reduce object allocations for GC
  • Loading branch information
soutaro committed Sep 2, 2022
2 parents f0d0eb9 + 5f565df commit 11ce417
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
10 changes: 6 additions & 4 deletions lib/rbs/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ def initialize(path:, absolute:)
end

def self.empty
new(path: [], absolute: false)
@empty ||= new(path: [], absolute: false)
end

def self.root
new(path: [], absolute: true)
@root ||= new(path: [], absolute: true)
end

def +(other)
Expand All @@ -30,8 +30,10 @@ def append(component)
end

def parent
raise "Parent with empty namespace" if empty?
self.class.new(path: path.take(path.size - 1), absolute: absolute?)
@parent ||= begin
raise "Parent with empty namespace" if empty?
self.class.new(path: path.take(path.size - 1), absolute: absolute?)
end
end

def absolute?
Expand Down
4 changes: 2 additions & 2 deletions lib/rbs/type_name_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def resolve(type_name, context:)

query = Query.new(type_name: type_name, context: context)
try_cache(query) do
path_head, *path_tail = type_name.to_namespace.path
path_head, *path_tail = type_name.split
raise unless path_head

name_head = TypeName.new(name: path_head, namespace: Namespace.empty)

absolute_head = context.find do |namespace|
# @type break: TypeName
full_name = name_head.with_prefix(namespace)
Expand Down
26 changes: 21 additions & 5 deletions lib/rbs/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -845,12 +845,12 @@ def free_variables(set = Set.new)
def map_type(&block)
if block
Function.new(
required_positionals: required_positionals.map {|param| param.map_type(&block) },
optional_positionals: optional_positionals.map {|param| param.map_type(&block) },
required_positionals: amap(required_positionals) {|param| param.map_type(&block) },
optional_positionals: amap(optional_positionals) {|param| param.map_type(&block) },
rest_positionals: rest_positionals&.yield_self {|param| param.map_type(&block) },
trailing_positionals: trailing_positionals.map {|param| param.map_type(&block) },
required_keywords: required_keywords.transform_values {|param| param.map_type(&block) },
optional_keywords: optional_keywords.transform_values {|param| param.map_type(&block) },
trailing_positionals: amap(trailing_positionals) {|param| param.map_type(&block) },
required_keywords: hmapv(required_keywords) {|param| param.map_type(&block) },
optional_keywords: hmapv(optional_keywords) {|param| param.map_type(&block) },
rest_keywords: rest_keywords&.yield_self {|param| param.map_type(&block) },
return_type: yield(return_type)
)
Expand All @@ -859,6 +859,22 @@ def map_type(&block)
end
end

def amap(array, &block)
if array.empty?
_ = array
else
array.map(&block)
end
end

def hmapv(hash, &block)
if hash.empty?
_ = hash
else
hash.transform_values(&block)
end
end

def map_type_name(&block)
map_type do |type|
type.map_type_name(&block)
Expand Down
6 changes: 6 additions & 0 deletions sig/namespace.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ module RBS

@absolute: bool

@parent: Namespace?

self.@root: Namespace?

self.@empty: Namespace?

# Returns new _empty_ namespace.
def self.empty: () -> Namespace

Expand Down
4 changes: 4 additions & 0 deletions sig/types.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ module RBS
def drop_tail: () -> [Param, Function]

def has_keyword?: () -> bool

def amap: [A, B] (Array[A]) { (A) -> B } -> Array[B]

def hmapv: [X, Y, Z] (Hash[X, Y]) { (Y) -> Z } -> Hash[X, Z]
end

class Block
Expand Down

0 comments on commit 11ce417

Please sign in to comment.