diff options
author | 2017-02-20 14:08:30 +0000 | |
---|---|---|
committer | 2017-02-21 12:31:52 +0000 | |
commit | 92907f3f7af106a9eeb341caba17f3770203f7fb (patch) | |
tree | c44f6b487c9ad078c044f19a4a6d72eaf4979d11 /runtime/native/java_lang_String.cc | |
parent | 1b7ba9e15d9525eec7ea68e9ecf95e5777a46069 (diff) |
Remove String.setCharAt().
The internal API String.setCharAt() breaks the assumption
that strings are really immutable. That in turn breaks
string compression invariants - compressible strings must
be compressed. This CL removes the String.setCharAt() API.
The method was used only in String.replace(char, char)
when we found a match, copying the string on first match.
Instead, introduce a new native method that does the whole
replacement with a single call when we find the first match.
StringReplaceBenchmark results on Nexus 6P, lower is better:
timeReplaceCharNonExistent/EMPTY 41.93 -> 38.25 (-9%)
timeReplaceCharNonExistent/L_16 114.90 -> 95.09 (-17%)
timeReplaceCharNonExistent/L_64 419.97 -> 320.65 (-24%)
timeReplaceCharNonExistent/L_256 1667.01 -> 1091.25 (-35%)
timeReplaceCharNonExistent/L_512 3253.50 -> 2075.62 (-36%)
timeReplaceCharRepeated/EMPTY 41.93 -> 39.58 (-6%)
timeReplaceCharRepeated/L_16 114.87 -> 95.40 (-17%)
timeReplaceCharRepeated/L_64 1267.29 -> 704.32 (-44%)
timeReplaceCharRepeated/L_256 5139.14 -> 1361.80 (-74%)
timeReplaceCharRepeated/L_512 10787.81 -> 2338.41 (-78%)
timeReplaceSingleChar/EMPTY 41.78 -> 37.16 (-11%)
timeReplaceSingleChar/L_16 449.54 -> 497.51 (+11%)
timeReplaceSingleChar/L_64 942.08 -> 891.35 (-5%)
timeReplaceSingleChar/L_256 2756.18 -> 2174.64 (-21%)
timeReplaceSingleChar/L_512 5489.91 -> 3983.32 (-27%)
Test: testrunner.py --host
Test: run-libcore-tests.sh --mode=host
Test: testrunner.py --host with string compression enabled.
Test: run-libcore-tests.sh --mode=host with string compression enabled.
Bug: 31040547
Change-Id: I9cf0d5457182f0a33ca8251c29931d3eb624ae07
Diffstat (limited to 'runtime/native/java_lang_String.cc')
-rw-r--r-- | runtime/native/java_lang_String.cc | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/runtime/native/java_lang_String.cc b/runtime/native/java_lang_String.cc index f1d6ff5f70..1357338464 100644 --- a/runtime/native/java_lang_String.cc +++ b/runtime/native/java_lang_String.cc @@ -99,9 +99,11 @@ static jstring String_intern(JNIEnv* env, jobject java_this) { return soa.AddLocalReference<jstring>(result); } -static void String_setCharAt(JNIEnv* env, jobject java_this, jint index, jchar c) { +static jstring String_doReplace(JNIEnv* env, jobject java_this, jchar old_c, jchar new_c) { ScopedFastNativeObjectAccess soa(env); - soa.Decode<mirror::String>(java_this)->SetCharAt(index, c); + ObjPtr<mirror::String> result = + soa.Decode<mirror::String>(java_this)->DoReplace(soa.Self(), old_c, new_c); + return soa.AddLocalReference<jstring>(result); } static jcharArray String_toCharArray(JNIEnv* env, jobject java_this) { @@ -114,11 +116,11 @@ static JNINativeMethod gMethods[] = { NATIVE_METHOD(String, charAt, "!(I)C"), NATIVE_METHOD(String, compareTo, "!(Ljava/lang/String;)I"), NATIVE_METHOD(String, concat, "!(Ljava/lang/String;)Ljava/lang/String;"), + NATIVE_METHOD(String, doReplace, "!(CC)Ljava/lang/String;"), NATIVE_METHOD(String, fastIndexOf, "!(II)I"), NATIVE_METHOD(String, fastSubstring, "!(II)Ljava/lang/String;"), NATIVE_METHOD(String, getCharsNoCheck, "!(II[CI)V"), NATIVE_METHOD(String, intern, "!()Ljava/lang/String;"), - NATIVE_METHOD(String, setCharAt, "!(IC)V"), NATIVE_METHOD(String, toCharArray, "!()[C"), }; |