From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id CEAE12DC5C for ; Fri, 26 Apr 2019 16:19:25 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id uPN69c1-6Qpa for ; Fri, 26 Apr 2019 16:19:25 -0400 (EDT) Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 16AEF2C6B4 for ; Fri, 26 Apr 2019 16:19:25 -0400 (EDT) From: Vladislav Shpilevoy Subject: [tarantool-patches] [PATCH 1/1] core: make tt_static buffers a singleton array Date: Fri, 26 Apr 2019 23:19:23 +0300 Message-Id: <0739fcb1c2659e40eadc2ca9ad03c1d19954626f.1556309762.git.v.shpilevoy@tarantool.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: tarantool-patches@freelists.org Cc: kostja@tarantool.org tt_static_buf() is a function rotating 4 thread local buffers declared inside this function as 'static' variables. They are used very often for a plenty of purposes from string formatting to small MessagePack encoding/decoding. This array of 4 buffers was supposed to be a singleton object, but function tt_static_buf() did not belong to a one certain linking item - it was 'static inline' function in a header 'trivia/util.h'. It means that *each* linking item (each .c file using these buffers) included its own version of this function with its own *copy* of these 4 buffers. Totally there were nearly 50 copies of 4Kb buffer in various files, according to the difference seen in 'vmmap' program on Mac (like /proc/pid/maps on Linux). This patch frees 200Kb of BSS section. --- Branch: https://github.com/tarantool/tarantool/tree/gerold103/make-static-bufs-real-singleton src/lib/core/util.c | 3 +++ src/trivia/util.h | 16 ++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/lib/core/util.c b/src/lib/core/util.c index 9458695b9..8c7e74fb9 100644 --- a/src/lib/core/util.c +++ b/src/lib/core/util.c @@ -45,6 +45,9 @@ #include "say.h" +__thread char tt_static_buf_storage[TT_STATIC_BUF_COUNT][TT_STATIC_BUF_LEN]; +__thread int tt_static_buf_i = TT_STATIC_BUF_COUNT - 1; + /** Find a string in an array of strings. * * @param haystack Array of strings. Either NULL diff --git a/src/trivia/util.h b/src/trivia/util.h index a3e84e6d5..d17badff3 100644 --- a/src/trivia/util.h +++ b/src/trivia/util.h @@ -479,8 +479,16 @@ int clock_gettime(uint32_t clock_id, struct timespec *tp); #define CLOCK_THREAD_CPUTIME_ID 3 #endif +enum { + TT_STATIC_BUF_COUNT = 4, +}; +/** Can't be enum - used by preprocessor for static assertions. */ #define TT_STATIC_BUF_LEN 1028 +extern __thread char tt_static_buf_storage[TT_STATIC_BUF_COUNT] + [TT_STATIC_BUF_LEN]; +extern __thread int tt_static_buf_i; + /** * Return a thread-local statically allocated temporary buffer of size * \a TT_STATIC_BUF_LEN @@ -488,12 +496,8 @@ int clock_gettime(uint32_t clock_id, struct timespec *tp); static inline char * tt_static_buf(void) { - enum { TT_STATIC_BUFS = 4 }; - static __thread char bufs[TT_STATIC_BUFS][TT_STATIC_BUF_LEN]; - static __thread int bufno = TT_STATIC_BUFS - 1; - - bufno = (bufno + 1) % TT_STATIC_BUFS; - return bufs[bufno]; + tt_static_buf_i = (tt_static_buf_i + 1) % TT_STATIC_BUF_COUNT; + return tt_static_buf_storage[tt_static_buf_i]; } /** -- 2.20.1 (Apple Git-117)