JS optimisations pt.1

This commit is contained in:
wuggy 2026-09-18 13:11:49 -07:00
commit 6cc90c7009
31 changed files with 867 additions and 107 deletions

View file

@ -20,7 +20,17 @@ HashBytes(const void* aBytes, size_t aLength)
/* Walk word by word. */
size_t i = 0;
for (; i < aLength - (aLength % sizeof(size_t)); i += sizeof(size_t)) {
const size_t wordLength = aLength - (aLength % sizeof(size_t));
const size_t doubleWordLength = wordLength - (wordLength % (2 * sizeof(size_t)));
for (; i < doubleWordLength; i += 2 * sizeof(size_t)) {
size_t data0;
size_t data1;
memcpy(&data0, b + i, sizeof(data0));
memcpy(&data1, b + i + sizeof(data0), sizeof(data1));
hash = AddToHash(hash, data0, sizeof(data0));
hash = AddToHash(hash, data1, sizeof(data1));
}
for (; i < wordLength; i += sizeof(size_t)) {
/* Do an explicitly unaligned load of the data. */
size_t data;
memcpy(&data, b + i, sizeof(size_t));

View file

@ -230,8 +230,17 @@ uint32_t
HashKnownLength(const T* aStr, size_t aLength)
{
uint32_t hash = 0;
for (size_t i = 0; i < aLength; i++) {
hash = AddToHash(hash, aStr[i]);
while (aLength >= 4) {
hash = AddToHash(hash, aStr[0]);
hash = AddToHash(hash, aStr[1]);
hash = AddToHash(hash, aStr[2]);
hash = AddToHash(hash, aStr[3]);
aStr += 4;
aLength -= 4;
}
while (aLength) {
hash = AddToHash(hash, *aStr++);
--aLength;
}
return hash;
}