Re: [Cocci] linux-kernel janitorial RFP: Mark static arrays as const

From: Joe Perches
Date: Wed Mar 03 2021 - 06:32:09 EST


On Tue, 2021-03-02 at 22:41 +0100, Julia Lawall wrote:
>
> On Tue, 2 Mar 2021, Joe Perches wrote:
>
> > Here is a possible opportunity to reduce data usage in the kernel.
>
> Does it actually reduce data usage?

Yes, at least for gcc. For instance:

$ gcc --version
gcc (Ubuntu 10.2.0-13ubuntu1) 10.2.0

And with the diff below (x86-64 defconfig with hwmon/pmbus and max20730)

$ diff --git a/drivers/hwmon/pmbus/max20730.c b/drivers/hwmon/pmbus/max20730.c
index 9dd3dd79bc18..b824eab95456 100644
--- a/drivers/hwmon/pmbus/max20730.c
+++ b/drivers/hwmon/pmbus/max20730.c
@@ -434,7 +434,7 @@ static long direct_to_val(u16 w, enum pmbus_sensor_classes >
return d;
}

-static u32 max_current[][5] = {
+static const u32 max_current[][5] = {
[max20710] = { 6200, 8000, 9700, 11600 },
[max20730] = { 13000, 16600, 20100, 23600 },
[max20734] = { 21000, 27000, 32000, 38000 },

$ size drivers/hwmon/pmbus/max20730.o*
text data bss dec hex filename
9245 256 0 9501 251d drivers/hwmon/pmbus/max20730.o.gcc.new
9149 352 0 9501 251d drivers/hwmon/pmbus/max20730.o.gcc.old

with clang-11 it doesn't seem to make a difference:

$ /usr/bin/clang --version
Ubuntu clang version 11.0.0-2

$ size drivers/hwmon/pmbus/max20730.o*
text data bss dec hex filename
9166 256 1 9423 24cf drivers/hwmon/pmbus/max20730.o.clang-11.new
9166 256 1 9423 24cf drivers/hwmon/pmbus/max20730.o.clang-11.old