aboutsummaryrefslogtreecommitdiff
path: root/stdlib/structs.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-12-22 15:53:26 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-12-22 15:53:26 -0500
commit3dd7854076c5f49032ad520dc56769faa1c35d08 (patch)
tree9edc71f9f37b0a667ab0ac5586d34c0089744f21 /stdlib/structs.c
parentcdda494fe53374dc513b04ada2dc0fad6a8b83bc (diff)
Add optimized metamethods for enums and structs that hold packed data
Diffstat (limited to 'stdlib/structs.c')
-rw-r--r--stdlib/structs.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/stdlib/structs.c b/stdlib/structs.c
index 010b49f7..1ff5a918 100644
--- a/stdlib/structs.c
+++ b/stdlib/structs.c
@@ -53,6 +53,14 @@ PUREFUNC public uint64_t Struct$hash(const void *obj, const TypeInfo_t *type)
}
#pragma GCC diagnostic pop
+PUREFUNC public uint64_t PackedData$hash(const void *obj, const TypeInfo_t *type)
+{
+ if (type->StructInfo.num_fields == 0)
+ return 0;
+
+ return siphash24(obj, (size_t)type->size);
+}
+
PUREFUNC public int32_t Struct$compare(const void *x, const void *y, const TypeInfo_t *type)
{
if (x == y)
@@ -122,6 +130,12 @@ PUREFUNC public bool Struct$equal(const void *x, const void *y, const TypeInfo_t
return true;
}
+PUREFUNC public bool PackedData$equal(const void *x, const void *y, const TypeInfo_t *type)
+{
+ if (x == y) return true;
+ return (memcmp(x, y, (size_t)type->size) == 0);
+}
+
PUREFUNC public Text_t Struct$as_text(const void *obj, bool colorize, const TypeInfo_t *type)
{
if (!obj) return Text$from_str(type->StructInfo.name);