From b091f84f905d1ef98a7b5752f698d7f3d43407fb Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Mon, 18 Feb 2019 10:48:03 +0100 Subject: [PATCH] Update code after benching empty creation of string. --- m-string.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/m-string.h b/m-string.h index 4006d572..11c5c2f1 100644 --- a/m-string.h +++ b/m-string.h @@ -94,11 +94,15 @@ static inline void string_clear(string_t v) { STRINGI_CONTRACT(v); - M_MEMORY_FREE(v->ptr); - /* This is not needed but is safer. size is not set to make - the string more or less invalid so that it can be detected. */ - v->alloc = 0; - v->ptr = NULL; + if (M_LIKELY(v->ptr)) { + /* Even free supports NULL argument, bench shows there is quite a high + cost to call free. This is an impact for empty string. */ + M_MEMORY_FREE(v->ptr); + /* This is not needed but is safer. size is not set to make + the string more or less invalid so that it can be detected. */ + v->alloc = 0; + v->ptr = NULL; + } } /* NOTE: Internaly used by STRING_DECL_INIT */