From 839c1983a0dad0232da3ceda9c7c02a49715453c Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 8 Oct 2024 21:10:36 -0400 Subject: Reduce padding needed for optional types and clean up some redundant type padding --- stdlib/arrays.c | 4 ++-- stdlib/optionals.c | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) (limited to 'stdlib') diff --git a/stdlib/arrays.c b/stdlib/arrays.c index 274983b7..93676979 100644 --- a/stdlib/arrays.c +++ b/stdlib/arrays.c @@ -20,7 +20,7 @@ PUREFUNC static inline int64_t get_padded_item_size(const TypeInfo_t *info) { int64_t size = info->ArrayInfo.item->size; if (info->ArrayInfo.item->align > 1 && size % info->ArrayInfo.item->align) - size += info->ArrayInfo.item->align - (size % info->ArrayInfo.item->align); // padding + errx(1, "Item size is not padded!"); return size; } @@ -524,7 +524,7 @@ public int32_t Array$compare(const Array_t *x, const Array_t *y, const TypeInfo_ if (item->tag == PointerInfo || (item->tag == CustomInfo && item->CustomInfo.compare == NULL)) { // data comparison int64_t item_padded_size = type->ArrayInfo.item->size; if (type->ArrayInfo.item->align > 1 && item_padded_size % type->ArrayInfo.item->align) - item_padded_size += type->ArrayInfo.item->align - (item_padded_size % type->ArrayInfo.item->align); // padding + errx(1, "Item size is not padded!"); if ((int64_t)x->stride == item_padded_size && (int64_t)y->stride == item_padded_size && item->size == item_padded_size) { int32_t cmp = (int32_t)memcmp(x->data, y->data, (size_t)(MIN(x->length, y->length)*item_padded_size)); diff --git a/stdlib/optionals.c b/stdlib/optionals.c index 37d1b1df..a3b3defc 100644 --- a/stdlib/optionals.c +++ b/stdlib/optionals.c @@ -43,9 +43,13 @@ public PUREFUNC bool is_null(const void *obj, const TypeInfo_t *non_optional_typ case TableInfo: return ((Table_t*)obj)->entries.length < 0; case FunctionInfo: return *(void**)obj == NULL; case StructInfo: { - int64_t offset = non_optional_type->size; - if (offset % non_optional_type->align) - offset += non_optional_type->align - (offset % non_optional_type->align); + int64_t offset = 0; + for (int i = 0; i < non_optional_type->StructInfo.num_fields; i++) { + NamedType_t field = non_optional_type->StructInfo.fields[i]; + if (offset > 0 && (offset % field.type->align) > 0) + offset += field.type->align - (offset % field.type->align); + offset += field.type->size; + } return *(bool*)(obj + offset); } case EnumInfo: return (*(int*)obj) == 0; // NULL tag -- cgit v1.2.3