trace: improve performance while category is disabled
Move just enough code from trace.c into trace.h header so all code necessary to determine that trace is disabled could be inlined to calling functions. Then perform the check if the trace key is enabled sooner in call chain. Signed-off-by: Gennady Kupava <gkupava@bloomberg.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Gennady Kupava committed
Nov 26, 2017 at 20:11 UTC
8eeb25c6795af71400537e8ab064407d7998e0e2
2 files changed
+42
-20
trace.c
+1
-2
@@ -25,6 +25,7 @@
25
#include "quote.h"
26
27
struct trace_key trace_default_key = { "GIT_TRACE", 0, 0, 0 };
28
+struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
29
30
/* Get a trace file descriptor from "key" env variable. */
31
static int get_trace_fd(struct trace_key *key)
@@ -172,8 +173,6 @@ void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
173
print_trace_line(key, &buf);
174
}
175
175
-static struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
176
-
176
static void trace_performance_vprintf_fl(const char *file, int line,
177
uint64_t nanos, const char *format,
178
va_list ap)
trace.h
+41
-18
@@ -14,6 +14,7 @@ struct trace_key {
14
extern struct trace_key trace_default_key;
15
16
#define TRACE_KEY_INIT(name) { "GIT_TRACE_" #name, 0, 0, 0 }
17
+extern struct trace_key trace_perf_key;
18
19
extern void trace_repo_setup(const char *prefix);
20
extern int trace_want(struct trace_key *key);
@@ -79,24 +80,42 @@ extern void trace_performance_since(uint64_t start, const char *format, ...);
80
* comma, but this is non-standard.
81
*/
82
82
-#define trace_printf(...) \
83
- trace_printf_key_fl(TRACE_CONTEXT, __LINE__, &trace_default_key, __VA_ARGS__)
84
-
85
-#define trace_printf_key(key, ...) \
86
- trace_printf_key_fl(TRACE_CONTEXT, __LINE__, key, __VA_ARGS__)
87
-
88
-#define trace_argv_printf(argv, ...) \
89
- trace_argv_printf_fl(TRACE_CONTEXT, __LINE__, argv, __VA_ARGS__)
90
-
91
-#define trace_strbuf(key, data) \
92
- trace_strbuf_fl(TRACE_CONTEXT, __LINE__, key, data)
93
-
94
-#define trace_performance(nanos, ...) \
95
- trace_performance_fl(TRACE_CONTEXT, __LINE__, nanos, __VA_ARGS__)
96
-
97
-#define trace_performance_since(start, ...) \
98
- trace_performance_fl(TRACE_CONTEXT, __LINE__, getnanotime() - (start), \
99
- __VA_ARGS__)
83
+#define trace_printf_key(key, ...) \
84
+ do { \
85
+ if (trace_pass_fl(key)) \
86
+ trace_printf_key_fl(TRACE_CONTEXT, __LINE__, key, \
87
+ __VA_ARGS__); \
88
+ } while (0)
89
+
90
+#define trace_printf(...) trace_printf_key(&trace_default_key, __VA_ARGS__)
91
+
92
+#define trace_argv_printf(argv, ...) \
93
+ do { \
94
+ if (trace_pass_fl(&trace_default_key)) \
95
+ trace_argv_printf_fl(TRACE_CONTEXT, __LINE__, \
96
+ argv, __VA_ARGS__); \
97
+ } while (0)
98
+
99
+#define trace_strbuf(key, data) \
100
+ do { \
101
+ if (trace_pass_fl(key)) \
102
+ trace_strbuf_fl(TRACE_CONTEXT, __LINE__, key, data);\
103
+ } while (0)
104
+
105
+#define trace_performance(nanos, ...) \
106
+ do { \
107
+ if (trace_pass_fl(&trace_perf_key)) \
108
+ trace_performance_fl(TRACE_CONTEXT, __LINE__, nanos,\
109
+ __VA_ARGS__); \
110
+ } while (0)
111
+
112
+#define trace_performance_since(start, ...) \
113
+ do { \
114
+ if (trace_pass_fl(&trace_perf_key)) \
115
+ trace_performance_fl(TRACE_CONTEXT, __LINE__, \
116
+ getnanotime() - (start), \
117
+ __VA_ARGS__); \
118
+ } while (0)
119
120
/* backend functions, use non-*fl macros instead */
121
__attribute__((format (printf, 4, 5)))
@@ -110,6 +129,10 @@ extern void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
129
__attribute__((format (printf, 4, 5)))
130
extern void trace_performance_fl(const char *file, int line,
131
uint64_t nanos, const char *fmt, ...);
132
+static inline int trace_pass_fl(struct trace_key *key)
133
+{
134
+ return key->fd || !key->initialized;
135
+}
136
137
#endif /* HAVE_VARIADIC_MACROS */
138