@cryptotaxi247 / netdata-1 / commits / 6553f615b

minor optimization on alarms parsing

Costa Tsaousis (ktsaou) committed Feb 11, 2017 at 13:28 UTC 6553f615bf9a39c373b154da1f32033d08918e70
1 file changed +42 -21
src/health.c
+42 -21
@@ -1896,14 +1896,35 @@ static inline int health_parse_db_lookup(
1896 return 1;
1897 }
1898
1899 -static inline char *tabs2spaces(char *s) {
1900 - char *t = s;
1901 - while(*t) {
1902 - if(unlikely(*t == '\t')) *t = ' ';
1903 - t++;
1899 +static inline char *trim_all_spaces(char *buffer) {
1900 + char *d = buffer, *s = buffer;
1901 +
1902 + // skip spaces
1903 + while(isspace(*s)) s++;
1904 +
1905 + while(*s) {
1906 + // copy the non-space part
1907 + while(*s && !isspace(*s)) *d++ = *s++;
1908 +
1909 + // add a space if we have to
1910 + if(*s && isspace(*s)) {
1911 + *d++ = ' ';
1912 + s++;
1913 + }
1914 +
1915 + // skip spaces
1916 + while(isspace(*s)) s++;
1917 + }
1918 +
1919 + *d = '\0';
1920 +
1921 + if(d > buffer) {
1922 + d--;
1923 + if(isspace(*d)) *d = '\0';
1924 }
1925
1906 - return s;
1926 + if(!buffer[0]) return NULL;
1927 + return buffer;
1928 }
1929
1930 static inline char *health_source_file(size_t line, const char *path, const char *filename) {
@@ -2003,8 +2024,8 @@ int health_readfile(const char *path, const char *filename) {
2024 s++;
2025
2026 char *value = s;
2006 - key = trim(key);
2007 - value = trim(value);
2027 + key = trim_all_spaces(key);
2028 + value = trim_all_spaces(value);
2029
2030 if(!key) {
2031 error("Health configuration has invalid line %zu of file '%s/%s'. Keyword is empty. Ignoring it.", line, path, filename);
@@ -2030,7 +2051,7 @@ int health_readfile(const char *path, const char *filename) {
2051
2052 rc = callocz(1, sizeof(RRDCALC));
2053 rc->next_event_id = 1;
2033 - rc->name = tabs2spaces(strdupz(value));
2054 + rc->name = strdupz(value);
2055 rc->hash = simple_hash(rc->name);
2056 rc->source = health_source_file(line, path, filename);
2057 rc->green = NAN;
@@ -2053,7 +2074,7 @@ int health_readfile(const char *path, const char *filename) {
2074 rrdcalctemplate_free(&localhost, rt);
2075
2076 rt = callocz(1, sizeof(RRDCALCTEMPLATE));
2056 - rt->name = tabs2spaces(strdupz(value));
2077 + rt->name = strdupz(value);
2078 rt->hash_name = simple_hash(rt->name);
2079 rt->source = health_source_file(line, path, filename);
2080 rt->green = NAN;
@@ -2072,7 +2093,7 @@ int health_readfile(const char *path, const char *filename) {
2093
2094 freez(rc->chart);
2095 }
2075 - rc->chart = tabs2spaces(strdupz(value));
2096 + rc->chart = strdupz(value);
2097 rc->hash_chart = simple_hash(rc->chart);
2098 }
2099 else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
@@ -2136,7 +2157,7 @@ int health_readfile(const char *path, const char *filename) {
2157
2158 freez(rc->exec);
2159 }
2139 - rc->exec = tabs2spaces(strdupz(value));
2160 + rc->exec = strdupz(value);
2161 }
2162 else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
2163 if(rc->recipient) {
@@ -2146,7 +2167,7 @@ int health_readfile(const char *path, const char *filename) {
2167
2168 freez(rc->recipient);
2169 }
2149 - rc->recipient = tabs2spaces(strdupz(value));
2170 + rc->recipient = strdupz(value);
2171 }
2172 else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
2173 if(rc->units) {
@@ -2156,7 +2177,7 @@ int health_readfile(const char *path, const char *filename) {
2177
2178 freez(rc->units);
2179 }
2159 - rc->units = tabs2spaces(strdupz(value));
2180 + rc->units = strdupz(value);
2181 strip_quotes(rc->units);
2182 }
2183 else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
@@ -2167,7 +2188,7 @@ int health_readfile(const char *path, const char *filename) {
2188
2189 freez(rc->info);
2190 }
2170 - rc->info = tabs2spaces(strdupz(value));
2191 + rc->info = strdupz(value);
2192 strip_quotes(rc->info);
2193 }
2194 else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
@@ -2190,14 +2211,14 @@ int health_readfile(const char *path, const char *filename) {
2211
2212 freez(rt->context);
2213 }
2193 - rt->context = tabs2spaces(strdupz(value));
2214 + rt->context = strdupz(value);
2215 rt->hash_context = simple_hash(rt->context);
2216 }
2217 else if(hash == hash_families && !strcasecmp(key, HEALTH_FAMILIES_KEY)) {
2218 freez(rt->family_match);
2219 simple_pattern_free(rt->family_pattern);
2220
2200 - rt->family_match = tabs2spaces(strdupz(value));
2221 + rt->family_match = strdupz(value);
2222 rt->family_pattern = simple_pattern_create(rt->family_match, SIMPLE_PATTERN_EXACT);
2223 }
2224 else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
@@ -2260,7 +2281,7 @@ int health_readfile(const char *path, const char *filename) {
2281
2282 freez(rt->exec);
2283 }
2263 - rt->exec = tabs2spaces(strdupz(value));
2284 + rt->exec = strdupz(value);
2285 }
2286 else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
2287 if(rt->recipient) {
@@ -2270,7 +2291,7 @@ int health_readfile(const char *path, const char *filename) {
2291
2292 freez(rt->recipient);
2293 }
2273 - rt->recipient = tabs2spaces(strdupz(value));
2294 + rt->recipient = strdupz(value);
2295 }
2296 else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
2297 if(rt->units) {
@@ -2280,7 +2301,7 @@ int health_readfile(const char *path, const char *filename) {
2301
2302 freez(rt->units);
2303 }
2283 - rt->units = tabs2spaces(strdupz(value));
2304 + rt->units = strdupz(value);
2305 strip_quotes(rt->units);
2306 }
2307 else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
@@ -2291,7 +2312,7 @@ int health_readfile(const char *path, const char *filename) {
2312
2313 freez(rt->info);
2314 }
2294 - rt->info = tabs2spaces(strdupz(value));
2315 + rt->info = strdupz(value);
2316 strip_quotes(rt->info);
2317 }
2318 else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {