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

@@ -47,6 +47,7 @@ Toy_String* Toy_createStringLength(Toy_Bucket** bucketHandle, const char* cstrin
ret->type = TOY_STRING_LEAF;
ret->length = length;
ret->refCount = 1;
ret->cachedHash = 0; //don't calc until needed
memcpy(ret->as.leaf.data, cstring, length);
ret->as.leaf.data[length] = '\0';
@@ -61,6 +62,7 @@ TOY_API Toy_String* Toy_createNameString(Toy_Bucket** bucketHandle, const char*
ret->type = TOY_STRING_NAME;
ret->length = length;
ret->refCount = 1;
ret->cachedHash = 0; //don't calc until needed
memcpy(ret->as.name.data, cname, length);
ret->as.name.data[length] = '\0';
@@ -87,6 +89,7 @@ Toy_String* Toy_deepCopyString(Toy_Bucket** bucketHandle, Toy_String* str) {
ret->type = TOY_STRING_LEAF;
ret->length = str->length;
ret->refCount = 1;
ret->cachedHash = str->cachedHash;
deepCopyUtil(ret->as.leaf.data, str); //copy each leaf into the buffer
ret->as.leaf.data[ret->length] = '\0';
}
@@ -94,6 +97,7 @@ Toy_String* Toy_deepCopyString(Toy_Bucket** bucketHandle, Toy_String* str) {
ret->type = TOY_STRING_NAME;
ret->length = str->length;
ret->refCount = 1;
ret->cachedHash = str->cachedHash;
memcpy(ret->as.name.data, str->as.name.data, str->length);
ret->as.name.data[ret->length] = '\0';
}
@@ -117,6 +121,7 @@ Toy_String* Toy_concatStrings(Toy_Bucket** bucketHandle, Toy_String* left, Toy_S
ret->type = TOY_STRING_NODE;
ret->length = left->length + right->length;
ret->refCount = 1;
ret->cachedHash = 0; //don't calc until needed
ret->as.node.left = left;
ret->as.node.right = right;
@@ -250,4 +255,4 @@ int Toy_compareStrings(Toy_String* left, Toy_String* right) {
const char* rightHead = NULL;
return deepCompareUtil(left, right, &leftHead, &rightHead);
}
}