Skip to content

Commit 76e4190

Browse files
committed
Use individual condition profiles for error handlers
1 parent 6a9fca0 commit 76e4190

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/CodecsModuleBuiltins.java

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
import com.oracle.truffle.api.frame.VirtualFrame;
9393
import com.oracle.truffle.api.library.CachedLibrary;
9494
import com.oracle.truffle.api.nodes.Node;
95-
import com.oracle.truffle.api.profiles.ValueProfile;
95+
import com.oracle.truffle.api.profiles.ConditionProfile;
9696

9797
@CoreFunctions(defineModule = "_codecs")
9898
public class CodecsModuleBuiltins extends PythonBuiltins {
@@ -116,12 +116,12 @@ public abstract static class RaiseEncodingErrorNode extends Node {
116116
public abstract RuntimeException execute(TruffleEncoder encoder, Object inputObject);
117117

118118
@Specialization
119-
public RuntimeException doRaise(TruffleEncoder encoder, Object inputObject,
119+
static RuntimeException doRaise(TruffleEncoder encoder, Object inputObject,
120120
@Cached CallNode callNode,
121121
@Cached PRaiseNode raiseNode,
122122
@CachedLanguage PythonLanguage pythonLanguage) {
123123
int start = encoder.getInputPosition();
124-
int end = start + encoder.getErrorLenght();
124+
int end = start + encoder.getErrorLength();
125125
Object exception = callNode.execute(UnicodeEncodeError, encoder.getEncodingName(), inputObject, start, end, encoder.getErrorReason());
126126
if (exception instanceof PBaseException) {
127127
throw raiseNode.raiseExceptionObject((PBaseException) exception, pythonLanguage);
@@ -139,38 +139,36 @@ public abstract static class HandleEncodingErrorNode extends Node {
139139
public abstract void execute(TruffleEncoder encoder, String errorAction, Object inputObject);
140140

141141
@Specialization
142-
void handle(TruffleEncoder encoder, String errorAction, Object inputObject,
143-
@Cached("createIdentityProfile()") ValueProfile actionProfile,
142+
static void handle(TruffleEncoder encoder, String errorAction, Object inputObject,
143+
@Cached ConditionProfile strictProfile,
144+
@Cached ConditionProfile backslashreplaceProfile,
145+
@Cached ConditionProfile surrogatepassProfile,
144146
@Cached RaiseEncodingErrorNode raiseEncodingErrorNode,
145147
@Cached PRaiseNode raiseNode) {
148+
boolean fixed;
146149
try {
147150
// Ignore and replace are handled by Java Charset
148-
switch (actionProfile.profile(errorAction)) {
149-
case STRICT:
150-
break;
151-
case BACKSLASHREPLACE:
152-
if (backslashreplace(encoder)) {
153-
return;
154-
}
155-
break;
156-
case SURROGATEPASS:
157-
if (surrogatepass(encoder)) {
158-
return;
159-
}
160-
break;
161-
default:
162-
raiseNode.raise(LookupError, ErrorMessages.UNKNOWN_ERROR_HANDLER, errorAction);
151+
if (strictProfile.profile(STRICT.equals(errorAction))) {
152+
fixed = false;
153+
} else if (backslashreplaceProfile.profile(BACKSLASHREPLACE.equals(errorAction))) {
154+
fixed = backslashreplace(encoder);
155+
} else if (surrogatepassProfile.profile(SURROGATEPASS.equals(errorAction))) {
156+
fixed = surrogatepass(encoder);
157+
} else {
158+
throw raiseNode.raise(LookupError, ErrorMessages.UNKNOWN_ERROR_HANDLER, errorAction);
163159
}
164160
} catch (OutOfMemoryError e) {
165161
CompilerDirectives.transferToInterpreterAndInvalidate();
166162
throw raiseNode.raise(MemoryError);
167163
}
168-
throw raiseEncodingErrorNode.execute(encoder, inputObject);
164+
if (!fixed) {
165+
throw raiseEncodingErrorNode.execute(encoder, inputObject);
166+
}
169167
}
170168

171169
@TruffleBoundary
172170
private static boolean backslashreplace(TruffleEncoder encoder) {
173-
String p = new String(encoder.getInputChars(encoder.getErrorLenght()));
171+
String p = new String(encoder.getInputChars(encoder.getErrorLength()));
174172
StringBuilder sb = new StringBuilder();
175173
byte[] buf = new byte[10];
176174
for (int i = 0; i < p.length();) {
@@ -195,7 +193,7 @@ private static boolean backslashreplace(TruffleEncoder encoder) {
195193
private static boolean surrogatepass(TruffleEncoder encoder) {
196194
// UTF-8 only for now. The name should be normalized already
197195
if (encoder.getEncodingName().equals("utf_8")) {
198-
String p = new String(encoder.getInputChars(encoder.getErrorLenght()));
196+
String p = new String(encoder.getInputChars(encoder.getErrorLength()));
199197
byte[] replacement = new byte[p.length() * 3];
200198
int outp = 0;
201199
for (int i = 0; i < p.length();) {
@@ -209,7 +207,7 @@ private static boolean surrogatepass(TruffleEncoder encoder) {
209207
replacement[outp++] = (byte) (0x80 | (ch & 0x3f));
210208
i += Character.charCount(ch);
211209
}
212-
encoder.replace(encoder.getErrorLenght(), replacement, 0, outp);
210+
encoder.replace(encoder.getErrorLength(), replacement, 0, outp);
213211
return true;
214212
}
215213
return false;
@@ -225,12 +223,12 @@ public abstract static class RaiseDecodingErrorNode extends Node {
225223
public abstract RuntimeException execute(TruffleDecoder decoder, Object inputObject);
226224

227225
@Specialization
228-
public RuntimeException doRaise(TruffleDecoder decoder, Object inputObject,
226+
static RuntimeException doRaise(TruffleDecoder decoder, Object inputObject,
229227
@Cached CallNode callNode,
230228
@Cached PRaiseNode raiseNode,
231229
@CachedLanguage PythonLanguage pythonLanguage) {
232230
int start = decoder.getInputPosition();
233-
int end = start + decoder.getErrorLenght();
231+
int end = start + decoder.getErrorLength();
234232
Object exception = callNode.execute(UnicodeDecodeError, decoder.getEncodingName(), inputObject, start, end, decoder.getErrorReason());
235233
if (exception instanceof PBaseException) {
236234
throw raiseNode.raiseExceptionObject((PBaseException) exception, pythonLanguage);
@@ -248,33 +246,36 @@ public abstract static class HandleDecodingErrorNode extends Node {
248246
public abstract void execute(TruffleDecoder decoder, String errorAction, Object inputObject);
249247

250248
@Specialization
251-
void doStrict(TruffleDecoder decoder, String errorAction, Object inputObject,
252-
@Cached("createIdentityProfile()") ValueProfile actionProfile,
249+
static void doStrict(TruffleDecoder decoder, String errorAction, Object inputObject,
250+
@Cached ConditionProfile strictProfile,
251+
@Cached ConditionProfile backslashreplaceProfile,
252+
@Cached ConditionProfile surrogatepassProfile,
253253
@Cached RaiseDecodingErrorNode raiseDecodingErrorNode,
254254
@Cached PRaiseNode raiseNode) {
255-
// Ignore and replace are handled by Java Charset
256-
switch (actionProfile.profile(errorAction)) {
257-
case STRICT:
258-
break;
259-
case BACKSLASHREPLACE:
260-
if (backslashreplace(decoder)) {
261-
return;
262-
}
263-
break;
264-
case SURROGATEPASS:
265-
if (surrogatepass(decoder)) {
266-
return;
267-
}
268-
break;
269-
default:
270-
raiseNode.raise(LookupError, ErrorMessages.UNKNOWN_ERROR_HANDLER, errorAction);
255+
boolean fixed;
256+
try {
257+
// Ignore and replace are handled by Java Charset
258+
if (strictProfile.profile(STRICT.equals(errorAction))) {
259+
fixed = false;
260+
} else if (backslashreplaceProfile.profile(BACKSLASHREPLACE.equals(errorAction))) {
261+
fixed = backslashreplace(decoder);
262+
} else if (surrogatepassProfile.profile(SURROGATEPASS.equals(errorAction))) {
263+
fixed = surrogatepass(decoder);
264+
} else {
265+
throw raiseNode.raise(LookupError, ErrorMessages.UNKNOWN_ERROR_HANDLER, errorAction);
266+
}
267+
} catch (OutOfMemoryError e) {
268+
CompilerDirectives.transferToInterpreterAndInvalidate();
269+
throw raiseNode.raise(MemoryError);
270+
}
271+
if (!fixed) {
272+
throw raiseDecodingErrorNode.execute(decoder, inputObject);
271273
}
272-
throw raiseDecodingErrorNode.execute(decoder, inputObject);
273274
}
274275

275276
@TruffleBoundary
276277
private static boolean backslashreplace(TruffleDecoder decoder) {
277-
byte[] p = decoder.getInputBytes(decoder.getErrorLenght());
278+
byte[] p = decoder.getInputBytes(decoder.getErrorLength());
278279
char[] replacement = new char[p.length * 4];
279280
int outp = 0;
280281
byte[] buf = new byte[4];
@@ -515,7 +516,7 @@ private void handleDecodingError(TruffleDecoder encoder, String errorAction, Obj
515516
@GenerateNodeFactory
516517
abstract static class CodecsLookupNode extends PythonBuiltinNode {
517518
@Specialization
518-
Object lookup(String encoding) {
519+
static Object lookup(String encoding) {
519520
if (CharsetMapping.getCharset(encoding) != null) {
520521
return true;
521522
} else {
@@ -623,7 +624,7 @@ public int getInputPosition() {
623624
}
624625

625626
@TruffleBoundary
626-
public int getErrorLenght() {
627+
public int getErrorLength() {
627628
return coderResult.length();
628629
}
629630

@@ -757,7 +758,7 @@ public int getInputPosition() {
757758
}
758759

759760
@TruffleBoundary
760-
public int getErrorLenght() {
761+
public int getErrorLength() {
761762
return coderResult.length();
762763
}
763764

0 commit comments

Comments
 (0)