diff --git a/runtime/vm/compiler/assembler/assembler_base.cc b/runtime/vm/compiler/assembler/assembler_base.cc index bc9f9a6025c..50bdba70134 100644 --- a/runtime/vm/compiler/assembler/assembler_base.cc +++ b/runtime/vm/compiler/assembler/assembler_base.cc @@ -640,16 +640,14 @@ void AssemblerBase::Stop(const char* message) { uword ObjIndexPair::Hash(Key key) { switch (key.type()) { case ObjectPoolBuilderEntry::kImmediate128: - return key.imm128_.int_storage[0] ^ key.imm128_.int_storage[1] ^ - key.imm128_.int_storage[2] ^ key.imm128_.int_storage[3]; - + return HashBytes(&key.imm128_, sizeof(key.imm128_)); #if defined(TARGET_ARCH_IS_32_BIT) case ObjectPoolBuilderEntry::kImmediate64: - return key.imm64_; + return HashBytes(&key.imm64_, sizeof(key.imm64_)); #endif case ObjectPoolBuilderEntry::kImmediate: case ObjectPoolBuilderEntry::kNativeFunction: - return key.imm_; + return HashBytes(&key.imm_, sizeof(key.imm_)); case ObjectPoolBuilderEntry::kTaggedObject: return ObjectHash(*key.obj_); } diff --git a/runtime/vm/hash.h b/runtime/vm/hash.h index ad00e892d49..745a9bdaccb 100644 --- a/runtime/vm/hash.h +++ b/runtime/vm/hash.h @@ -28,14 +28,21 @@ inline uint32_t FinalizeHash(uint32_t hash, intptr_t hashbits = kBitsPerInt32) { return (hash == 0) ? 1 : hash; } -inline uint32_t HashBytes(const uint8_t* bytes, intptr_t size) { - uint32_t hash = size; - while (size > 0) { - hash = CombineHashes(hash, *bytes); - bytes++; - size--; +inline uint32_t HashBytes(const void* bytes, + intptr_t len, + intptr_t hashbits = kBitsPerInt32) { + if (len == 0) { + return 1; } - return hash; + uint32_t hash = len; + const intptr_t chunks = len / kInt32Size; + for (intptr_t i = 0; i < chunks; i++) { + hash = CombineHashes(hash, reinterpret_cast(bytes)[i]); + } + for (intptr_t i = chunks * kInt32Size; i < len; i++) { + hash = CombineHashes(hash, reinterpret_cast(bytes)[i]); + } + return FinalizeHash(hash, hashbits); } } // namespace dart diff --git a/runtime/vm/object.cc b/runtime/vm/object.cc index 598eeaf4059..4fbd65cbf97 100644 --- a/runtime/vm/object.cc +++ b/runtime/vm/object.cc @@ -25978,13 +25978,14 @@ float Float32x4::w() const { } bool Float32x4::CanonicalizeEquals(const Instance& other) const { + NoSafepointScope no_safepoint; return memcmp(&untag()->value_, Float32x4::Cast(other).untag()->value_, sizeof(simd128_value_t)) == 0; } uint32_t Float32x4::CanonicalizeHash() const { - return HashBytes(reinterpret_cast(&untag()->value_), - sizeof(simd128_value_t)); + NoSafepointScope no_safepoint; + return HashBytes(&untag()->value_, sizeof(simd128_value_t), kHashBits); } const char* Float32x4::ToCString() const { @@ -26062,13 +26063,14 @@ void Int32x4::set_value(simd128_value_t value) const { } bool Int32x4::CanonicalizeEquals(const Instance& other) const { + NoSafepointScope no_safepoint; return memcmp(&untag()->value_, Int32x4::Cast(other).untag()->value_, sizeof(simd128_value_t)) == 0; } uint32_t Int32x4::CanonicalizeHash() const { - return HashBytes(reinterpret_cast(&untag()->value_), - sizeof(simd128_value_t)); + NoSafepointScope no_safepoint; + return HashBytes(&untag()->value_, sizeof(simd128_value_t), kHashBits); } const char* Int32x4::ToCString() const { @@ -26122,13 +26124,14 @@ void Float64x2::set_value(simd128_value_t value) const { } bool Float64x2::CanonicalizeEquals(const Instance& other) const { + NoSafepointScope no_safepoint; return memcmp(&untag()->value_, Float64x2::Cast(other).untag()->value_, sizeof(simd128_value_t)) == 0; } uint32_t Float64x2::CanonicalizeHash() const { - return HashBytes(reinterpret_cast(&untag()->value_), - sizeof(simd128_value_t)); + NoSafepointScope no_safepoint; + return HashBytes(&untag()->value_, sizeof(simd128_value_t), kHashBits); } const char* Float64x2::ToCString() const { @@ -26181,19 +26184,8 @@ bool TypedData::CanonicalizeEquals(const Instance& other) const { } uint32_t TypedData::CanonicalizeHash() const { - const intptr_t len = this->LengthInBytes(); - if (len == 0) { - return 1; - } - uint32_t hash = len; - const intptr_t chunks = len / kInt32Size; - for (intptr_t i = 0; i < chunks; i++) { - hash = CombineHashes(hash, GetUint32(i * kInt32Size)); - } - for (intptr_t i = chunks * kInt32Size; i < len; i++) { - hash = CombineHashes(hash, GetUint8(i)); - } - return FinalizeHash(hash, kHashBits); + NoSafepointScope no_safepoint; + return HashBytes(DataAddr(0), LengthInBytes(), kHashBits); } TypedDataPtr TypedData::New(intptr_t class_id, diff --git a/runtime/vm/object.h b/runtime/vm/object.h index b6775cc7f5b..ff7cdb6b696 100644 --- a/runtime/vm/object.h +++ b/runtime/vm/object.h @@ -5959,7 +5959,7 @@ class Instructions : public Object { uint32_t Hash() const { return Hash(ptr()); } static uint32_t Hash(const InstructionsPtr instr) { - return HashBytes(reinterpret_cast(PayloadStart(instr)), + return HashBytes(reinterpret_cast(PayloadStart(instr)), Size(instr)); } diff --git a/runtime/vm/perfetto_utils.h b/runtime/vm/perfetto_utils.h index c78c450f14c..9bc30e9655a 100644 --- a/runtime/vm/perfetto_utils.h +++ b/runtime/vm/perfetto_utils.h @@ -162,10 +162,7 @@ struct Span { return memcmp(data, other.data, length * sizeof(T)) == 0; } - uword Hash() const { - return HashBytes(reinterpret_cast(data), - length * sizeof(T)); - } + uword Hash() const { return HashBytes(data, length * sizeof(T)); } }; template @@ -197,7 +194,7 @@ struct Interned { if constexpr (DefinesHashAndEquality) { return data.Hash(); } else { - return HashBytes(reinterpret_cast(&data), sizeof(T)); + return HashBytes(&data, sizeof(T)); } }