Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH 1/1] core: make tt_static buffers a singleton array
@ 2019-04-26 20:19 Vladislav Shpilevoy
  2019-04-27 21:11 ` [tarantool-patches] " Konstantin Osipov
  0 siblings, 1 reply; 5+ messages in thread
From: Vladislav Shpilevoy @ 2019-04-26 20:19 UTC (permalink / raw)
  To: tarantool-patches; +Cc: kostja

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)

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-04-28 19:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-26 20:19 [tarantool-patches] [PATCH 1/1] core: make tt_static buffers a singleton array Vladislav Shpilevoy
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

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