On Sat, Nov 06, 2021 at 12:58:31PM +0100, Lukas Wunner wrote: > On Sat, Nov 06, 2021 at 04:56:05PM +0530, Puranjay Mohan wrote: > > + switch (i) { > > + case 0: return "BAR 0"; > > + case 1: return "BAR 1"; > > + case 2: return "BAR 2"; > > + case 3: return "BAR 3"; > > + case 4: return "BAR 4"; > > + case 5: return "BAR 5"; > > + case PCI_ROM_RESOURCE: return "ROM"; > > + #ifdef CONFIG_PCI_IOV > > + case PCI_IOV_RESOURCES + 0: return "VF BAR 0"; > > + case PCI_IOV_RESOURCES + 1: return "VF BAR 1"; > > + case PCI_IOV_RESOURCES + 2: return "VF BAR 2"; > > + case PCI_IOV_RESOURCES + 3: return "VF BAR 3"; > > + case PCI_IOV_RESOURCES + 4: return "VF BAR 4"; > > + case PCI_IOV_RESOURCES + 5: return "VF BAR 5"; > > + #endif > > + } > > Use a static const array of char * instead of a switch/case ladder > to reduce LoC count and improve performance. > > See pcie_to_hpx3_type[] or state_conv[] in drivers/pci/pci-acpi.c > for an example. I tried converting this and came up with the below. Is that the sort of thing you're thinking? Gcc *does* generate slightly smaller code for it, but Puranjay's original source code is smaller and IMO a little easier to read. And I just noticed that Puranjay's code nicely returns "unknown" for BAR 2-5 of bridges, while my code below does not. Could be fixed, of course, but would take a little more code. const char *pci_resource_name(struct pci_dev *dev, int i) { static const char *bar_name[] = { "BAR 0", "BAR 1", "BAR 2", "BAR 3", "BAR 4", "BAR 5", "ROM", #ifdef CONFIG_PCI_IOV "VF BAR 0", "VF BAR 1", "VF BAR 2", "VF BAR 3", "VF BAR 4", "VF BAR 5", #endif "bridge I/O window", "bridge mem window", "bridge mem pref window", }; static const char *cardbus_name[] = { "BAR 0", "unknown", "unknown", "unknown", "unknown", "unknown", "unknown", #ifdef CONFIG_PCI_IOV "unknown", "unknown", "unknown", "unknown", "unknown", "unknown", #endif "CardBus bridge I/O 0 window", "CardBus bridge I/O 1 window", "CardBus bridge mem 0 window", "CardBus bridge mem 1 window", }; if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS && i < ARRAY_SIZE(cardbus_name)) return cardbus_name[i]; else if (i < ARRAY_SIZE(bar_name)) return bar_name[i]; return "unknown"; }