Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Unified Diff: runtime/vm/object.h

Issue 2893553002: More compact string representation on 64 bit. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/vm/object.h
diff --git a/runtime/vm/object.h b/runtime/vm/object.h
index 603467775b9072d2a688818e3bf1ac6d66e6ecaa..f4d2303bff388afd7ad4ae496a4568fdab142ce2 100644
--- a/runtime/vm/object.h
+++ b/runtime/vm/object.h
@@ -6752,8 +6752,11 @@ class String : public Instance {
// All strings share the same maximum element count to keep things
// simple. We choose a value that will prevent integer overflow for
// 2 byte strings, since it is the worst case.
- static const intptr_t kSizeofRawString =
- sizeof(RawInstance) + (2 * kWordSize);
+#if defined(HASH_IN_OBJECT_HEADER)
+ static const intptr_t kSizeofRawString = sizeof(RawInstance) + kWordSize;
+#else
+ static const intptr_t kSizeofRawString = sizeof(RawInstance) + 2 * kWordSize;
+#endif
static const intptr_t kMaxElements = kSmiMax / kTwoByteChar;
class CodePointIterator : public ValueObject {
@@ -6789,7 +6792,7 @@ class String : public Instance {
static intptr_t length_offset() { return OFFSET_OF(RawString, length_); }
intptr_t Hash() const {
- intptr_t result = Smi::Value(raw_ptr()->hash_);
+ intptr_t result = GetRawHash();
Vyacheslav Egorov (Google) 2017/05/18 06:14:42 Maybe call it GetCachedHash(...)?
erikcorry 2017/05/18 14:28:38 Done.
if (result != 0) {
return result;
}
@@ -6800,17 +6803,23 @@ class String : public Instance {
bool HasHash() const {
ASSERT(Smi::New(0) == NULL);
- return (raw_ptr()->hash_ != NULL);
+ return GetRawHash() != 0;
}
+#if defined(HASH_IN_OBJECT_HEADER)
+ static intptr_t hash_offset() { return kInt32Size; } // Wrong for big-endian?
Vyacheslav Egorov (Google) 2017/05/18 06:14:42 we don't support big-endian afaik
erikcorry 2017/05/18 14:28:38 No, but if we ever do, it's nice to leave hints th
+#else
static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); }
+#endif
+ uint32_t GetRawHash() const { return raw()->GetRawHash(); }
+ void SetRawHash(uint32_t hash) { raw()->SetRawHash(hash); }
static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len);
static intptr_t Hash(const char* characters, intptr_t len);
static intptr_t Hash(const uint16_t* characters, intptr_t len);
static intptr_t Hash(const int32_t* characters, intptr_t len);
static intptr_t HashRawSymbol(const RawString* symbol) {
ASSERT(symbol->IsCanonical());
- intptr_t result = Smi::Value(symbol->ptr()->hash_);
+ intptr_t result = symbol->GetRawHash();
ASSERT(result != 0);
return result;
}
@@ -7042,11 +7051,7 @@ class String : public Instance {
StoreSmi(&raw_ptr()->length_, Smi::New(value));
}
- void SetHash(intptr_t value) const {
- // This is only safe because we create a new Smi, which does not cause
- // heap allocation.
- StoreSmi(&raw_ptr()->hash_, Smi::New(value));
- }
+ void SetHash(intptr_t value) const { raw()->SetRawHash(value); }
template <typename HandleType, typename ElementType, typename CallbackType>
static void ReadFromImpl(SnapshotReader* reader,
@@ -7105,6 +7110,12 @@ class OneByteString : public AllStatic {
static intptr_t InstanceSize(intptr_t len) {
ASSERT(sizeof(RawOneByteString) == String::kSizeofRawString);
ASSERT(0 <= len && len <= kMaxElements);
+#if defined(HASH_IN_OBJECT_HEADER)
+ // We have to pad zero-length raw strings so that they can be externalized.
+ // If we don't pad, then the external string object does not fit in the
+ // memory allocated for the raw string.
+ if (len == 0) return InstanceSize(1);
+#endif
return String::RoundedAllocationSize(sizeof(RawOneByteString) +
(len * kBytesPerElement));
}
@@ -7238,6 +7249,10 @@ class TwoByteString : public AllStatic {
static intptr_t InstanceSize(intptr_t len) {
ASSERT(sizeof(RawTwoByteString) == String::kSizeofRawString);
ASSERT(0 <= len && len <= kMaxElements);
+ // We have to pad zero-length raw strings so that they can be externalized.
+ // If we don't pad, then the external string object does not fit in the
+ // memory allocated for the raw string.
+ if (len == 0) return InstanceSize(1);
return String::RoundedAllocationSize(sizeof(RawTwoByteString) +
(len * kBytesPerElement));
}
@@ -8931,7 +8946,7 @@ bool String::Equals(const String& str) const {
intptr_t Library::UrlHash() const {
- intptr_t result = Smi::Value(url()->ptr()->hash_);
+ intptr_t result = url()->GetRawHash();
ASSERT(result != 0);
return result;
}

Powered by Google App Engine
This is Rietveld 408576698