Skip to content

Commit

Permalink
Merge pull request #230 from soutaro/cache-interfaces
Browse files Browse the repository at this point in the history
Cache interfaces
  • Loading branch information
soutaro committed Oct 3, 2020
2 parents d5d8560 + 557938a commit c88deaa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 1 addition & 2 deletions bin/steep-prof
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ def exit(*)

end


STDERR.puts "Running profiler: mode=#{mode}, out=#{out}"
StackProf.run(mode: mode, out: out) do
StackProf.run(mode: mode, out: out, raw: true) do
load File.join(__dir__, "../exe/steep")
end
15 changes: 14 additions & 1 deletion lib/steep/ast/types/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ class Factory
attr_reader :type_name_cache
attr_reader :type_cache

attr_reader :type_interface_cache

def initialize(builder:)
@definition_builder = builder

@type_name_cache = {}
@type_cache = {}
@type_interface_cache = {}
end

def type_name_resolver
Expand Down Expand Up @@ -390,15 +393,23 @@ def setup_primitives(method_name, method_type)

def interface(type, private:, self_type: type)
Steep.logger.debug { "Factory#interface: #{type}, private=#{private}, self_type=#{self_type}" }
type = expand_alias(type)

cache_key = [type, self_type, private]
if type_interface_cache.key?(cache_key)
return type_interface_cache[cache_key]
end

case type
when Name::Alias
interface(expand_alias(type), private: private, self_type: self_type)

when Self
if self_type != type
interface self_type, private: private, self_type: Self.new
else
raise "Unexpected `self` type interface"
end

when Name::Instance
Interface::Interface.new(type: self_type, private: private).tap do |interface|
definition = definition_builder.build_instance(type.name)
Expand Down Expand Up @@ -689,6 +700,8 @@ def interface(type, private:, self_type: type)

else
raise "Unexpected type for interface: #{type}"
end.tap do |interface|
type_interface_cache[cache_key] = interface
end
end

Expand Down

0 comments on commit c88deaa

Please sign in to comment.