Added 'Toy_String' to 'Toy_Value' structure, read more

To help with storing strings within tables, I've replaced the unused
'_padding' member of 'Toy_String' with 'cachedHash', which is set to
zero on string allocation.

The hash of a string isn't generated and stored until it's actually
needed, as the rope pattern means not every string needs a hash -
hopefully this will save unnecessarily wasted time.

When a hash of a string is needed, the hashing function first checks to
see if that string already has one, and if so, returns it. Again, less
time wasted.

When generating a new string hash, the hashing function takes the
string's type into account, as node-based strings first need their
contents assembled into a simple char buffer.

Other changes include:

* Changed 'TOY_VALUE_TO_*' to 'TOY_VALUE_FROM_*'
* Changed 'TOY_VALUE_IS_EQUAL' to 'TOY_VALUES_ARE_EQUAL'
* Added a missing '#pragma once' to 'toy_print.h'
This commit is contained in:
2024-10-07 17:20:08 +11:00
parent d62ee2a9a3
commit 14653a303f
25 changed files with 266 additions and 175 deletions

View File

@@ -18,7 +18,7 @@ static void probeAndInsert(Toy_Table** tableHandle, Toy_Value key, Toy_Value val
//probe
while (true) {
//if we're overriding an existing value
if (TOY_VALUE_IS_EQUAL((*tableHandle)->data[probe].key, key)) {
if (TOY_VALUES_ARE_EQUAL((*tableHandle)->data[probe].key, key)) {
(*tableHandle)->data[probe] = entry;
//TODO: benchmark the psl optimisation
@@ -120,13 +120,13 @@ Toy_Value Toy_lookupTable(Toy_Table** tableHandle, Toy_Value key) {
while (true) {
//found the entry
if (TOY_VALUE_IS_EQUAL((*tableHandle)->data[probe].key, key)) {
if (TOY_VALUES_ARE_EQUAL((*tableHandle)->data[probe].key, key)) {
return (*tableHandle)->data[probe].value;
}
//if its an empty slot
if (TOY_VALUE_IS_NULL((*tableHandle)->data[probe].key)) {
return TOY_VALUE_TO_NULL();
return TOY_VALUE_FROM_NULL();
}
//adjust and continue
@@ -145,7 +145,7 @@ void Toy_removeTable(Toy_Table** tableHandle, Toy_Value key) {
while (true) {
//found the entry
if (TOY_VALUE_IS_EQUAL((*tableHandle)->data[probe].key, key)) {
if (TOY_VALUES_ARE_EQUAL((*tableHandle)->data[probe].key, key)) {
break;
}
@@ -174,6 +174,6 @@ void Toy_removeTable(Toy_Table** tableHandle, Toy_Value key) {
}
//finally, wipe the removed entry
(*tableHandle)->data[wipe] = (Toy_TableEntry){ .key = TOY_VALUE_TO_NULL(), .value = TOY_VALUE_TO_NULL(), .psl = 0 };
(*tableHandle)->data[wipe] = (Toy_TableEntry){ .key = TOY_VALUE_FROM_NULL(), .value = TOY_VALUE_FROM_NULL(), .psl = 0 };
(*tableHandle)->count--;
}