Fix horrendously bad bit hashing function.

hashBits() is used to generate a hash code from the same 64 bits used
to represent a Wren number as a double. When building a map containing
a large number of integer keys, it's important for this to do a good
job scattering the bits across the 32-bit key space.

Alas, it does not. Worse, the benchmark to test this happens to stop
just before the performance falls off a cliff, so this was easy to
overlook.

This replaces it with the hash function V8 uses, which has much better
performance across the numeric range.
This commit is contained in:
Robert Nystrom
2019-07-27 13:34:07 -07:00
parent d1a0d0682a
commit 2a1499b04b
7 changed files with 29 additions and 37 deletions
+3 -3
View File
@@ -6,17 +6,17 @@ main() {
var map = {};
for (var i = 1; i <= 1000000; i++) {
for (var i = 1; i <= 2000000; i++) {
map[i] = i;
}
var sum = 0;
for (var i = 1; i <= 1000000; i++) {
for (var i = 1; i <= 2000000; i++) {
sum += map[i];
}
print(sum);
for (var i = 1; i <= 1000000; i++) {
for (var i = 1; i <= 2000000; i++) {
map.remove(i);
}
+3 -3
View File
@@ -2,17 +2,17 @@ local start = os.clock()
local map = {}
for i = 1, 1000000 do
for i = 1, 2000000 do
map[i] = i
end
local sum = 0
for i = 1, 1000000 do
for i = 1, 2000000 do
sum = sum + map[i]
end
io.write(string.format("%d\n", sum))
for i = 1, 1000000 do
for i = 1, 2000000 do
map[i] = nil
end
+3 -3
View File
@@ -6,15 +6,15 @@ start = time.clock()
map = {}
for i in range(1, 1000001):
for i in range(1, 2000001):
map[i] = i
sum = 0
for i in range(1, 1000001):
for i in range(1, 2000001):
sum = sum + map[i]
print(sum)
for i in range(1, 1000001):
for i in range(1, 2000001):
del map[i]
print("elapsed: " + str(time.clock() - start))
+3 -3
View File
@@ -2,17 +2,17 @@ start = Time.now
map = Hash.new
for i in (1..1000000)
for i in (1..2000000)
map[i] = i
end
sum = 0
for i in (1..1000000)
for i in (1..2000000)
sum = sum + map[i]
end
puts sum
for i in (1..1000000)
for i in (1..2000000)
map.delete(i)
end
+3 -3
View File
@@ -2,17 +2,17 @@ var start = System.clock
var map = {}
for (i in 1..1000000) {
for (i in 1..2000000) {
map[i] = i
}
var sum = 0
for (i in 1..1000000) {
for (i in 1..2000000) {
sum = sum + map[i]
}
System.print(sum)
for (i in 1..1000000) {
for (i in 1..2000000) {
map.remove(i)
}