From 5affa05bd609aee248836216ed7f59adc456a754 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Fri, 10 Nov 2023 21:16:35 +0800 Subject: DiskIOMeter: Cache the formatted "KiB/s" number strings In DiskIOMeter, the number strings of bytes per second are formatted with Meter_humanUnit(). As the numbers are only updated every 500 ms, it is good to cache the formatted strings. This saves code size. Signed-off-by: Kang-Che Sung --- DiskIOMeter.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'DiskIOMeter.c') diff --git a/DiskIOMeter.c b/DiskIOMeter.c index 6662918b..9740db47 100644 --- a/DiskIOMeter.c +++ b/DiskIOMeter.c @@ -27,8 +27,8 @@ static const int DiskIOMeter_attributes[] = { }; static MeterRateStatus status = RATESTATUS_INIT; -static uint32_t cached_read_diff; -static uint32_t cached_write_diff; +static char cached_read_diff_str[6]; +static char cached_write_diff_str[6]; static double cached_utilisation_diff; static void DiskIOMeter_updateValues(Meter* this) { @@ -67,19 +67,19 @@ static void DiskIOMeter_updateValues(Meter* this) { diff = data.totalBytesRead - cached_read_total; diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */ diff /= ONE_K; /* convert to KiB/s */ - cached_read_diff = (uint32_t)diff; } else { - cached_read_diff = 0; + diff = 0; } + Meter_humanUnit(cached_read_diff_str, diff, sizeof(cached_read_diff_str)); if (data.totalBytesWritten > cached_write_total) { diff = data.totalBytesWritten - cached_write_total; diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */ diff /= ONE_K; /* convert to KiB/s */ - cached_write_diff = (uint32_t)diff; } else { - cached_write_diff = 0; + diff = 0; } + Meter_humanUnit(cached_write_diff_str, diff, sizeof(cached_write_diff_str)); if (data.totalMsTimeSpend > cached_msTimeSpend_total) { diff = data.totalMsTimeSpend - cached_msTimeSpend_total; @@ -110,10 +110,7 @@ static void DiskIOMeter_updateValues(Meter* this) { return; } - char bufferRead[12], bufferWrite[12]; - Meter_humanUnit(bufferRead, cached_read_diff, sizeof(bufferRead)); - Meter_humanUnit(bufferWrite, cached_write_diff, sizeof(bufferWrite)); - xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "r:%siB/s w:%siB/s %.1f%%", bufferRead, bufferWrite, cached_utilisation_diff); + xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "r:%siB/s w:%siB/s %.1f%%", cached_read_diff_str, cached_write_diff_str, cached_utilisation_diff); } static void DiskIOMeter_display(ATTR_UNUSED const Object* cast, RichString* out) { @@ -139,13 +136,11 @@ static void DiskIOMeter_display(ATTR_UNUSED const Object* cast, RichString* out) RichString_appendnAscii(out, CRT_colors[color], buffer, len); RichString_appendAscii(out, CRT_colors[METER_TEXT], " read: "); - Meter_humanUnit(buffer, cached_read_diff, sizeof(buffer)); - RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], buffer); + RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], cached_read_diff_str); RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], "iB/s"); RichString_appendAscii(out, CRT_colors[METER_TEXT], " write: "); - Meter_humanUnit(buffer, cached_write_diff, sizeof(buffer)); - RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], buffer); + RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], cached_write_diff_str); RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], "iB/s"); } -- cgit v1.2.3