feat: store precomputed hash in ObjString and use it for equality and hashing

Cache the FNV-1a hash code in the ObjString struct at creation time, replacing the
on-the-fly sampling hash in hashObject() and enabling constant-time string equality
checks. Refactor string allocation into allocateString() and hashString() helpers,
remove the old wrenNewUninitializedString() declaration, and add a CONST_STRING
macro. Disable the unimplemented subscript range operator with a runtime error and
skip the related UTF-8 tests. Add a string_equals benchmark to measure the speedup.
This commit is contained in:
Bob Nystrom
2015-03-18 14:09:03 +00:00
parent a3418dc06a
commit 1e842bbb2e
14 changed files with 89 additions and 52 deletions
+24
View File
@@ -0,0 +1,24 @@
var start = IO.clock
var count = 0
for (i in 1..1000000) {
if ("abc" == "abc") count = count + 1
if ("a slightly longer string" ==
"a slightly longer string") count = count + 1
if ("a significantly longer string but still not overwhelmingly long string" ==
"a significantly longer string but still not overwhelmingly long string") count = count + 1
if ("" == "abc") count = count + 1
if ("abc" == "abcd") count = count + 1
if ("changed one character" == "changed %ne character") count = count + 1
if ("123" == 123) count = count + 1
if ("a slightly longer string" ==
"a slightly longer string!") count = count + 1
if ("a slightly longer string" ==
"a slightly longer strinh") count = count + 1
if ("a significantly longer string but still not overwhelmingly long string" ==
"another") count = count + 1
}
IO.print(count)
IO.print("elapsed: ", IO.clock - start)
+1
View File
@@ -1,3 +1,4 @@
// skip: Range subscripts for strings don't handle UTF-8.
var string = "abcde"
IO.print(string[0..0]) // expect: a
IO.print(string[1...1] == "") // expect: true
@@ -1,2 +1,3 @@
// skip: Range subscripts for strings don't handle UTF-8.
var a = "string"
a[1.5..2] // expect runtime error: Range start must be an integer.
@@ -1,2 +1,3 @@
// skip: Range subscripts for strings don't handle UTF-8.
var a = "123"
a[3..2] // expect runtime error: Range start out of bounds.
@@ -1,2 +1,3 @@
// skip: Range subscripts for strings don't handle UTF-8.
var a = "123"
a[-4..2] // expect runtime error: Range start out of bounds.
@@ -1,2 +1,3 @@
// skip: Range subscripts for strings don't handle UTF-8.
var a = "123"
a[1...4] // expect runtime error: Range end out of bounds.
@@ -1,2 +1,3 @@
// skip: Range subscripts for strings don't handle UTF-8.
var a = "123"
a[0...-5] // expect runtime error: Range end out of bounds.
@@ -1,2 +1,3 @@
// skip: Range subscripts for strings don't handle UTF-8.
var a = "string"
a[1..2.5] // expect runtime error: Range end must be an integer.
@@ -1,2 +1,3 @@
// skip: Range subscripts for strings don't handle UTF-8.
var a = "123"
a[1..3] // expect runtime error: Range end out of bounds.
@@ -1,2 +1,3 @@
// skip: Range subscripts for strings don't handle UTF-8.
var a = "123"
a[0..-4] // expect runtime error: Range end out of bounds.