Skip to content

Commit 9b8f817

Browse files
hyperxpronormanmaurer
authored andcommitted
Use if-else instead of switch case in MacAddressUtil#bestAvailableMac (netty#12375)
Motivation: `switch` cases are efficient when there are quite a few branches compared to `if-else`. Since we are only using 1 case, it's best to use the `if-else` condition. Modification: Changed `switch` to `if-else` Result: Efficient code
1 parent b18f791 commit 9b8f817

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

common/src/main/java/io/netty/util/internal/MacAddressUtil.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,16 @@ public static byte[] bestAvailableMac() {
114114
return null;
115115
}
116116

117-
switch (bestMacAddr.length) {
118-
case EUI48_MAC_ADDRESS_LENGTH: // EUI-48 - convert to EUI-64
119-
byte[] newAddr = new byte[EUI64_MAC_ADDRESS_LENGTH];
120-
System.arraycopy(bestMacAddr, 0, newAddr, 0, 3);
121-
newAddr[3] = (byte) 0xFF;
122-
newAddr[4] = (byte) 0xFE;
123-
System.arraycopy(bestMacAddr, 3, newAddr, 5, 3);
124-
bestMacAddr = newAddr;
125-
break;
126-
default: // Unknown
127-
bestMacAddr = Arrays.copyOf(bestMacAddr, EUI64_MAC_ADDRESS_LENGTH);
117+
if (bestMacAddr.length == EUI48_MAC_ADDRESS_LENGTH) { // EUI-48 - convert to EUI-64
118+
byte[] newAddr = new byte[EUI64_MAC_ADDRESS_LENGTH];
119+
System.arraycopy(bestMacAddr, 0, newAddr, 0, 3);
120+
newAddr[3] = (byte) 0xFF;
121+
newAddr[4] = (byte) 0xFE;
122+
System.arraycopy(bestMacAddr, 3, newAddr, 5, 3);
123+
bestMacAddr = newAddr;
124+
} else {
125+
// Unknown
126+
bestMacAddr = Arrays.copyOf(bestMacAddr, EUI64_MAC_ADDRESS_LENGTH);
128127
}
129128

130129
return bestMacAddr;

0 commit comments

Comments
 (0)