[array] Speed up hash() for byte arrays

This commit is contained in:
Behdad Esfahbod 2022-11-21 23:19:42 -07:00
parent d7b492e3f5
commit bca569ae53
1 changed files with 28 additions and 4 deletions

View File

@ -424,17 +424,41 @@ inline bool hb_array_t<const unsigned char>::operator == (const hb_array_t<const
return 0 == hb_memcmp (arrayZ, o.arrayZ, length);
}
/* Specialize hash() for byte arrays. */
template <>
inline uint32_t hb_array_t<const char>::hash () const {
inline uint32_t hb_array_t<const char>::hash () const
{
uint32_t current = 0;
for (unsigned int i = 0; i < this->length; i++)
unsigned i = 0;
#if defined(__OPTIMIZE__) && !defined(HB_NO_PACKED) && \
((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__))
struct __attribute__((packed)) packed_uint32_t { uint32_t v; };
for (; i + 4 <= this->length; i += 4)
current = current * 31 + (uint32_t) ((((packed_uint32_t *) &this->arrayZ[i])->v) * 2654435761u);
#endif
for (; i < this->length; i++)
current = current * 31 + (uint32_t) (this->arrayZ[i] * 2654435761u);
return current;
}
template <>
inline uint32_t hb_array_t<const unsigned char>::hash () const {
inline uint32_t hb_array_t<const unsigned char>::hash () const
{
uint32_t current = 0;
for (unsigned int i = 0; i < this->length; i++)
unsigned i = 0;
#if defined(__OPTIMIZE__) && !defined(HB_NO_PACKED) && \
((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__))
struct __attribute__((packed)) packed_uint32_t { uint32_t v; };
for (; i + 4 <= this->length; i += 4)
current = current * 31 + (uint32_t) ((((packed_uint32_t *) &this->arrayZ[i])->v) * 2654435761u);
#endif
for (; i < this->length; i++)
current = current * 31 + (uint32_t) (this->arrayZ[i] * 2654435761u);
return current;
}