[icinga-checkins] icinga.org: icinga-core/rbartels/cgi: core: try the most common macros ( $ARGn$, $USERn$) first (Andreas Ericsson) #2674
git at icinga.org
git at icinga.org
Thu Aug 9 18:04:46 CEST 2012
Module: icinga-core
Branch: rbartels/cgi
Commit: 01a7df3e446f2f4b8694b0cb6ee08b732b53e300
URL: https://git.icinga.org/?p=icinga-core.git;a=commit;h=01a7df3e446f2f4b8694b0cb6ee08b732b53e300
Author: Michael Friedrich <michael.friedrich at univie.ac.at>
Date: Fri Jul 6 12:46:03 2012 +0200
core: try the most common macros ($ARGn$, $USERn$) first (Andreas Ericsson) #2674
the arg and user macros are most commonly used, as well as do not
require a copy of the original buffer, so this can be easily moved into
the first macro grabbing section, returning early once the macro has
been grabbed, increasing performance a bit.
kudos to Andreas Ericsson for the finding and patching.
refs #2674
---
Changelog | 1 +
common/macros.c | 69 +++++++++++++++++++++++++++++-------------------------
2 files changed, 38 insertions(+), 32 deletions(-)
diff --git a/Changelog b/Changelog
index e54f373..a494a6d 100644
--- a/Changelog
+++ b/Changelog
@@ -8,6 +8,7 @@ Thanks to all contributers, testers and developers. Please read AUTHORS and THAN
ENHANCEMENTS
* core/classic ui: make hashfunc more efficient by using sdbm #2761
+* core: try the most common macros ($ARGn$, $USERn$) first (Andreas Ericsson) #2674
FIXES
diff --git a/common/macros.c b/common/macros.c
index 9cbd11f..3296d04 100644
--- a/common/macros.c
+++ b/common/macros.c
@@ -448,6 +448,8 @@ int grab_macro_value_r(icinga_macros *mac, char *macro_buffer, char **output, in
int delimiter_len = 0;
register int x;
int result = OK;
+ /* for the early cases, this is the default */
+ *free_macro = FALSE;
if (output == NULL)
return ERROR;
@@ -458,6 +460,41 @@ int grab_macro_value_r(icinga_macros *mac, char *macro_buffer, char **output, in
if (macro_buffer == NULL || clean_options == NULL || free_macro == NULL)
return ERROR;
+ /*
+ * Handle $ARGn$ and $USERn$ macros first, since those are the most
+ * common accessed ones per check. Since none of them requires to be
+ * copied from the original buffer, we can return early as well
+ */
+ /***** ARGV MACROS *****/
+ else if (strstr(macro_buffer, "ARG") == macro_buffer) {
+
+ /* which arg do we want? */
+ x = atoi(macro_buffer + 3);
+
+ if (x <= 0 || x > MAX_COMMAND_ARGUMENTS) {
+ return ERROR;
+ }
+
+ /* use a pre-computed macro value */
+ *output = mac->argv[x-1];
+ return OK;
+ }
+
+ /***** USER MACROS *****/
+ else if (strstr(macro_buffer, "USER") == macro_buffer) {
+
+ /* which macro do we want? */
+ x = atoi(macro_buffer + 4);
+
+ if (x <= 0 || x > MAX_USER_MACROS) {
+ return ERROR;
+ }
+
+ /* use a pre-computed macro value */
+ *output = macro_user[x-1];
+ return OK;
+ }
+
/* work with a copy of the original buffer */
if ((buf = (char *)strdup(macro_buffer)) == NULL)
return ERROR;
@@ -525,38 +562,6 @@ int grab_macro_value_r(icinga_macros *mac, char *macro_buffer, char **output, in
if (x < MACRO_X_COUNT)
x = x;
- /***** ARGV MACROS *****/
- else if (strstr(macro_name, "ARG") == macro_name) {
-
- /* which arg do we want? */
- x = atoi(macro_name + 3);
-
- if (x <= 0 || x > MAX_COMMAND_ARGUMENTS) {
- my_free(buf);
- return ERROR;
- }
-
- /* use a pre-computed macro value */
- *output = mac->argv[x-1];
- *free_macro = FALSE;
- }
-
- /***** USER MACROS *****/
- else if (strstr(macro_name, "USER") == macro_name) {
-
- /* which macro do we want? */
- x = atoi(macro_name + 4);
-
- if (x <= 0 || x > MAX_USER_MACROS) {
- my_free(buf);
- return ERROR;
- }
-
- /* use a pre-computed macro value */
- *output = macro_user[x-1];
- *free_macro = FALSE;
- }
-
/***** CONTACT ADDRESS MACROS *****/
/* NOTE: the code below should be broken out into a separate function */
else if (strstr(macro_name, "CONTACTADDRESS") == macro_name) {
More information about the icinga-checkins
mailing list