Skip to content

Commit 8eeb25c

Browse files
Gennady Kupavagitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 406102a commit 8eeb25c

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

trace.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "quote.h"
2626

2727
struct trace_key trace_default_key = { "GIT_TRACE", 0, 0, 0 };
28+
struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
2829

2930
/* Get a trace file descriptor from "key" env variable. */
3031
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,
172173
print_trace_line(key, &buf);
173174
}
174175

175-
static struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
176-
177176
static void trace_performance_vprintf_fl(const char *file, int line,
178177
uint64_t nanos, const char *format,
179178
va_list ap)

trace.h

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct trace_key {
1414
extern struct trace_key trace_default_key;
1515

1616
#define TRACE_KEY_INIT(name) { "GIT_TRACE_" #name, 0, 0, 0 }
17+
extern struct trace_key trace_perf_key;
1718

1819
extern void trace_repo_setup(const char *prefix);
1920
extern int trace_want(struct trace_key *key);
@@ -79,24 +80,42 @@ extern void trace_performance_since(uint64_t start, const char *format, ...);
7980
* comma, but this is non-standard.
8081
*/
8182

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)
100119

101120
/* backend functions, use non-*fl macros instead */
102121
__attribute__((format (printf, 4, 5)))
@@ -110,6 +129,10 @@ extern void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
110129
__attribute__((format (printf, 4, 5)))
111130
extern void trace_performance_fl(const char *file, int line,
112131
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+
}
113136

114137
#endif /* HAVE_VARIADIC_MACROS */
115138

0 commit comments

Comments
 (0)