@cryptotaxi247 / netdata-1 / commits / fc6a6d9a1

use smaller stack allocated buffer in svg generation; eliminate trailing zeros from svg values

Costa Tsaousis (ktsaou) committed Aug 27, 2016 at 13:02 UTC fc6a6d9a16a90df80ed6bc697054424f3fd1818c
1 file changed +60 -19
src/web_buffer_svg.c
+60 -19
@@ -492,46 +492,87 @@ static inline void calc_colorz(const char *color, char *final, size_t len, calcu
492 strncpyz(final, b, len);
493 }
494
495 +// value + units
496 +#define VALUE_STRING_SIZE 100
497 +
498 +// label
499 +#define LABEL_STRING_SIZE 200
500 +
501 +// colors
502 +#define COLOR_STRING_SIZE 100
503 +
504 void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const char *units, const char *label_color, const char *value_color, int value_is_null, int precision) {
496 - char label_buffer[256 + 1], value_string[512 + 1], value_color_buffer[256 + 1];
497 - char label_escaped[256 + 1], value_escaped[512 + 1], label_color_escaped[256 + 1], value_color_escaped[256 + 1];
505 + char label_buffer[LABEL_STRING_SIZE + 1]
506 + , value_color_buffer[COLOR_STRING_SIZE + 1]
507 + , value_string[VALUE_STRING_SIZE + 1]
508 + , label_escaped[LABEL_STRING_SIZE + 1]
509 + , value_escaped[VALUE_STRING_SIZE + 1]
510 + , label_color_escaped[COLOR_STRING_SIZE + 1]
511 + , value_color_escaped[COLOR_STRING_SIZE + 1];
512 +
513 int label_width, value_width, total_width;
514
500 - if(!label_color || !*label_color) label_color = "#555";
501 - if(!value_color || !*value_color) value_color = (value_is_null)?"#999":"#4c1";
515 + if(unlikely(!label_color || !*label_color))
516 + label_color = "#555";
517 +
518 + if(unlikely(!value_color || !*value_color))
519 + value_color = (value_is_null)?"#999":"#4c1";
520
521 units = fix_units(units);
504 - calc_colorz(value_color, value_color_buffer, 256, value, value_is_null);
522 + calc_colorz(value_color, value_color_buffer, COLOR_STRING_SIZE, value, value_is_null);
523
524 char *separator = "";
507 - if(isalnum(*units)) separator = " ";
525 + if(unlikely(isalnum(*units)))
526 + separator = " ";
527
509 - if(value_is_null)
528 + if(unlikely(value_is_null))
529 strcpy(value_string, "-");
530 +
531 else if(precision < 0) {
512 - calculated_number abs = (value < (calculated_number)0)?-value:value;
513 - if(abs > (calculated_number)1000.0) snprintfz(value_string, 512, "%0.0Lf%s%s", (long double)value, separator, units);
514 - else if(abs > (calculated_number)100.0) snprintfz(value_string, 512, "%0.1Lf%s%s", (long double)value, separator, units);
515 - else if(abs > (calculated_number)1.0) snprintfz(value_string, 512, "%0.2Lf%s%s", (long double)value, separator, units);
516 - else if(abs > (calculated_number)0.1) snprintfz(value_string, 512, "%0.3Lf%s%s", (long double)value, separator, units);
517 - else snprintfz(value_string, 512, "%0.4Lf%s%s", (long double)value, separator, units);
532 + size_t len, l, lstop = 0;
533 +
534 + calculated_number abs = value;
535 + if(isless(value, 0)) {
536 + lstop = 1;
537 + abs = -value;
538 + }
539 +
540 + if(isgreaterequal(abs, 1000)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.0Lf%s%s", (long double)value, separator, units);
541 + else if(isgreaterequal(abs, 100)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.1Lf%s%s", (long double)value, separator, units);
542 + else if(isgreaterequal(abs, 1)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.2Lf%s%s", (long double)value, separator, units);
543 + else if(isgreaterequal(abs, 0.1)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.3Lf%s%s", (long double)value, separator, units);
544 + else len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.4Lf%s%s", (long double)value, separator, units);
545 +
546 + // remove trailing zeros
547 + for(l = len - 1; l > lstop ; l--) {
548 + if(likely(value_string[l] == '0'))
549 + value_string[l] = '\0';
550 +
551 + else if(unlikely(value_string[l] == '.')) {
552 + value_string[l] = '\0';
553 + break;
554 + }
555 +
556 + else
557 + break;
558 + }
559 }
560 else {
561 if(precision > 50) precision = 50;
521 - snprintfz(value_string, 512, "%0.*Lf%s%s", precision, (long double)value, separator, units);
562 + snprintfz(value_string, VALUE_STRING_SIZE, "%0.*Lf%s%s", precision, (long double)value, separator, units);
563 }
564
565 // we need to copy the label, since verdana11_width may write to it
525 - strncpyz(label_buffer, label, 256);
566 + strncpyz(label_buffer, label, LABEL_STRING_SIZE);
567
568 label_width = verdana11_width(label_buffer) + (BADGE_HORIZONTAL_PADDING * 2);
569 value_width = verdana11_width(value_string) + (BADGE_HORIZONTAL_PADDING * 2);
570 total_width = label_width + value_width;
571
531 - escape_xmlz(label_escaped, label_buffer, 256);
532 - escape_xmlz(value_escaped, value_string, 256);
533 - escape_xmlz(label_color_escaped, color_map(label_color), 256);
534 - escape_xmlz(value_color_escaped, color_map(value_color_buffer), 256);
572 + escape_xmlz(label_escaped, label_buffer, LABEL_STRING_SIZE);
573 + escape_xmlz(value_escaped, value_string, VALUE_STRING_SIZE);
574 + escape_xmlz(label_color_escaped, color_map(label_color), COLOR_STRING_SIZE);
575 + escape_xmlz(value_color_escaped, color_map(value_color_buffer), COLOR_STRING_SIZE);
576
577 wb->contenttype = CT_IMAGE_SVG_XML;
578