Skip to content
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

Compiler: don't always use Array for node dependencies and observers #12405

Merged
merged 24 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions spec/compiler/crystal/zero_one_or_many_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
require "spec"
require "../../../src/compiler/crystal/zero_one_or_many"

describe Crystal::ZeroOneOrMany do
describe "initialize and size" do
it "creates without a value" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary.size.should eq(0)
ary.value.should be_nil
end

it "creates with a value" do
ary = Crystal::ZeroOneOrMany.new(1)
ary.size.should eq(1)
ary.to_a.should eq([1])
ary.value.should be_a(Int32)
end
end

describe "as Indexable" do
it "when there's no value" do
ary = Crystal::ZeroOneOrMany(Int32).new
expect_raises(IndexError) { ary[0] }
expect_raises(IndexError) { ary[1] }
end

it "when there's a single value" do
ary = Crystal::ZeroOneOrMany.new(1)
ary[0].should eq(1)
expect_raises(IndexError) { ary[1] }
end

it "when there's two values" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary += [1, 2]
ary[0].should eq(1)
ary[1].should eq(2)
expect_raises(IndexError) { ary[2] }
end
end

describe "#each" do
it "when there's no value" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary.sum.should eq(0)
end

it "when there's a single value" do
ary = Crystal::ZeroOneOrMany.new(1)
ary.sum.should eq(1)
end

it "when there's two values" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary += [1, 2]
ary.sum.should eq(3)
end
end

describe "#+ element" do
it "when there's no value" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary += 1
ary.to_a.should eq([1])
end

it "when there's a single value" do
ary = Crystal::ZeroOneOrMany.new(1)
ary += 2
ary.to_a.should eq([1, 2])
end

it "when there's two values" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary += [1, 2]
ary += 3
ary.to_a.should eq([1, 2, 3])
end
end

describe "#+ elements" do
it "when there's no value and elements is empty" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary += [] of Int32
ary.empty?.should be_true
ary.value.should be_nil
end

it "when there's no value and elements has a single value" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary += [1]
ary.to_a.should eq([1])
ary.value.should be_a(Int32)
end

it "when there's no value and elements has more than one value" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary += [1, 2]
ary.to_a.should eq([1, 2])
ary.value.should be_a(Array(Int32))
end

it "when there's a single value" do
ary = Crystal::ZeroOneOrMany.new(1)
ary += [2, 3]
ary.to_a.should eq([1, 2, 3])
ary.value.should be_a(Array(Int32))
end

it "when there's two values" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary += [1, 2]
ary += [3, 4]
ary.to_a.should eq([1, 2, 3, 4])
ary.value.should be_a(Array(Int32))
end
end

describe "#reject" do
it "when there's no value" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary = ary.reject { true }
ary.empty?.should be_true
ary.value.should be_nil
end

it "when there's a single value and it matches" do
ary = Crystal::ZeroOneOrMany(Int32).new(1)
ary = ary.reject { |x| x == 1 }
ary.empty?.should be_true
ary.value.should be_nil
end

it "when there's a single value and it doesn't match" do
ary = Crystal::ZeroOneOrMany(Int32).new(1)
ary = ary.reject { |x| x == 2 }
ary.to_a.should eq([1])
ary.value.should be_a(Int32)
end

it "when there are three values and none matches" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary += [1, 2, 3]
ary = ary.reject { |x| x == 4 }
ary.to_a.should eq([1, 2, 3])
ary.value.should be_a(Array(Int32))
end

it "when there are three values and two match" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary += [1, 2, 3]
ary = ary.reject { |x| x < 3 }
ary.to_a.should eq([3])
ary.value.should be_a(Int32)
end

it "when there are three values and all match" do
ary = Crystal::ZeroOneOrMany(Int32).new
ary += [1, 2, 3]
ary = ary.reject { |x| x < 4 }
ary.empty?.should be_true
ary.value.should be_nil
end
end
end
4 changes: 2 additions & 2 deletions src/compiler/crystal/codegen/call.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Crystal::CodeGenVisitor
end

target_defs = node.target_defs
unless target_defs
if target_defs.empty?
node.raise "BUG: no target defs"
end

Expand Down Expand Up @@ -411,7 +411,7 @@ class Crystal::CodeGenVisitor
position_at_end current_def_label

# Prepare this specific call
call.target_defs = [a_def] of Def
call.target_defs = ZeroOneOrMany.new(a_def)
call.obj.try &.set_type(a_def.owner)
call.args.zip(a_def.args) do |call_arg, a_def_arg|
call_arg.set_type(a_def_arg.type)
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/interpreter/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ class Crystal::Repl::Compiler < Crystal::Visitor
end

target_defs = node.target_defs
unless target_defs
if target_defs.empty?
node.raise "BUG: no target defs"
end

Expand Down Expand Up @@ -2436,7 +2436,7 @@ class Crystal::Repl::Compiler < Crystal::Visitor
end

target_defs = call.target_defs
unless target_defs
if target_defs.empty?
call.raise "BUG: no target defs"
end

Expand Down
6 changes: 3 additions & 3 deletions src/compiler/crystal/interpreter/multidispatch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require "../semantic/main_visitor"
# end
# ```
module Crystal::Repl::Multidispatch
def self.create_def(context : Context, node : Call, target_defs : Array(Def))
def self.create_def(context : Context, node : Call, target_defs : ZeroOneOrMany(Def))
if node.block
a_def = create_def_uncached(context, node, target_defs)

Expand Down Expand Up @@ -70,7 +70,7 @@ module Crystal::Repl::Multidispatch
a_def
end

private def self.create_def_uncached(context : Context, node : Call, target_defs : Array(Def))
private def self.create_def_uncached(context : Context, node : Call, target_defs : ZeroOneOrMany(Def))
autocast_types = nil

# The generated multidispatch method should handle autocasted
Expand Down Expand Up @@ -200,7 +200,7 @@ module Crystal::Repl::Multidispatch
end

call = Call.new(call_obj, node.name, call_args)
call.target_defs = [target_def]
call.target_defs = ZeroOneOrMany(Def).new(target_def)
call.type = target_def.type

if block
Expand Down
80 changes: 47 additions & 33 deletions src/compiler/crystal/semantic/bindings.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Crystal
class ASTNode
property! dependencies : Array(ASTNode)
property observers : Array(ASTNode)?
property dependencies = ZeroOneOrMany(ASTNode).new
property observers = ZeroOneOrMany(ASTNode).new
property enclosing_call : Call?

@dirty = false
Expand Down Expand Up @@ -107,18 +107,28 @@ module Crystal
end

def bind_to(node : ASTNode)
bind(node) do |dependencies|
dependencies.push node
bind(node) do
@dependencies += node
node.add_observer self
node
end
end

def bind_to(nodes : Indexable)
def bind_to(node1 : ASTNode, node2 : ASTNode)
bind do
@dependencies += node1
@dependencies += node2
node1.add_observer self
node2.add_observer self
node1
end
end

def bind_to(nodes : Indexable(ASTNode))
return if nodes.empty?

bind do |dependencies|
dependencies.concat nodes
bind do
@dependencies += nodes
nodes.each &.add_observer self
nodes.first
end
Expand All @@ -132,15 +142,9 @@ module Crystal
raise_frozen_type freeze_type, from_type, from
end

dependencies = @dependencies ||= [] of ASTNode
node = yield

node = yield dependencies

if dependencies.size == 1
new_type = node.type?
else
new_type = Type.merge dependencies
end
new_type = type_from_dependencies
new_type = map_type(new_type) if new_type

if new_type && (freeze_type = self.freeze_type)
Expand All @@ -155,27 +159,36 @@ module Crystal
propagate
end

def unbind_from(nodes : Nil)
# Nothing to do
end

def unbind_from(node : ASTNode)
@dependencies.try &.reject! &.same?(node)
def unbind_from(node : ASTNode) : Nil
@dependencies = @dependencies.reject &.same?(node)
node.remove_observer self
end

def unbind_from(nodes : Array(ASTNode))
@dependencies.try &.reject! { |dep| nodes.any? &.same?(dep) }
def unbind_from(nodes : Enumerable(ASTNode)) : Nil
@dependencies = @dependencies.reject { |dependency|
nodes.any? &.same?(dependency)
}
nodes.each &.remove_observer self
end

def add_observer(observer)
observers = @observers ||= [] of ASTNode
observers.push observer
private def type_from_dependencies : Type?
dependencies = @dependencies.value
case dependencies
in Nil
nil
in ASTNode
dependencies.type?
in Array(ASTNode)
Type.merge dependencies
end
end

def add_observer(observer : ASTNode) : Nil
@observers += observer
end

def remove_observer(observer)
@observers.try &.reject! &.same?(observer)
def remove_observer(observer : ASTNode) : Nil
@observers = @observers.reject &.same?(observer)
end

def set_enclosing_call(enclosing_call)
Expand All @@ -197,16 +210,17 @@ module Crystal
end

def notify_observers
@observers.try &.each &.update self
@observers.each &.update self
@enclosing_call.try &.recalculate
@observers.try &.each &.propagate
@observers.each &.propagate
@enclosing_call.try &.propagate
end

def update(from = nil)
return if @type && @type.same? from.try &.type?

new_type = Type.merge dependencies
dependencies = @dependencies.value
new_type = type_from_dependencies
new_type = map_type(new_type) if new_type

if new_type && (freeze_type = self.freeze_type)
Expand Down Expand Up @@ -271,8 +285,8 @@ module Crystal
visited = Set(ASTNode).new.compare_by_identity
owner_trace << node if node.type?.try &.includes_type?(owner)
visited.add node
while deps = node.dependencies?
dependencies = deps.select { |dep| dep.type? && dep.type.includes_type?(owner) && !visited.includes?(dep) }
until node.dependencies.empty?
dependencies = node.dependencies.select { |dep| dep.type? && dep.type.includes_type?(owner) && !visited.includes?(dep) }
if dependencies.size > 0
node = dependencies.first
nil_reason = node.nil_reason if node.is_a?(MetaTypeVar)
Expand Down
Loading