Skip to content

Commit 15325b4

Browse files
committed
vsprintf: rework bitmap_list_string
bitmap_list_string() is very ineffective when printing bitmaps with long ranges of set bits because it calls find_next_bit for each bit in the bitmap. We can do better by detecting ranges of set bits. In my environment, before/after is 943008/31008 ns. Signed-off-by: Yury Norov <yury.norov@gmail.com> Tested-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
1 parent db73130 commit 15325b4

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

lib/vsprintf.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,20 +1241,13 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
12411241
struct printf_spec spec, const char *fmt)
12421242
{
12431243
int nr_bits = max_t(int, spec.field_width, 0);
1244-
/* current bit is 'cur', most recently seen range is [rbot, rtop] */
1245-
int cur, rbot, rtop;
12461244
bool first = true;
1245+
int rbot, rtop;
12471246

12481247
if (check_pointer(&buf, end, bitmap, spec))
12491248
return buf;
12501249

1251-
rbot = cur = find_first_bit(bitmap, nr_bits);
1252-
while (cur < nr_bits) {
1253-
rtop = cur;
1254-
cur = find_next_bit(bitmap, nr_bits, cur + 1);
1255-
if (cur < nr_bits && cur <= rtop + 1)
1256-
continue;
1257-
1250+
for_each_set_bitrange(rbot, rtop, bitmap, nr_bits) {
12581251
if (!first) {
12591252
if (buf < end)
12601253
*buf = ',';
@@ -1263,15 +1256,12 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
12631256
first = false;
12641257

12651258
buf = number(buf, end, rbot, default_dec_spec);
1266-
if (rbot < rtop) {
1267-
if (buf < end)
1268-
*buf = '-';
1269-
buf++;
1270-
1271-
buf = number(buf, end, rtop, default_dec_spec);
1272-
}
1259+
if (rtop == rbot + 1)
1260+
continue;
12731261

1274-
rbot = cur;
1262+
if (buf < end)
1263+
*buf = '-';
1264+
buf = number(++buf, end, rtop - 1, default_dec_spec);
12751265
}
12761266
return buf;
12771267
}

0 commit comments

Comments
 (0)