From 2e28c9d79325b57c7bf41abd31804d3960b4c188 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 20 Jan 2018 15:12:44 +0000 Subject: Cache type size/align; Improve speed of ir_print.cpp --- src/types.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src/types.cpp') diff --git a/src/types.cpp b/src/types.cpp index 204f70d29..f089351e4 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -208,6 +208,10 @@ struct Type { TYPE_KINDS #undef TYPE_KIND }; + + // NOTE(bill): These need to be at the end to not affect the unionized data + i64 cached_size; + i64 cached_align; bool failure; }; @@ -468,6 +472,8 @@ Type *alloc_type(gbAllocator a, TypeKind kind) { Type *t = gb_alloc_item(a, Type); gb_zero_item(t); t->kind = kind; + t->cached_size = -1; + t->cached_align = -1; return t; } @@ -1792,24 +1798,31 @@ i64 type_size_of(gbAllocator allocator, Type *t) { if (t == nullptr) { return 0; } - i64 size; + // NOTE(bill): Always calculate the size when it is a Type_Basic + if (t->kind != Type_Basic && t->cached_size >= 0) { + return t->cached_size; + } TypePath path = {0}; type_path_init(&path); - size = type_size_of_internal(allocator, t, &path); + t->cached_size = type_size_of_internal(allocator, t, &path); type_path_free(&path); - return size; + return t->cached_size; } i64 type_align_of(gbAllocator allocator, Type *t) { if (t == nullptr) { return 1; } - i64 align; + // NOTE(bill): Always calculate the size when it is a Type_Basic + if (t->kind != Type_Basic && t->cached_align > 0) { + return t->cached_align; + } + TypePath path = {0}; type_path_init(&path); - align = type_align_of_internal(allocator, t, &path); + t->cached_align = type_align_of_internal(allocator, t, &path); type_path_free(&path); - return align; + return t->cached_align; } -- cgit v1.2.3