-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
Reworked call site, inline cache mechanism.
- v5.0
- v4.20
- v4.19
- v4.18
- v4.17
- v4.16
- v4.15
- v4.14
- v4.13
- v4.12
- v4.11
- v4.10
- v4.9
- v4.8
- v4.7
- v4.6
- v4.5
- v4.4
- v4.3
- v4.2
- v4.1
- v4.0
- v3.107
- v3.106
- v3.105
- v3.104
- v3.103
- v3.102
- v3.101
- v3.100
- v3.99
- v3.98
- v3.97
- v3.96
- v3.95
- v3.94
- v3.93
- v3.92
- v3.91
- v3.90
- v3.89
- v3.88
- v3.87
- v3.86
- v3.85
- v3.84
- v3.83
- v3.82
- v3.81
- v3.80
- v3.79
- v3.78
- v3.77
- v3.76
- v3.75
- v3.74
- v3.73
- v3.72
- v3.71
- v3.70
- v3.69
- v3.68
- v3.67
- v3.66
- v3.65
- v3.64
- v3.63
- v3.62
- v3.61
- v3.60
- v3.59
- v3.58
- v3.57
- v3.56
- v3.55
- v3.54
- v3.53
- v3.52
- v3.51
- v3.50
- v3.49
- v3.48
- v3.47
- v3.46
- v3.45
- v3.44
- v3.43
- v3.42
- v3.41
- v3.40
- v3.39
- v3.38
- v3.37
- v3.36
- v3.35
- v3.34
- v3.33
- v3.32
- v3.31
- v3.30
- v3.29
- v3.28
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,35 @@ | ||
module Rubinius | ||
class CallSite | ||
attr_reader :name | ||
attr_reader :executable | ||
|
||
def hits | ||
0 | ||
end | ||
attr_reader :cache | ||
|
||
def ip | ||
Rubinius.primitive :call_site_ip | ||
raise PrimitiveFailure, "CallSite#ip primitive failed" | ||
end | ||
|
||
def location | ||
"#{@executable.file}:#{@executable.line_from_ip(ip)}" | ||
def depth | ||
Rubinius.primitive :call_site_depth | ||
raise PrimitiveFailure, "CallSite#depth primitive failed" | ||
end | ||
|
||
def invokes | ||
Rubinius.primitive :call_site_invokes | ||
raise PrimitiveFailure, "CallSite#invokes primitive failed" | ||
end | ||
|
||
def hits | ||
Rubinius.primitive :call_site_hits | ||
raise PrimitiveFailure, "CallSite#hits primitive failed" | ||
end | ||
|
||
def misses | ||
Rubinius.primitive :call_site_misses | ||
raise PrimitiveFailure, "CallSite#misses primitive failed" | ||
end | ||
|
||
def inspect | ||
"#<#{self.class.name}:0x#{self.object_id.to_s(16)} #{location}##{@name}(#{hits})>" | ||
"#<#{self.class.name}:0x#{self.object_id.to_s(16)} name=#{@name} ip=#{ip} depth=#{depth} invokes=#{invokes} hits=#{hits} misses=#{misses}>" | ||
end | ||
end | ||
end |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,262 +1,130 @@ | ||
#include "arguments.hpp" | ||
#include "call_frame.hpp" | ||
#include "global_cache.hpp" | ||
#include "lookup_data.hpp" | ||
#include "object_utils.hpp" | ||
#include "memory.hpp" | ||
|
||
#include "builtin/class.hpp" | ||
#include "builtin/call_site.hpp" | ||
#include "builtin/exception.hpp" | ||
#include "builtin/executable.hpp" | ||
#include "builtin/mono_inline_cache.hpp" | ||
#include "builtin/object.hpp" | ||
#include "builtin/poly_inline_cache.hpp" | ||
|
||
#include <stdlib.h> | ||
#include <sstream> | ||
|
||
namespace rubinius { | ||
int CallSite::max_caches = 0; | ||
CallSite::Executor CallSite::default_execute = CallSite::lookup_invoke_cache; | ||
|
||
void CallSite::bootstrap(STATE) { | ||
GO(call_site).set(state->memory()->new_class<Class, CallSite>( | ||
state, G(rubinius), "CallSite")); | ||
} | ||
|
||
CallSite* CallSite::empty(STATE, Symbol* name, Executable* executable, int ip) { | ||
CallSite* cache = state->memory()->new_object<CallSite>(state, G(call_site)); | ||
|
||
cache->name(name); | ||
cache->executable(state, executable); | ||
cache->ip(ip); | ||
|
||
state->vm()->metrics().machine.call_site_count++; | ||
|
||
return cache; | ||
} | ||
|
||
Integer* CallSite::ip_prim(STATE) { | ||
return Integer::from(state, ip()); | ||
} | ||
|
||
Object* CallSite::empty_cache(STATE, CallSite* call_site, Arguments& args) { | ||
Object* const self = state->vm()->call_frame()->self(); | ||
Object* const recv = args.recv(); | ||
Class* const recv_class = recv->direct_class(state); | ||
|
||
LookupData lookup(self, recv->lookup_begin(state), G(sym_public)); | ||
Dispatch dispatch(call_site->name()); | ||
|
||
if(!dispatch.resolve(state, call_site->name(), lookup)) { | ||
if(!lookup_method_missing(state, args, | ||
dispatch, self, recv->lookup_begin(state))) { | ||
return NULL; | ||
} | ||
} | ||
|
||
state->vm()->metrics().machine.methods_invoked++; | ||
|
||
call_site->update(state, recv_class, dispatch); | ||
|
||
Executable* meth = dispatch.method; | ||
Module* mod = dispatch.module; | ||
|
||
if(meth->custom_call_site_p()) { | ||
CallSiteInformation info(call_site->executable(), call_site->ip()); | ||
state->set_call_site_information(&info); | ||
Object* res = meth->execute(state, meth, mod, args); | ||
state->set_call_site_information(NULL); | ||
return res; | ||
if(state->shared().config.machine_call_site_cache.value) { | ||
default_execute = lookup_invoke_cache; | ||
} else { | ||
return meth->execute(state, meth, mod, args); | ||
} | ||
} | ||
|
||
Object* CallSite::empty_cache_private(STATE, CallSite* call_site, | ||
Arguments& args) | ||
{ | ||
Object* const self = state->vm()->call_frame()->self(); | ||
Object* const recv = args.recv(); | ||
Class* const recv_class = recv->direct_class(state); | ||
|
||
LookupData lookup(self, recv->lookup_begin(state), G(sym_private)); | ||
Dispatch dispatch(call_site->name()); | ||
|
||
if(!dispatch.resolve(state, dispatch.name, lookup)) { | ||
if(!lookup_method_missing(state, args, | ||
dispatch, self, recv->lookup_begin(state))) { | ||
return NULL; | ||
} | ||
default_execute = dispatch; | ||
} | ||
|
||
state->vm()->metrics().machine.methods_invoked++; | ||
|
||
call_site->update(state, recv_class, dispatch); | ||
|
||
Executable* meth = dispatch.method; | ||
Module* mod = dispatch.module; | ||
|
||
if(meth->custom_call_site_p()) { | ||
CallSiteInformation info(call_site->executable(), call_site->ip()); | ||
state->set_call_site_information(&info); | ||
Object* res = meth->execute(state, meth, mod, args); | ||
state->set_call_site_information(NULL); | ||
return res; | ||
} else { | ||
return meth->execute(state, meth, mod, args); | ||
} | ||
max_caches = state->shared().config.machine_call_site_limit.value; | ||
} | ||
|
||
Object* CallSite::empty_cache_vcall(STATE, CallSite* call_site, Arguments& args) { | ||
Object* const self = state->vm()->call_frame()->self(); | ||
Object* const recv = args.recv(); | ||
Class* const recv_class = recv->direct_class(state); | ||
|
||
LookupData lookup(self, recv->lookup_begin(state), G(sym_private)); | ||
Dispatch dispatch(call_site->name()); | ||
|
||
if(!dispatch.resolve(state, call_site->name(), lookup)) { | ||
dispatch.method_missing = eVCall; | ||
if(!lookup_method_missing(state, args, | ||
dispatch, self, recv->lookup_begin(state))) { | ||
return NULL; | ||
} | ||
} | ||
void CallSite::Info::mark(Object* obj, memory::ObjectMark& mark) { | ||
auto_mark(obj, mark); | ||
|
||
state->vm()->metrics().machine.methods_invoked++; | ||
CallSite* call_site = as<CallSite>(obj); | ||
|
||
call_site->update(state, recv_class, dispatch); | ||
if(!call_site->caches()) return; | ||
|
||
Executable* meth = dispatch.method; | ||
Module* mod = dispatch.module; | ||
// 1. Check if individual caches should be evicted. | ||
bool evict_p[call_site->depth()]; | ||
|
||
if(meth->custom_call_site_p()) { | ||
CallSiteInformation info(call_site->executable(), call_site->ip()); | ||
state->set_call_site_information(&info); | ||
Object* res = meth->execute(state, meth, mod, args); | ||
state->set_call_site_information(NULL); | ||
return res; | ||
} else { | ||
return meth->execute(state, meth, mod, args); | ||
for(int i = 0; i < call_site->depth(); i++) { | ||
evict_p[i] = call_site->caches()->cache[i].inefficient_p(); | ||
} | ||
} | ||
|
||
Object* CallSite::empty_cache_super(STATE, CallSite* call_site, | ||
Arguments& args) | ||
{ | ||
CallFrame* call_frame = state->vm()->call_frame(); | ||
|
||
Symbol* original_name = call_frame->original_name(); | ||
if(call_site->name() != original_name) { | ||
call_site->name(original_name); | ||
args.set_name(call_site->name()); | ||
int evict_count = 0; | ||
for(int i = 0; i < call_site->depth(); i++) { | ||
if(evict_p[i]) evict_count++; | ||
} | ||
|
||
Object* const recv = args.recv(); | ||
Class* const recv_class = recv->direct_class(state); | ||
Module* const start = call_frame->module()->superclass(); | ||
if(evict_count) { | ||
VM::current()->metrics().machine.inline_cache_evicted += evict_count; | ||
|
||
LookupData lookup(call_frame->self(), start, G(sym_private)); | ||
Dispatch dispatch(call_site->name()); | ||
int new_size = call_site->depth() - evict_count; | ||
|
||
if(start->nil_p() || !dispatch.resolve(state, call_site->name(), lookup)) { | ||
if(new_size == 0) { | ||
call_site->depth(0); | ||
delete[] call_site->caches(); | ||
call_site->caches(NULL); | ||
|
||
LookupData missing_lookup(call_frame->self(), | ||
recv->lookup_begin(state), G(sym_private)); | ||
Dispatch missing_dispatch(G(sym_method_missing)); | ||
call_site->execute(CallSite::default_execute); | ||
call_site->cache_miss(CallSite::default_execute); | ||
|
||
if(!missing_dispatch.resolve(state, G(sym_method_missing), missing_lookup)) { | ||
std::ostringstream msg; | ||
msg << "no method_missing for "; | ||
msg << recv_class->to_string(state); | ||
msg << "#" << call_site->name()->to_string(state); | ||
return; | ||
} | ||
|
||
Exception::internal_error(state, msg.str().c_str()); | ||
return 0; | ||
for(int i = 0, j = 0; i < call_site->depth() && j < new_size; i++) { | ||
if(!evict_p[i]) { | ||
call_site->caches()->cache[j++] = call_site->caches()->cache[i]; | ||
} | ||
} | ||
|
||
args.unshift(state, call_site->name()); | ||
dispatch.method = missing_dispatch.method; | ||
dispatch.module = missing_dispatch.module; | ||
dispatch.method_missing = eSuper; | ||
state->vm()->set_method_missing_reason(dispatch.method_missing); | ||
state->vm()->global_cache()->add_seen(state, call_site->name()); | ||
call_site->caches()->depth(new_size); | ||
} | ||
|
||
state->vm()->metrics().machine.methods_invoked++; | ||
|
||
call_site->update(state, recv_class, dispatch); | ||
// 2. Attempt to re-order the caches by bubbling most hit forward. | ||
bool reorder_p = false; | ||
int indexes[call_site->depth()]; | ||
|
||
Executable* meth = dispatch.method; | ||
Module* mod = dispatch.module; | ||
|
||
if(meth->custom_call_site_p()) { | ||
CallSiteInformation info(call_site->executable(), call_site->ip()); | ||
state->set_call_site_information(&info); | ||
Object* res = meth->execute(state, meth, mod, args); | ||
state->set_call_site_information(NULL); | ||
return res; | ||
} else { | ||
return meth->execute(state, meth, mod, args); | ||
for(int i = 0; i < call_site->depth(); i++) { | ||
indexes[i] = i; | ||
} | ||
} | ||
|
||
void CallSite::empty_cache_updater(STATE, CallSite* call_site, Class* klass, Dispatch& dispatch) { | ||
MonoInlineCache* cache = MonoInlineCache::create(state, call_site, klass, dispatch); | ||
call_site->update_call_site(state, cache); | ||
} | ||
InlineCaches* caches = call_site->caches(); | ||
|
||
bool CallSite::update_and_validate(STATE, Object* recv, Symbol* vis, int serial) { | ||
Class* const recv_class = recv->direct_class(state); | ||
for(int i = 0; i < call_site->depth() - 1; i++) { | ||
if(caches->cache[i].hits() < caches->cache[i + 1].hits()) { | ||
int tmp = indexes[i]; | ||
indexes[i] = indexes[i + 1]; | ||
indexes[i + 1] = tmp; | ||
reorder_p = true; | ||
|
||
if(MonoInlineCache* mono = try_as<MonoInlineCache>(this)) { | ||
if(recv_class->data_raw() == mono->receiver_data_raw()) { | ||
return mono->method()->serial()->to_native() == serial; | ||
// TODO: pass State through the GC! | ||
VM::current()->metrics().machine.inline_cache_reordered++; | ||
} | ||
} | ||
|
||
if(PolyInlineCache* cache = try_as<PolyInlineCache>(this)) { | ||
InlineCacheEntry* ice = cache->get_entry(recv_class); | ||
if(likely(ice)) return ice->method()->serial()->to_native() == serial; | ||
} | ||
if(reorder_p) { | ||
InlineCache* inline_caches = static_cast<InlineCache*>( | ||
alloca(sizeof(CallSite) * call_site->depth())); | ||
|
||
LookupData lookup(state->vm()->call_frame()->self(), | ||
recv->lookup_begin(state), G(sym_public)); | ||
Dispatch dispatch(name()); | ||
for(int i = 0; i < call_site->depth(); i++) { | ||
inline_caches[i] = caches->cache[i]; | ||
} | ||
|
||
if(dispatch.resolve(state, name(), lookup)) { | ||
update(state, recv_class, dispatch); | ||
return dispatch.method->serial()->to_native() == serial; | ||
for(int i = 0; i < call_site->depth(); i++) { | ||
caches->cache[i] = inline_caches[indexes[i]]; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
bool CallSite::lookup_method_missing(STATE, | ||
Arguments& args, Dispatch& dispatch, Object* self, Module* begin) | ||
{ | ||
LookupData missing_lookup(self, begin, G(sym_private)); | ||
Dispatch missing_dispatch(G(sym_method_missing)); | ||
|
||
if(!missing_dispatch.resolve(state, G(sym_method_missing), missing_lookup)) { | ||
std::ostringstream msg; | ||
msg << "no method_missing for "; | ||
msg << begin->to_string(state); | ||
msg << "#" << dispatch.name->to_string(state); | ||
// 3. Mark remaining caches. | ||
for(int i = 0; i < call_site->depth(); i++) { | ||
InlineCache* cache = &caches->cache[i]; | ||
|
||
Exception::internal_error(state, msg.str().c_str()); | ||
return false; | ||
} | ||
|
||
args.unshift(state, dispatch.name); | ||
dispatch.method = missing_dispatch.method; | ||
dispatch.module = missing_dispatch.module; | ||
state->vm()->set_method_missing_reason(dispatch.method_missing); | ||
state->vm()->global_cache()->add_seen(state, dispatch.name); | ||
if(Object* ref = mark.call(cache->receiver_class())) { | ||
cache->receiver_class(as<Class>(ref)); | ||
mark.just_set(call_site, ref); | ||
} | ||
|
||
return true; | ||
} | ||
if(Object* ref = mark.call(cache->stored_module())) { | ||
cache->stored_module(as<Module>(ref)); | ||
mark.just_set(call_site, ref); | ||
} | ||
|
||
void CallSite::Info::mark(Object* obj, memory::ObjectMark& mark) { | ||
auto_mark(obj, mark); | ||
if(Object* ref = mark.call(cache->executable())) { | ||
cache->executable(as<Executable>(ref)); | ||
mark.just_set(call_site, ref); | ||
} | ||
} | ||
} | ||
|
||
} |
Large diffs are not rendered by default.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require File.expand_path('../../fixtures/call_site.rb', __FILE__) | ||
|
||
describe "Rubinius::CallSite#invokes" do | ||
before :each do | ||
@executable = CallSiteSpecs::A.executable(:miss) | ||
end | ||
|
||
it "returns 0 before the call site has executed" do | ||
@executable.call_sites.first.invokes.should == 0 | ||
end | ||
|
||
it "returns the number of invocations that do not hit a cache entry" do | ||
obj = CallSiteSpecs::A.new | ||
obj.miss(1).should == "1" | ||
obj.miss("2").should == "2" | ||
|
||
@executable.call_sites.first.invokes.should == 2 | ||
end | ||
end | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
require File.expand_path('../../fixtures/call_site.rb', __FILE__) | ||
|
||
describe "Rubinius::CallSite#hits" do | ||
before :each do | ||
@executable = CallSiteSpecs::A.executable(:c) | ||
@klass = Class.new do | ||
def m(a) | ||
a.to_s | ||
end | ||
end | ||
end | ||
|
||
it "returns 0 before the call site has executed" do | ||
@executable.call_sites.first.hits.should == 0 | ||
@klass.new.method(:m).executable.call_sites.first.hits.should == 0 | ||
end | ||
|
||
it "returns the number of times the call site has executed" do | ||
obj = CallSiteSpecs::A.new | ||
obj.c | ||
obj.c | ||
it "returns the call site invocations that have hit the cache entries" do | ||
obj = @klass.new | ||
|
||
obj.m :a | ||
obj.m :b | ||
obj.m :c | ||
|
||
# needs to retrieve the new call site because it has been replaced | ||
@executable.call_sites.first.hits.should == 2 | ||
obj.method(:m).executable.call_sites.first.hits.should == 2 | ||
end | ||
end | ||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require File.expand_path('../../fixtures/call_site.rb', __FILE__) | ||
|
||
describe "Rubinius::CallSite#invokes" do | ||
it "returns 0 before the call site has executed" do | ||
executable = CallSiteSpecs::A.executable(:c) | ||
executable.call_sites.first.invokes.should == 0 | ||
end | ||
|
||
it "returns the call site invocations that do not hit the cache" do | ||
obj = Class.new do | ||
def m(a) | ||
a.to_s | ||
end | ||
end.new | ||
|
||
obj.m 1 | ||
obj.m :a | ||
obj.m :b | ||
obj.m "2" | ||
|
||
obj.method(:m).executable.call_sites.first.invokes.should == 3 | ||
end | ||
end |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
describe "Rubinius::CallSite#misses" do | ||
before :each do | ||
@klass = Class.new do | ||
def m(a) | ||
a.to_s | ||
end | ||
end | ||
end | ||
|
||
it "returns 0 before the call site has executed" do | ||
@klass.new.method(:m).executable.call_sites.first.misses.should == 0 | ||
end | ||
|
||
it "returns the call site invocations that have missed the cache entries" do | ||
obj = @klass.new | ||
|
||
obj.m 1 | ||
obj.m :a | ||
obj.m "a" | ||
obj.m Object.new | ||
obj.m [] | ||
|
||
obj.method(:m).executable.call_sites.first.misses.should == 4 | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,10 @@ def p | |
a B.new | ||
a BB.new | ||
end | ||
|
||
def miss(a) | ||
a.to_s | ||
end | ||
end | ||
|
||
class B | ||
|
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.