diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2013-08-04 12:42:29 +0200 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2013-08-04 12:42:29 +0200 |
commit | 390443112c6432543d63b77d6ad1d8028653f818 (patch) | |
tree | 8b42ebe02a4575037d4719476f45f06a43e7ff56 /src | |
parent | 2dff60d7f832fbb65cd6390a1ef69e8d36a58ce9 (diff) | |
download | weechat-390443112c6432543d63b77d6ad1d8028653f818.zip |
core: optimize creation of hashtable "pointers" in eval_expression if argument is NULL
The hashtable is created on first call to eval_expression with pointers == NULL.
On next calls, the hashtable is cleared, then used again.
The hashtable is freed on exit.
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-eval.c | 69 | ||||
-rw-r--r-- | src/core/wee-eval.h | 1 | ||||
-rw-r--r-- | src/core/weechat.c | 2 |
3 files changed, 39 insertions, 33 deletions
diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c index 0097103e8..fa06bd2a9 100644 --- a/src/core/wee-eval.c +++ b/src/core/wee-eval.c @@ -45,6 +45,8 @@ char *logical_ops[EVAL_NUM_LOGICAL_OPS] = { "||", "&&" }; char *comparisons[EVAL_NUM_COMPARISONS] = { "==", "!=", "<=", "<", ">=", ">", "=~", "!~" }; +struct t_hashtable *eval_hashtable_pointers = NULL; + /* * Checks if a value is true: a value is true if string is non-NULL, non-empty @@ -242,9 +244,12 @@ eval_replace_vars_cb (void *data, const char *text) extra_vars = (struct t_hashtable *)(((void **)data)[1]); /* 1. look for var in hashtable "extra_vars" */ - ptr_value = hashtable_get (extra_vars, text); - if (ptr_value) - return strdup (ptr_value); + if (extra_vars) + { + ptr_value = hashtable_get (extra_vars, text); + if (ptr_value) + return strdup (ptr_value); + } /* 2. look for a color */ if (strncmp (text, "color:", 6) == 0) @@ -723,7 +728,7 @@ char * eval_expression (const char *expr, struct t_hashtable *pointers, struct t_hashtable *extra_vars, struct t_hashtable *options) { - int condition, pointers_created, extra_vars_created, rc; + int condition, rc; char *value; const char *prefix, *suffix, *default_prefix = "${", *default_suffix = "}"; const char *ptr_value; @@ -732,9 +737,6 @@ eval_expression (const char *expr, struct t_hashtable *pointers, if (!expr) return NULL; - pointers_created = 0; - extra_vars_created = 0; - condition = 0; prefix = default_prefix; suffix = default_suffix; @@ -742,14 +744,19 @@ eval_expression (const char *expr, struct t_hashtable *pointers, /* create hashtable pointers if it's NULL */ if (!pointers) { - pointers = hashtable_new (32, - WEECHAT_HASHTABLE_STRING, - WEECHAT_HASHTABLE_POINTER, - NULL, - NULL); - if (!pointers) - return NULL; - pointers_created = 1; + if (eval_hashtable_pointers) + hashtable_remove_all (eval_hashtable_pointers); + else + { + eval_hashtable_pointers = hashtable_new (32, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_POINTER, + NULL, + NULL); + if (!eval_hashtable_pointers) + return NULL; + } + pointers = eval_hashtable_pointers; } /* @@ -768,19 +775,6 @@ eval_expression (const char *expr, struct t_hashtable *pointers, } } - /* create hashtable extra_vars if it's NULL */ - if (!extra_vars) - { - extra_vars = hashtable_new (32, - WEECHAT_HASHTABLE_STRING, - WEECHAT_HASHTABLE_STRING, - NULL, - NULL); - if (!extra_vars) - return NULL; - extra_vars_created = 1; - } - /* read options */ if (options) { @@ -817,10 +811,19 @@ eval_expression (const char *expr, struct t_hashtable *pointers, value = eval_replace_vars (expr, pointers, extra_vars, prefix, suffix); } - if (pointers_created) - hashtable_free (pointers); - if (extra_vars_created) - hashtable_free (extra_vars); - return value; } + +/* + * Frees all allocated data. + */ + +void +eval_end () +{ + if (eval_hashtable_pointers) + { + hashtable_free (eval_hashtable_pointers); + eval_hashtable_pointers = NULL; + } +} diff --git a/src/core/wee-eval.h b/src/core/wee-eval.h index a570b57e6..957954bbe 100644 --- a/src/core/wee-eval.h +++ b/src/core/wee-eval.h @@ -52,5 +52,6 @@ extern char *eval_expression (const char *expr, struct t_hashtable *pointers, struct t_hashtable *extra_vars, struct t_hashtable *options); +extern void eval_end (); #endif /* __WEECHAT_EVAL_H */ diff --git a/src/core/weechat.c b/src/core/weechat.c index 750493b95..218028075 100644 --- a/src/core/weechat.c +++ b/src/core/weechat.c @@ -56,6 +56,7 @@ #include "wee-completion.h" #include "wee-config.h" #include "wee-debug.h" +#include "wee-eval.h" #include "wee-hdata.h" #include "wee-hook.h" #include "wee-log.h" @@ -486,6 +487,7 @@ main (int argc, char *argv[]) gui_key_end (); /* remove all keys */ unhook_all (); /* remove all hooks */ hdata_end (); /* end hdata */ + eval_end (); /* end eval */ weechat_shutdown (EXIT_SUCCESS, 0); /* quit WeeChat (oh no, why?) */ return EXIT_SUCCESS; /* make C compiler happy */ |