creating workflow for mirror::String compression
All-ASCII String characters are stored in 8-bit blocks
instead of 16-bit. The compression has not taken place, but all
workflow are in the code already (changing kUseStringCompression in
string.h file to TRUE will enable the feature)
Notes: Feature works on interpreter only without optimizing
Test art: m ART_TEST_INTERPRETER=true ART_TEST_OPTIMIZING=false
test-art-host
Also tested with String tests from libcore/:
1. libcore.java.lang.StringTest
2. libcore.java.lang.StringBufferTest
3. libcore.java.lang.StringBuilderTest
4. libcore.java.lang.OldStringTest
5. libcore.java.lang.OldStringBufferTest
Memory improvement is 33% (from 6.03% to 4.03%, total String memory
from all apps per total memory of all apps) measured on Angler
with Hprof tools
Bug: 31040547
Change-Id: I9cc92c265ebf1305fc06b5fc33efd83797660cce
diff --git a/runtime/mirror/object_test.cc b/runtime/mirror/object_test.cc
index 0034220..b35a479 100644
--- a/runtime/mirror/object_test.cc
+++ b/runtime/mirror/object_test.cc
@@ -62,7 +62,7 @@
Handle<String> string(
hs.NewHandle(String::AllocFromModifiedUtf8(self, expected_utf16_length, utf8_in)));
ASSERT_EQ(expected_utf16_length, string->GetLength());
- ASSERT_TRUE(string->GetValue() != nullptr);
+ ASSERT_EQ(string->IsValueNull(), false);
// strlen is necessary because the 1-character string "\x00\x00" is interpreted as ""
ASSERT_TRUE(string->Equals(utf8_in) || (expected_utf16_length == 1 && strlen(utf8_in) == 0));
ASSERT_TRUE(string->Equals(StringPiece(utf8_in)) ||
diff --git a/runtime/mirror/string-inl.h b/runtime/mirror/string-inl.h
index d3660e5..bc39ea8 100644
--- a/runtime/mirror/string-inl.h
+++ b/runtime/mirror/string-inl.h
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
#ifndef ART_RUNTIME_MIRROR_STRING_INL_H_
#define ART_RUNTIME_MIRROR_STRING_INL_H_
@@ -49,6 +48,7 @@
// Avoid AsString as object is not yet in live bitmap or allocation stack.
String* string = down_cast<String*>(obj);
string->SetCount(count_);
+ DCHECK(!string->IsCompressed() || kUseStringCompression);
}
private:
@@ -68,10 +68,19 @@
// Avoid AsString as object is not yet in live bitmap or allocation stack.
String* string = down_cast<String*>(obj);
string->SetCount(count_);
- uint16_t* value = string->GetValue();
+ DCHECK(!string->IsCompressed() || kUseStringCompression);
+ int32_t length = String::GetLengthFromCount(count_);
const uint8_t* const src = reinterpret_cast<uint8_t*>(src_array_->GetData()) + offset_;
- for (int i = 0; i < count_; i++) {
- value[i] = high_byte_ + (src[i] & 0xFF);
+ if (string->IsCompressed()) {
+ uint8_t* valueCompressed = string->GetValueCompressed();
+ for (int i = 0; i < length; i++) {
+ valueCompressed[i] = (src[i] & 0xFF);
+ }
+ } else {
+ uint16_t* value = string->GetValue();
+ for (int i = 0; i < length; i++) {
+ value[i] = high_byte_ + (src[i] & 0xFF);
+ }
}
}
@@ -96,7 +105,16 @@
String* string = down_cast<String*>(obj);
string->SetCount(count_);
const uint16_t* const src = src_array_->GetData() + offset_;
- memcpy(string->GetValue(), src, count_ * sizeof(uint16_t));
+ const int32_t length = String::GetLengthFromCount(count_);
+ bool compressible = kUseStringCompression && String::GetCompressionFlagFromCount(count_);
+ DCHECK(!compressible || kUseStringCompression);
+ if (compressible) {
+ for (int i = 0; i < length; ++i) {
+ string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
+ }
+ } else {
+ memcpy(string->GetValue(), src, length * sizeof(uint16_t));
+ }
}
private:
@@ -118,8 +136,22 @@
// Avoid AsString as object is not yet in live bitmap or allocation stack.
String* string = down_cast<String*>(obj);
string->SetCount(count_);
- const uint16_t* const src = src_string_->GetValue() + offset_;
- memcpy(string->GetValue(), src, count_ * sizeof(uint16_t));
+ const int32_t length = String::GetLengthFromCount(count_);
+ bool compressible = kUseStringCompression && String::GetCompressionFlagFromCount(count_);
+ DCHECK(!compressible || kUseStringCompression);
+ if (src_string_->IsCompressed()) {
+ const uint8_t* const src = src_string_->GetValueCompressed() + offset_;
+ memcpy(string->GetValueCompressed(), src, length * sizeof(uint8_t));
+ } else {
+ const uint16_t* const src = src_string_->GetValue() + offset_;
+ if (compressible) {
+ for (int i = 0; i < length; ++i) {
+ string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
+ }
+ } else {
+ memcpy(string->GetValue(), src, length * sizeof(uint16_t));
+ }
+ }
}
private:
@@ -133,17 +165,38 @@
}
inline uint16_t String::CharAt(int32_t index) {
- int32_t count = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_));
+ int32_t count = GetLength();
if (UNLIKELY((index < 0) || (index >= count))) {
ThrowStringIndexOutOfBoundsException(index, count);
return 0;
}
- return GetValue()[index];
+ if (IsCompressed()) {
+ return GetValueCompressed()[index];
+ } else {
+ return GetValue()[index];
+ }
+}
+
+template <typename MemoryType>
+int32_t String::FastIndexOf(MemoryType* chars, int32_t ch, int32_t start) {
+ const MemoryType* p = chars + start;
+ const MemoryType* end = chars + GetLength();
+ while (p < end) {
+ if (*p++ == ch) {
+ return (p - 1) - chars;
+ }
+ }
+ return -1;
}
template<VerifyObjectFlags kVerifyFlags>
inline size_t String::SizeOf() {
- size_t size = sizeof(String) + (sizeof(uint16_t) * GetLength<kVerifyFlags>());
+ size_t size = sizeof(String);
+ if (IsCompressed()) {
+ size += (sizeof(uint8_t) * GetLength<kVerifyFlags>());
+ } else {
+ size += (sizeof(uint16_t) * GetLength<kVerifyFlags>());
+ }
// String.equals() intrinsics assume zero-padding up to kObjectAlignment,
// so make sure the zero-padding is actually copied around if GC compaction
// chooses to copy only SizeOf() bytes.
@@ -152,31 +205,35 @@
}
template <bool kIsInstrumented, typename PreFenceVisitor>
-inline String* String::Alloc(Thread* self, int32_t utf16_length, gc::AllocatorType allocator_type,
+inline String* String::Alloc(Thread* self, int32_t utf16_length_with_flag,
+ gc::AllocatorType allocator_type,
const PreFenceVisitor& pre_fence_visitor) {
constexpr size_t header_size = sizeof(String);
- static_assert(sizeof(utf16_length) <= sizeof(size_t),
+ const bool compressible = kUseStringCompression &&
+ String::GetCompressionFlagFromCount(utf16_length_with_flag);
+ const size_t block_size = (compressible) ? sizeof(uint8_t) : sizeof(uint16_t);
+ size_t length = String::GetLengthFromCount(utf16_length_with_flag);
+ static_assert(sizeof(length) <= sizeof(size_t),
"static_cast<size_t>(utf16_length) must not lose bits.");
- size_t length = static_cast<size_t>(utf16_length);
- size_t data_size = sizeof(uint16_t) * length;
+ size_t data_size = block_size * length;
size_t size = header_size + data_size;
// String.equals() intrinsics assume zero-padding up to kObjectAlignment,
// so make sure the allocator clears the padding as well.
// http://b/23528461
size_t alloc_size = RoundUp(size, kObjectAlignment);
- Class* string_class = GetJavaLangString();
+ Class* string_class = GetJavaLangString();
// Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
// Do this by comparing with the maximum length that will _not_ cause an overflow.
- constexpr size_t overflow_length = (-header_size) / sizeof(uint16_t); // Unsigned arithmetic.
- constexpr size_t max_alloc_length = overflow_length - 1u;
+ const size_t overflow_length = (-header_size) / block_size; // Unsigned arithmetic.
+ const size_t max_alloc_length = overflow_length - 1u;
static_assert(IsAligned<sizeof(uint16_t)>(kObjectAlignment),
"kObjectAlignment must be at least as big as Java char alignment");
- constexpr size_t max_length = RoundDown(max_alloc_length, kObjectAlignment / sizeof(uint16_t));
+ const size_t max_length = RoundDown(max_alloc_length, kObjectAlignment / block_size);
if (UNLIKELY(length > max_length)) {
self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
PrettyDescriptor(string_class).c_str(),
- utf16_length).c_str());
+ static_cast<int>(length)).c_str());
return nullptr;
}
@@ -187,11 +244,22 @@
}
template <bool kIsInstrumented>
+inline String* String::AllocEmptyString(Thread* self, gc::AllocatorType allocator_type) {
+ SetStringCountVisitor visitor(0);
+ return Alloc<kIsInstrumented>(self, 0, allocator_type, visitor);
+}
+
+template <bool kIsInstrumented>
inline String* String::AllocFromByteArray(Thread* self, int32_t byte_length,
Handle<ByteArray> array, int32_t offset,
int32_t high_byte, gc::AllocatorType allocator_type) {
- SetStringCountAndBytesVisitor visitor(byte_length, array, offset, high_byte << 8);
- String* string = Alloc<kIsInstrumented>(self, byte_length, allocator_type, visitor);
+ const uint8_t* const src = reinterpret_cast<uint8_t*>(array->GetData()) + offset;
+ const bool compressible = kUseStringCompression && String::AllASCII<uint8_t>(src, byte_length)
+ && (high_byte == 0);
+ const int32_t length_with_flag = (compressible) ? String::GetFlaggedCount(byte_length)
+ : byte_length;
+ SetStringCountAndBytesVisitor visitor(length_with_flag, array, offset, high_byte << 8);
+ String* string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
return string;
}
@@ -201,16 +269,24 @@
gc::AllocatorType allocator_type) {
// It is a caller error to have a count less than the actual array's size.
DCHECK_GE(array->GetLength(), count);
- SetStringCountAndValueVisitorFromCharArray visitor(count, array, offset);
- String* new_string = Alloc<kIsInstrumented>(self, count, allocator_type, visitor);
+ const bool compressible = kUseStringCompression &&
+ String::AllASCII<uint16_t>(array->GetData() + offset, count);
+ const int32_t length_with_flag = (compressible) ? String::GetFlaggedCount(count) : count;
+ SetStringCountAndValueVisitorFromCharArray visitor(length_with_flag, array, offset);
+ String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
return new_string;
}
template <bool kIsInstrumented>
inline String* String::AllocFromString(Thread* self, int32_t string_length, Handle<String> string,
int32_t offset, gc::AllocatorType allocator_type) {
- SetStringCountAndValueVisitorFromString visitor(string_length, string, offset);
- String* new_string = Alloc<kIsInstrumented>(self, string_length, allocator_type, visitor);
+ const bool compressible = kUseStringCompression &&
+ ((string->IsCompressed()) ? true : String::AllASCII<uint16_t>(string->GetValue() + offset,
+ string_length));
+ const int32_t length_with_flag = (compressible) ? String::GetFlaggedCount(string_length)
+ : string_length;
+ SetStringCountAndValueVisitorFromString visitor(length_with_flag, string, offset);
+ String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
return new_string;
}
@@ -219,11 +295,28 @@
if (UNLIKELY(result == 0)) {
result = ComputeHashCode();
}
- DCHECK(result != 0 || ComputeUtf16Hash(GetValue(), GetLength()) == 0)
- << ToModifiedUtf8() << " " << result;
+ if (kIsDebugBuild) {
+ if (IsCompressed()) {
+ DCHECK(result != 0 || ComputeUtf16Hash(GetValueCompressed(), GetLength()) == 0)
+ << ToModifiedUtf8() << " " << result;
+ } else {
+ DCHECK(result != 0 || ComputeUtf16Hash(GetValue(), GetLength()) == 0)
+ << ToModifiedUtf8() << " " << result;
+ }
+ }
return result;
}
+template<typename MemoryType>
+bool String::AllASCII(const MemoryType* const chars, const int length) {
+ for (int i = 0; i < length; ++i) {
+ if (chars[i] > 0x80) {
+ return false;
+ }
+ }
+ return true;
+}
+
} // namespace mirror
} // namespace art
diff --git a/runtime/mirror/string.cc b/runtime/mirror/string.cc
index 33aca03..46caa4d 100644
--- a/runtime/mirror/string.cc
+++ b/runtime/mirror/string.cc
@@ -41,15 +41,11 @@
} else if (start > count) {
start = count;
}
- const uint16_t* chars = GetValue();
- const uint16_t* p = chars + start;
- const uint16_t* end = chars + count;
- while (p < end) {
- if (*p++ == ch) {
- return (p - 1) - chars;
- }
+ if (IsCompressed()) {
+ return FastIndexOf<uint8_t>(GetValueCompressed(), ch, start);
+ } else {
+ return FastIndexOf<uint16_t>(GetValue(), ch, start);
}
- return -1;
}
void String::SetClass(Class* java_lang_String) {
@@ -65,45 +61,91 @@
}
int String::ComputeHashCode() {
- const int32_t hash_code = ComputeUtf16Hash(GetValue(), GetLength());
+ int32_t hash_code = 0;
+ if (IsCompressed()) {
+ hash_code = ComputeUtf16Hash(GetValueCompressed(), GetLength());
+ } else {
+ hash_code = ComputeUtf16Hash(GetValue(), GetLength());
+ }
SetHashCode(hash_code);
return hash_code;
}
int32_t String::GetUtfLength() {
- return CountUtf8Bytes(GetValue(), GetLength());
+ if (IsCompressed()) {
+ return GetLength();
+ } else {
+ return CountUtf8Bytes(GetValue(), GetLength());
+ }
}
void String::SetCharAt(int32_t index, uint16_t c) {
- DCHECK((index >= 0) && (index < count_));
- GetValue()[index] = c;
+ DCHECK((index >= 0) && (index < GetLength()));
+ if (IsCompressed()) {
+ // TODO: Handle the case where String is compressed and c is non-ASCII
+ GetValueCompressed()[index] = static_cast<uint8_t>(c);
+ } else {
+ GetValue()[index] = c;
+ }
}
String* String::AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2) {
int32_t length = string->GetLength();
int32_t length2 = string2->GetLength();
gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
- SetStringCountVisitor visitor(length + length2);
- String* new_string = Alloc<true>(self, length + length2, allocator_type, visitor);
+ const bool compressible = kUseStringCompression && (string->IsCompressed() && string2->IsCompressed());
+ const int32_t length_with_flag = (compressible) ? String::GetFlaggedCount(length + length2)
+ : (length + length2);
+
+ SetStringCountVisitor visitor(length_with_flag);
+ String* new_string = Alloc<true>(self, length_with_flag, allocator_type, visitor);
if (UNLIKELY(new_string == nullptr)) {
return nullptr;
}
- uint16_t* new_value = new_string->GetValue();
- memcpy(new_value, string->GetValue(), length * sizeof(uint16_t));
- memcpy(new_value + length, string2->GetValue(), length2 * sizeof(uint16_t));
+ if (compressible) {
+ uint8_t* new_value = new_string->GetValueCompressed();
+ memcpy(new_value, string->GetValueCompressed(), length * sizeof(uint8_t));
+ memcpy(new_value + length, string2->GetValueCompressed(), length2 * sizeof(uint8_t));
+ } else {
+ uint16_t* new_value = new_string->GetValue();
+ if (string->IsCompressed()) {
+ for (int i = 0; i < length; ++i) {
+ new_value[i] = string->CharAt(i);
+ }
+ } else {
+ memcpy(new_value, string->GetValue(), length * sizeof(uint16_t));
+ }
+ if (string2->IsCompressed()) {
+ for (int i = 0; i < length2; ++i) {
+ new_value[i+length] = string2->CharAt(i);
+ }
+ } else {
+ memcpy(new_value + length, string2->GetValue(), length2 * sizeof(uint16_t));
+ }
+ }
return new_string;
}
String* String::AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in) {
CHECK(utf16_data_in != nullptr || utf16_length == 0);
gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
- SetStringCountVisitor visitor(utf16_length);
- String* string = Alloc<true>(self, utf16_length, allocator_type, visitor);
+ const bool compressible = kUseStringCompression &&
+ String::AllASCII<uint16_t>(utf16_data_in, utf16_length);
+ int32_t length_with_flag = (compressible) ? String::GetFlaggedCount(utf16_length)
+ : utf16_length;
+ SetStringCountVisitor visitor(length_with_flag);
+ String* string = Alloc<true>(self, length_with_flag, allocator_type, visitor);
if (UNLIKELY(string == nullptr)) {
return nullptr;
}
- uint16_t* array = string->GetValue();
- memcpy(array, utf16_data_in, utf16_length * sizeof(uint16_t));
+ if (compressible) {
+ for (int i = 0; i < utf16_length; ++i) {
+ string->GetValueCompressed()[i] = static_cast<uint8_t>(utf16_data_in[i]);
+ }
+ } else {
+ uint16_t* array = string->GetValue();
+ memcpy(array, utf16_data_in, utf16_length * sizeof(uint16_t));
+ }
return string;
}
@@ -121,13 +163,20 @@
String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
const char* utf8_data_in, int32_t utf8_length) {
gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
- SetStringCountVisitor visitor(utf16_length);
- String* string = Alloc<true>(self, utf16_length, allocator_type, visitor);
+ const bool compressible = kUseStringCompression && (utf16_length == utf8_length);
+ const int32_t utf16_length_with_flag = (compressible) ? String::GetFlaggedCount(utf16_length)
+ : utf16_length;
+ SetStringCountVisitor visitor(utf16_length_with_flag);
+ String* string = Alloc<true>(self, utf16_length_with_flag, allocator_type, visitor);
if (UNLIKELY(string == nullptr)) {
return nullptr;
}
- uint16_t* utf16_data_out = string->GetValue();
- ConvertModifiedUtf8ToUtf16(utf16_data_out, utf16_length, utf8_data_in, utf8_length);
+ if (compressible) {
+ memcpy(string->GetValueCompressed(), utf8_data_in, utf16_length * sizeof(uint8_t));
+ } else {
+ uint16_t* utf16_data_out = string->GetValue();
+ ConvertModifiedUtf8ToUtf16(utf16_data_out, utf16_length, utf8_data_in, utf8_length);
+ }
return string;
}
@@ -219,10 +268,16 @@
// Create a modified UTF-8 encoded std::string from a java/lang/String object.
std::string String::ToModifiedUtf8() {
- const uint16_t* chars = GetValue();
size_t byte_count = GetUtfLength();
std::string result(byte_count, static_cast<char>(0));
- ConvertUtf16ToModifiedUtf8(&result[0], byte_count, chars, GetLength());
+ if (IsCompressed()) {
+ for (size_t i = 0; i < byte_count; ++i) {
+ result[i] = static_cast<char>(CharAt(i));
+ }
+ } else {
+ const uint16_t* chars = GetValue();
+ ConvertUtf16ToModifiedUtf8(&result[0], byte_count, chars, GetLength());
+ }
return result;
}
@@ -242,11 +297,24 @@
int32_t rhsCount = rhs->GetLength();
int32_t countDiff = lhsCount - rhsCount;
int32_t minCount = (countDiff < 0) ? lhsCount : rhsCount;
- const uint16_t* lhsChars = lhs->GetValue();
- const uint16_t* rhsChars = rhs->GetValue();
- int32_t otherRes = MemCmp16(lhsChars, rhsChars, minCount);
- if (otherRes != 0) {
- return otherRes;
+ if (lhs->IsCompressed() && rhs->IsCompressed()) {
+ int32_t comparison = memcmp(lhs->GetValueCompressed(), rhs->GetValueCompressed(), minCount * sizeof(uint8_t));
+ if (comparison != 0) {
+ return comparison;
+ }
+ } else if (lhs->IsCompressed() || rhs->IsCompressed()) {
+ for (int32_t i = 0; i < minCount; ++i) {
+ if (lhs->CharAt(i) != rhs->CharAt(i)) {
+ return static_cast<int32_t>(lhs->CharAt(i)) - static_cast<int32_t>(rhs->CharAt(i));
+ }
+ }
+ } else {
+ const uint16_t* lhsChars = lhs->GetValue();
+ const uint16_t* rhsChars = rhs->GetValue();
+ int32_t otherRes = MemCmp16(lhsChars, rhsChars, minCount);
+ if (otherRes != 0) {
+ return otherRes;
+ }
}
return countDiff;
}
@@ -260,7 +328,14 @@
Handle<String> string(hs.NewHandle(this));
CharArray* result = CharArray::Alloc(self, GetLength());
if (result != nullptr) {
- memcpy(result->GetData(), string->GetValue(), string->GetLength() * sizeof(uint16_t));
+ if (string->IsCompressed()) {
+ int32_t length = string->GetLength();
+ for (int i = 0; i < length; ++i) {
+ result->GetData()[i] = string->CharAt(i);
+ }
+ } else {
+ memcpy(result->GetData(), string->GetValue(), string->GetLength() * sizeof(uint16_t));
+ }
} else {
self->AssertPendingOOMException();
}
@@ -269,8 +344,18 @@
void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) {
uint16_t* data = array->GetData() + index;
- uint16_t* value = GetValue() + start;
- memcpy(data, value, (end - start) * sizeof(uint16_t));
+ if (IsCompressed()) {
+ for (int i = start; i < end; ++i) {
+ data[i-start] = CharAt(i);
+ }
+ } else {
+ uint16_t* value = GetValue() + start;
+ memcpy(data, value, (end - start) * sizeof(uint16_t));
+ }
+}
+
+bool String::IsValueNull() {
+ return (IsCompressed()) ? (GetValueCompressed() == nullptr) : (GetValue() == nullptr);
}
} // namespace mirror
diff --git a/runtime/mirror/string.h b/runtime/mirror/string.h
index d492ba3..8695fe8 100644
--- a/runtime/mirror/string.h
+++ b/runtime/mirror/string.h
@@ -31,6 +31,9 @@
namespace mirror {
+// String Compression
+static constexpr bool kUseStringCompression = false;
+
// C++ mirror of java.lang.String
class MANAGED String FINAL : public Object {
public:
@@ -54,18 +57,28 @@
return &value_[0];
}
+ uint8_t* GetValueCompressed() SHARED_REQUIRES(Locks::mutator_lock_) {
+ return &value_compressed_[0];
+ }
+
template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
size_t SizeOf() SHARED_REQUIRES(Locks::mutator_lock_);
+ // Taking out the first/uppermost bit because it is not part of actual length value
template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
int32_t GetLength() SHARED_REQUIRES(Locks::mutator_lock_) {
+ return GetLengthFromCount(GetCount<kVerifyFlags>());
+ }
+
+ template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
+ int32_t GetCount() SHARED_REQUIRES(Locks::mutator_lock_) {
return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(String, count_));
}
void SetCount(int32_t new_count) SHARED_REQUIRES(Locks::mutator_lock_) {
// Count is invariant so use non-transactional mode. Also disable check as we may run inside
// a transaction.
- DCHECK_LE(0, new_count);
+ DCHECK_LE(0, (new_count & INT32_MAX));
SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count);
}
@@ -82,12 +95,6 @@
String* Intern() SHARED_REQUIRES(Locks::mutator_lock_);
- template <bool kIsInstrumented, typename PreFenceVisitor>
- ALWAYS_INLINE static String* Alloc(Thread* self, int32_t utf16_length,
- gc::AllocatorType allocator_type,
- const PreFenceVisitor& pre_fence_visitor)
- SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
-
template <bool kIsInstrumented>
ALWAYS_INLINE static String* AllocFromByteArray(Thread* self, int32_t byte_length,
Handle<ByteArray> array, int32_t offset,
@@ -107,6 +114,11 @@
gc::AllocatorType allocator_type)
SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
+ template <bool kIsInstrumented>
+ ALWAYS_INLINE static String* AllocEmptyString(Thread* self,
+ gc::AllocatorType allocator_type)
+ SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
+
static String* AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2)
SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
@@ -149,6 +161,10 @@
int32_t FastIndexOf(int32_t ch, int32_t start) SHARED_REQUIRES(Locks::mutator_lock_);
+ template <typename MemoryType>
+ int32_t FastIndexOf(MemoryType* chars, int32_t ch, int32_t start)
+ SHARED_REQUIRES(Locks::mutator_lock_);
+
int32_t CompareTo(String* other) SHARED_REQUIRES(Locks::mutator_lock_);
CharArray* ToCharArray(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_)
@@ -157,6 +173,28 @@
void GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index)
SHARED_REQUIRES(Locks::mutator_lock_);
+ template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
+ bool IsCompressed() SHARED_REQUIRES(Locks::mutator_lock_) {
+ return kUseStringCompression && GetCompressionFlagFromCount(GetCount());
+ }
+
+ bool IsValueNull() SHARED_REQUIRES(Locks::mutator_lock_);
+
+ template<typename MemoryType>
+ static bool AllASCII(const MemoryType* const chars, const int length);
+
+ ALWAYS_INLINE static bool GetCompressionFlagFromCount(const int32_t count) {
+ return kUseStringCompression && ((count & (1u << 31)) != 0);
+ }
+
+ ALWAYS_INLINE static int32_t GetLengthFromCount(const int32_t count) {
+ return kUseStringCompression ? (count & INT32_MAX) : count;
+ }
+
+ ALWAYS_INLINE static int32_t GetFlaggedCount(const int32_t count) {
+ return kUseStringCompression ? (count | (1u << 31)) : count;
+ }
+
static Class* GetJavaLangString() SHARED_REQUIRES(Locks::mutator_lock_) {
DCHECK(!java_lang_String_.IsNull());
return java_lang_String_.Read();
@@ -174,12 +212,24 @@
SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), new_hash_code);
}
+ template <bool kIsInstrumented, typename PreFenceVisitor>
+ ALWAYS_INLINE static String* Alloc(Thread* self, int32_t utf16_length_with_flag,
+ gc::AllocatorType allocator_type,
+ const PreFenceVisitor& pre_fence_visitor)
+ SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
+
// Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
+ // First bit (uppermost/leftmost) is taken out for Compressed/Uncompressed flag
+ // [0] Uncompressed: string uses 16-bit memory | [1] Compressed: 8-bit memory
int32_t count_;
uint32_t hash_code_;
- uint16_t value_[0];
+ // Compression of all-ASCII into 8-bit memory leads to usage one of these fields
+ union {
+ uint16_t value_[0];
+ uint8_t value_compressed_[0];
+ };
static GcRoot<Class> java_lang_String_;