Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org
Cc: kostja@tarantool.org
Subject: [tarantool-patches] [PATCH 1/1] core: make tt_static buffers a singleton array
Date: Fri, 26 Apr 2019 23:19:23 +0300	[thread overview]
Message-ID: <0739fcb1c2659e40eadc2ca9ad03c1d19954626f.1556309762.git.v.shpilevoy@tarantool.org> (raw)

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)

             reply	other threads:[~2019-04-26 20:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-26 20:19 Vladislav Shpilevoy [this message]
2019-04-27 21:11 ` [tarantool-patches] " Konstantin Osipov
2019-04-28 16:56   ` Vladislav Shpilevoy
2019-04-28 18:31     ` Konstantin Osipov
2019-04-28 19:05       ` Vladislav Shpilevoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0739fcb1c2659e40eadc2ca9ad03c1d19954626f.1556309762.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH 1/1] core: make tt_static buffers a singleton array' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox