Skip to content

Commit

Permalink
Rewrite LOAD_GLOBAL _Unbound to LOAD_IMMEDIATE in builtins module
Browse files Browse the repository at this point in the history
This should save some caches and be faster. Ideally we would do this in
all Skybison-controlled modules but I don't think we have a good signal
for that yet.

Partially addresses #395; we still cannot use it as marker in
dictionaries because it's still in the globals dict.
  • Loading branch information
tekknolagi committed Mar 22, 2023
1 parent beb1437 commit 58ff7ca
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions runtime/bytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ struct RewrittenOp {
};

static RewrittenOp rewriteOperation(const Function& function, BytecodeOp op,
bool use_load_fast_reverse_unchecked) {
bool use_load_fast_reverse_unchecked,
bool is_builtin_module) {
auto cached_binop = [](Interpreter::BinaryOp bin_op) {
return RewrittenOp{BINARY_OP_ANAMORPHIC, static_cast<int32_t>(bin_op),
true};
Expand Down Expand Up @@ -178,6 +179,20 @@ static RewrittenOp rewriteOperation(const Function& function, BytecodeOp op,
return cached_inplace(Interpreter::BinaryOp::XOR);
case LOAD_ATTR:
return RewrittenOp{LOAD_ATTR_ANAMORPHIC, op.arg, true};
case LOAD_GLOBAL: {
if (is_builtin_module) {
RawTuple names = Tuple::cast(Code::cast(function.code()).names());
RawStr name = Str::cast(names.at(op.arg));
if (name.equalsCStr("_Unbound")) {
DCHECK(Unbound::object() ==
objectFromOparg(opargFromObject(Unbound::object())),
"Expected to be able to fit _Unbound in a byte");
return RewrittenOp{LOAD_IMMEDIATE, opargFromObject(Unbound::object()),
false};
}
}
break;
}
case LOAD_FAST: {
CHECK(op.arg < Code::cast(function.code()).nlocals(),
"unexpected local number");
Expand Down Expand Up @@ -305,7 +320,7 @@ void rewriteBytecode(Thread* thread, const Function& function) {
use_load_fast_reverse_unchecked = false;
continue;
}
RewrittenOp rewritten = rewriteOperation(function, op, false);
RewrittenOp rewritten = rewriteOperation(function, op, false, false);
if (rewritten.needs_inline_cache) {
num_caches++;
}
Expand All @@ -322,11 +337,15 @@ void rewriteBytecode(Thread* thread, const Function& function) {
return;
}
word cache = num_global_caches;
RawObject module = function.moduleObject();
bool is_builtin_module =
module.isModule() &&
Module::cast(module).id() == thread->runtime()->builtinsModuleId();
for (word i = 0; i < num_opcodes;) {
BytecodeOp op = nextBytecodeOp(bytecode, &i);
word previous_index = i - 1;
RewrittenOp rewritten =
rewriteOperation(function, op, use_load_fast_reverse_unchecked);
RewrittenOp rewritten = rewriteOperation(
function, op, use_load_fast_reverse_unchecked, is_builtin_module);
if (rewritten.bc == UNUSED_BYTECODE_0) continue;
if (rewritten.needs_inline_cache) {
rewrittenBytecodeOpAtPut(bytecode, previous_index, rewritten.bc);
Expand Down

0 comments on commit 58ff7ca

Please sign in to comment.