From 383f9bab4163ec36588a3009b2587e2da66d02f4 Mon Sep 17 00:00:00 2001 From: frapank Date: Sat, 4 Jul 2026 03:05:49 +0200 Subject: [PATCH] perf: inline dstrlen/dstrca --- dstr.c | 86 ---------------------------------------------------------- dstr.h | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 88 deletions(-) diff --git a/dstr.c b/dstr.c index 4c6fa0b..bc29b81 100644 --- a/dstr.c +++ b/dstr.c @@ -11,46 +11,6 @@ #include "dstr_options.h" -enum dstrhd_type { - DSTRHD_TYPE_8 = 0, - DSTRHD_TYPE_16 = 1, - DSTRHD_TYPE_32 = 2, - DSTRHD_TYPE_64 = 3 -}; - -struct __attribute__((packed)) dstrhd8 { - uint8_t len; - uint8_t cap; // 255 - uint8_t type; - char buf[]; -}; - -struct __attribute__((packed)) dstrhd16 { - uint16_t len; - uint16_t cap; // 65,535 - uint8_t type; - char buf[]; -}; - -struct __attribute__((packed)) dstrhd32 { - uint32_t len; - uint32_t cap; // 4,294,967,295 - uint8_t type; - char buf[]; -}; - -struct __attribute__((packed)) dstrhd64 { - uint64_t len; - uint64_t cap; // 18,446,744,073,709,551,615 - uint8_t type; - char buf[]; -}; - -// Utils -#define DSTRGETHDR(n, s) \ - ((struct dstrhd##n*)((char*)(s) - sizeof(struct dstrhd##n))) -#define DSTRGETTYPE(s) ((uint8_t)((s)[-1])) - static inline enum dstrhd_type _dstr_type_by_size(size_t cap) { if (cap <= UINT8_MAX) @@ -321,52 +281,6 @@ void dstrsplitfree(dstr* parts, size_t count) DSTR_FREE(parts); } -// strlen -size_t dstrlen(dstr s) -{ - if (!s) - return 0; - enum dstrhd_type t = DSTRGETTYPE(s); - switch (t) { - case DSTRHD_TYPE_8: { - return DSTRGETHDR(8, s)->len; - } - case DSTRHD_TYPE_16: { - return DSTRGETHDR(16, s)->len; - } - case DSTRHD_TYPE_32: { - return DSTRGETHDR(32, s)->len; - } - case DSTRHD_TYPE_64: { - return DSTRGETHDR(64, s)->len; - } - } - return 0; -}; - -// dstrcap -size_t dstrcap(dstr s) -{ - if (!s) - return 0; - enum dstrhd_type t = DSTRGETTYPE(s); - switch (t) { - case DSTRHD_TYPE_8: { - return DSTRGETHDR(8, s)->cap; - } - case DSTRHD_TYPE_16: { - return DSTRGETHDR(16, s)->cap; - } - case DSTRHD_TYPE_32: { - return DSTRGETHDR(32, s)->cap; - } - case DSTRHD_TYPE_64: { - return DSTRGETHDR(64, s)->cap; - } - } - return 0; -} - // strdup dstr dstrdup(dstr s) { diff --git a/dstr.h b/dstr.h index 41f612d..d3f3eb4 100644 --- a/dstr.h +++ b/dstr.h @@ -20,6 +20,7 @@ #include #include +#include #ifdef _WIN32 #include @@ -50,6 +51,45 @@ typedef ptrdiff_t ssize_t; typedef char* dstr; +enum dstrhd_type { + DSTRHD_TYPE_8 = 0, + DSTRHD_TYPE_16 = 1, + DSTRHD_TYPE_32 = 2, + DSTRHD_TYPE_64 = 3 +}; + +struct __attribute__((packed)) dstrhd8 { + uint8_t len; + uint8_t cap; + uint8_t type; + char buf[]; +}; + +struct __attribute__((packed)) dstrhd16 { + uint16_t len; + uint16_t cap; + uint8_t type; + char buf[]; +}; + +struct __attribute__((packed)) dstrhd32 { + uint32_t len; + uint32_t cap; + uint8_t type; + char buf[]; +}; + +struct __attribute__((packed)) dstrhd64 { + uint64_t len; + uint64_t cap; + uint8_t type; + char buf[]; +}; + +#define DSTRGETHDR(n, s) \ + ((struct dstrhd##n*)((char*)(s) - sizeof(struct dstrhd##n))) +#define DSTRGETTYPE(s) ((uint8_t)((s)[-1])) + /* * dstrnew(msg) * dstrnew(msg, cap) @@ -122,8 +162,41 @@ void dstrsplitfree(dstr* parts, size_t count); * dstrlen(s) - number of characters in s, excluding the null terminator. * dstrcap(s) - total allocated capacity in bytes. */ -size_t dstrlen(dstr s) W_UNUSED_RESULT; -size_t dstrcap(dstr s) W_UNUSED_RESULT; +static inline size_t dstrlen(dstr s) W_UNUSED_RESULT; +static inline size_t dstrlen(dstr s) +{ + if (!s) + return 0; + switch ((enum dstrhd_type)DSTRGETTYPE(s)) { + case DSTRHD_TYPE_8: + return DSTRGETHDR(8, s)->len; + case DSTRHD_TYPE_16: + return DSTRGETHDR(16, s)->len; + case DSTRHD_TYPE_32: + return DSTRGETHDR(32, s)->len; + case DSTRHD_TYPE_64: + return DSTRGETHDR(64, s)->len; + } + return 0; +} + +static inline size_t dstrcap(dstr s) W_UNUSED_RESULT; +static inline size_t dstrcap(dstr s) +{ + if (!s) + return 0; + switch ((enum dstrhd_type)DSTRGETTYPE(s)) { + case DSTRHD_TYPE_8: + return DSTRGETHDR(8, s)->cap; + case DSTRHD_TYPE_16: + return DSTRGETHDR(16, s)->cap; + case DSTRHD_TYPE_32: + return DSTRGETHDR(32, s)->cap; + case DSTRHD_TYPE_64: + return DSTRGETHDR(64, s)->cap; + } + return 0; +} /* * dstrclear(s)