Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: String#getBytes(Charset) vs getBytes(String) #18

Closed
wants to merge 3 commits into from

Conversation

phraktle
Copy link
Contributor

@phraktle phraktle commented Sep 9, 2014

Use the cleaner and slightly faster getBytes on Strings.

@xfxyjwf
Copy link
Contributor

xfxyjwf commented Sep 9, 2014

Please sign this Google Individual Contributor License Agreement:
https://developers.google.com/open-source/cla/individual?csw=1

and let me know when you are done. It's required for us to use your code.

*/
public abstract String toString(String charsetName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a public method. Removing it will break existing clients. Please keep the old one and add a new method instead.

@phraktle
Copy link
Contributor Author

Filled out the CLA as requested. Will look into re-adding the method w/ the original signature shortly.

@phraktle
Copy link
Contributor Author

Do you think the original method should be marked @deprecated ?

@xfxyjwf
Copy link
Contributor

xfxyjwf commented Sep 10, 2014

No. The original method is used widely in our internal code base. Unless we are willing to do a global clean-up and replace all call-sites (which will take weeks or months and isn't worthwhile in this case), we shouldn't mark the old method as deprecated.

@@ -36,6 +36,8 @@
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove use of this StandardCharsets.

This StandardCharsets is introduced only since jdk 1.7. Our library supports back to jdk 1.5. We probably can drop 1.5 support soon but I believe jdk 1.6 will be supported for quite some time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed use of StandardCharsets. Since String#getBytes(Charset) was introduced in jdk 1.6 (which is the whole point of this change), support for jdk 1.5 cannot be provided.

ByteString chopped = unicode.substring(2, unicode.size() - 6);
assertEquals(classUnderTest + ".substring() must have the expected type",
classUnderTest, getActualClassName(chopped));

String roundTripString = chopped.toString(UTF_8);
String roundTripString = chopped.toString(ByteString.UTF_8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test both ByteString.toString(String) and ByteString.toString(Charset) here (you added a test case for the latter but removed the test case for the former).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants