JoshRosen commented on a change in pull request #24709: [SPARK-27841][SQL] 
Improve UTF8String to/fromString()/numBytesForFirstByte() performance
URL: https://github.com/apache/spark/pull/24709#discussion_r287611545
 
 

 ##########
 File path: 
common/unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java
 ##########
 @@ -1217,6 +1264,20 @@ public boolean toByte(IntWrapper intWrapper) {
 
   @Override
   public String toString() {
+    byte[] bytes = getBytes();
+    // Optimization for ASCII characters: use deprecated string API which
+    // skips charset encoder and simply casts each byte into a char.
+    if (isAscii(bytes)) {
+      return new String(bytes, 0);
 
 Review comment:
   That's a good question. I think it might be a little bit slower because the 
`US_ASCII` encoder needs to perform error-checking to validate whether the 
bytes are actually ASCII characters. That validation is probably cheap (just 
checking the sign / highest order bit of the byte) but it's still technically a 
bit more work.
   
   Good point about benchmark resolution, especially for short strings: I 
probably need to run more benchmark iterations for short strings to smooth out 
variation / noise.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to