Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c33475e

Browse files
committedDec 7, 2022
Rewrite LOAD_GLOBAL _Unbound to LOAD_IMMEDIATE in builtins module
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.
1 parent 80d8286 commit c33475e

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed
 

‎runtime/bytecode.cpp

+20-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ struct RewrittenOp {
8989
bool needs_inline_cache;
9090
};
9191

92-
static RewrittenOp rewriteOperation(const Function& function, BytecodeOp op,
92+
static RewrittenOp rewriteOperation(Thread* thread, const Function& function,
93+
BytecodeOp op,
9394
bool use_load_fast_reverse_unchecked) {
9495
auto cached_binop = [](Interpreter::BinaryOp bin_op) {
9596
return RewrittenOp{BINARY_OP_ANAMORPHIC, static_cast<int32_t>(bin_op),
@@ -178,6 +179,22 @@ static RewrittenOp rewriteOperation(const Function& function, BytecodeOp op,
178179
return cached_inplace(Interpreter::BinaryOp::XOR);
179180
case LOAD_ATTR:
180181
return RewrittenOp{LOAD_ATTR_ANAMORPHIC, op.arg, true};
182+
case LOAD_GLOBAL: {
183+
RawObject module = function.moduleObject();
184+
if (module.isModule() &&
185+
Module::cast(module).id() == thread->runtime()->builtinsModuleId()) {
186+
RawTuple names = Tuple::cast(Code::cast(function.code()).names());
187+
RawStr name = Str::cast(names.at(op.arg));
188+
if (name.equalsCStr("_Unbound")) {
189+
DCHECK(Unbound::object() ==
190+
objectFromOparg(opargFromObject(Unbound::object())),
191+
"Expected to be able to fit _Unbound in a byte");
192+
return RewrittenOp{LOAD_IMMEDIATE, opargFromObject(Unbound::object()),
193+
false};
194+
}
195+
}
196+
break;
197+
}
181198
case LOAD_FAST: {
182199
CHECK(op.arg < Code::cast(function.code()).nlocals(),
183200
"unexpected local number");
@@ -305,7 +322,7 @@ void rewriteBytecode(Thread* thread, const Function& function) {
305322
use_load_fast_reverse_unchecked = false;
306323
continue;
307324
}
308-
RewrittenOp rewritten = rewriteOperation(function, op, false);
325+
RewrittenOp rewritten = rewriteOperation(thread, function, op, false);
309326
if (rewritten.needs_inline_cache) {
310327
num_caches++;
311328
}
@@ -326,7 +343,7 @@ void rewriteBytecode(Thread* thread, const Function& function) {
326343
BytecodeOp op = nextBytecodeOp(bytecode, &i);
327344
word previous_index = i - 1;
328345
RewrittenOp rewritten =
329-
rewriteOperation(function, op, use_load_fast_reverse_unchecked);
346+
rewriteOperation(thread, function, op, use_load_fast_reverse_unchecked);
330347
if (rewritten.bc == UNUSED_BYTECODE_0) continue;
331348
if (rewritten.needs_inline_cache) {
332349
rewrittenBytecodeOpAtPut(bytecode, previous_index, rewritten.bc);

0 commit comments

Comments
 (0)
Please sign in to comment.