From 436808ff99d7b7e6f5d6e8f3127d9d03f6295f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Wed, 14 Apr 2021 20:47:42 +0200 Subject: Use RichString_appendnAscii where possible `RichString_appendnAscii()` avoids a `strlen(3)` call over ` RichString_appendAscii()`. Use the former where the length is available from a previous checked `snprintf(3)` call. Keep `RichString_appendAscii()` when passing a string literal and rely on compilers to optimize the `strlen(3)` call away. --- LoadAverageMeter.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'LoadAverageMeter.c') diff --git a/LoadAverageMeter.c b/LoadAverageMeter.c index 196cee9f..46685f43 100644 --- a/LoadAverageMeter.c +++ b/LoadAverageMeter.c @@ -60,12 +60,14 @@ static void LoadAverageMeter_updateValues(Meter* this) { static void LoadAverageMeter_display(const Object* cast, RichString* out) { const Meter* this = (const Meter*)cast; char buffer[20]; - xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[0]); - RichString_writeAscii(out, CRT_colors[LOAD_AVERAGE_ONE], buffer); - xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[1]); - RichString_appendAscii(out, CRT_colors[LOAD_AVERAGE_FIVE], buffer); - xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[2]); - RichString_appendAscii(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer); + int len; + + len = xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[0]); + RichString_appendnAscii(out, CRT_colors[LOAD_AVERAGE_ONE], buffer, len); + len = xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[1]); + RichString_appendnAscii(out, CRT_colors[LOAD_AVERAGE_FIVE], buffer, len); + len = xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[2]); + RichString_appendnAscii(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer, len); } static void LoadMeter_updateValues(Meter* this) { @@ -90,8 +92,10 @@ static void LoadMeter_updateValues(Meter* this) { static void LoadMeter_display(const Object* cast, RichString* out) { const Meter* this = (const Meter*)cast; char buffer[20]; - xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[0]); - RichString_writeAscii(out, CRT_colors[LOAD], buffer); + int len; + + len = xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[0]); + RichString_appendnAscii(out, CRT_colors[LOAD], buffer, len); } const MeterClass LoadAverageMeter_class = { -- cgit v1.2.3