Skip to content

Commit d0083b1

Browse files
committed
Speed up boolean-token fallback in StringArray.getAsDouble
Replace the toLowerCase plus equals chain in the parse fallback with a length-based dispatch: a single char compare for the 1-char "t"/"f" tokens and compareToIgnoreCase for "true"/"false", matching the idiom already used in DoubleArray.parseDouble. This avoids allocating a lower-cased copy and rejects non-boolean strings immediately. Restore throwing DMLRuntimeException on unparseable input. The previous re-throw of the raw NumberFormatException changed the exception type and broke callers such as Array.extractDouble that expect DMLRuntimeException; the throw path is the genuinely-exceptional case, so the wrapping cost is irrelevant there.
1 parent 6b4e2fb commit d0083b1

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/main/java/org/apache/sysds/runtime/frame/data/columns/StringArray.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -610,14 +610,22 @@ private static double getAsDouble(String s) {
610610
return DoubleArray.parseDouble(s);
611611
}
612612
catch(Exception e) {
613-
String ls = s.toLowerCase();
614-
if(ls.equals("true") || ls.equals("t"))
613+
// Fallback for boolean-like tokens. Dispatch on length first so non-boolean strings are
614+
// rejected immediately, and avoid allocating a lower-cased copy by comparing case-insensitively
615+
// (single char compare for the 1-char tokens).
616+
final int len = s.length();
617+
if(len == 1) {
618+
final char c = s.charAt(0);
619+
if(c == 't' || c == 'T')
620+
return 1;
621+
else if(c == 'f' || c == 'F')
622+
return 0;
623+
}
624+
else if(len == 4 && s.compareToIgnoreCase("true") == 0)
615625
return 1;
616-
else if(ls.equals("false") || ls.equals("f"))
626+
else if(len == 5 && s.compareToIgnoreCase("false") == 0)
617627
return 0;
618-
else
619-
throw e; // for efficiency
620-
// throw new DMLRuntimeException("Unable to change to double: " + s, e);
628+
throw new DMLRuntimeException("Unable to change to double: " + s, e);
621629
}
622630
}
623631

0 commit comments

Comments
 (0)