Skip to content

Commit fdb8b25

Browse files
committed
Add fast paths to bytecode frame materialization
1 parent 8652fcd commit fdb8b25

File tree

2 files changed

+178
-49
lines changed

2 files changed

+178
-49
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeRootNode.java

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
*/
4141
package com.oracle.graal.python.nodes.bytecode;
4242

43-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.KeyError;
4443
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.RecursionError;
4544
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SystemError;
4645
import static com.oracle.graal.python.nodes.BuiltinNames.__BUILD_CLASS__;
@@ -120,7 +119,6 @@
120119
import com.oracle.graal.python.nodes.frame.ReadNameNode;
121120
import com.oracle.graal.python.nodes.frame.WriteGlobalNode;
122121
import com.oracle.graal.python.nodes.frame.WriteNameNode;
123-
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
124122
import com.oracle.graal.python.nodes.object.IsNode;
125123
import com.oracle.graal.python.nodes.statement.ExceptNode.ExceptMatchNode;
126124
import com.oracle.graal.python.nodes.statement.ExceptionHandlingStatementNode;
@@ -423,35 +421,41 @@ public int getLineno(Frame frame) {
423421
return bciToLine(getBci(frame));
424422
}
425423

426-
public void syncLocals(VirtualFrame virtualFrame, Object localsObject, Frame frameToSync, PyObjectSetItem setItem, PyObjectDelItem delItem, IsBuiltinClassProfile errorProfile) {
427-
Frame localFrame = frameToSync;
424+
public int getVariableCount() {
428425
CodeUnit code = rootNode.co;
429-
if (code.isGeneratorOrCoroutine()) {
430-
localFrame = PArguments.getGeneratorFrame(frameToSync);
426+
return code.varnames.length + code.cellvars.length + code.freevars.length;
427+
}
428+
429+
public String getVariableName(int slot) {
430+
CodeUnit code = rootNode.co;
431+
if (slot < code.varnames.length) {
432+
return code.varnames[slot];
433+
} else if (slot < code.varnames.length + code.cellvars.length) {
434+
return code.cellvars[slot - code.varnames.length];
435+
} else {
436+
return code.freevars[slot - code.varnames.length - code.cellvars.length];
431437
}
438+
}
439+
440+
@TruffleBoundary
441+
public int findVariable(String name) {
442+
CodeUnit code = rootNode.co;
432443
for (int i = 0; i < code.varnames.length; i++) {
433-
setVar(virtualFrame, localsObject, setItem, delItem, errorProfile, code.varnames[i], localFrame.getObject(i));
444+
if (name.equals(code.varnames[i])) {
445+
return i;
446+
}
434447
}
435448
for (int i = 0; i < code.cellvars.length; i++) {
436-
PCell cell = (PCell) localFrame.getObject(rootNode.celloffset + i);
437-
setVar(virtualFrame, localsObject, setItem, delItem, errorProfile, code.cellvars[i], cell.getRef());
449+
if (name.equals(code.cellvars[i])) {
450+
return code.varnames.length + i;
451+
}
438452
}
439453
for (int i = 0; i < code.freevars.length; i++) {
440-
PCell cell = (PCell) localFrame.getObject(rootNode.freeoffset + i);
441-
setVar(virtualFrame, localsObject, setItem, delItem, errorProfile, code.freevars[i], cell.getRef());
442-
}
443-
}
444-
445-
private void setVar(VirtualFrame virtualFrame, Object localsObject, PyObjectSetItem setItem, PyObjectDelItem delItem, IsBuiltinClassProfile errorProfile, String name, Object value) {
446-
if (value == null) {
447-
try {
448-
delItem.execute(virtualFrame, localsObject, name);
449-
} catch (PException e) {
450-
e.expect(KeyError, errorProfile);
454+
if (name.equals(code.freevars[i])) {
455+
return code.varnames.length + code.cellvars.length + i;
451456
}
452-
} else {
453-
setItem.execute(virtualFrame, localsObject, name, value);
454457
}
458+
return -1;
455459
}
456460
}
457461

@@ -463,11 +467,17 @@ private static FrameDescriptor makeFrameDescriptor(CodeUnit co) {
463467
FrameDescriptor.Builder newBuilder = FrameDescriptor.newBuilder(capacity);
464468
newBuilder.info(new FrameInfo());
465469
// locals
466-
newBuilder.addSlots(co.varnames.length, FrameSlotKind.Illegal);
470+
for (int i = 0; i < co.varnames.length; i++) {
471+
newBuilder.addSlot(FrameSlotKind.Illegal, co.varnames[i], null);
472+
}
467473
// cells
468-
newBuilder.addSlots(co.cellvars.length, FrameSlotKind.Illegal);
474+
for (int i = 0; i < co.cellvars.length; i++) {
475+
newBuilder.addSlot(FrameSlotKind.Illegal, co.cellvars[i], null);
476+
}
469477
// freevars
470-
newBuilder.addSlots(co.freevars.length, FrameSlotKind.Illegal);
478+
for (int i = 0; i < co.freevars.length; i++) {
479+
newBuilder.addSlot(FrameSlotKind.Illegal, co.freevars[i], null);
480+
}
471481
// stack
472482
newBuilder.addSlots(co.stacksize, FrameSlotKind.Illegal);
473483
// BCI filled when unwinding the stack or when pausing generators

0 commit comments

Comments
 (0)