1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-26 08:56:54 +00:00

Minor performance improvements to WiredNetworkChange

This should improve the performance of the common case, where one
peripheral set is empty.
This commit is contained in:
SquidDev 2018-04-16 15:30:44 +01:00
parent 20a47a7f88
commit 6ca61f000f

View File

@ -10,6 +10,8 @@ import java.util.Map;
public class WiredNetworkChange implements IWiredNetworkChange
{
private static final WiredNetworkChange EMPTY = new WiredNetworkChange( Collections.emptyMap(), Collections.emptyMap() );
private final Map<String, IPeripheral> removed;
private final Map<String, IPeripheral> added;
@ -26,16 +28,30 @@ public class WiredNetworkChange implements IWiredNetworkChange
public static WiredNetworkChange added( Map<String, IPeripheral> added )
{
return new WiredNetworkChange( Collections.emptyMap(), Collections.unmodifiableMap( added ) );
return added.isEmpty() ? EMPTY : new WiredNetworkChange( Collections.emptyMap(), Collections.unmodifiableMap( added ) );
}
public static WiredNetworkChange removed( Map<String, IPeripheral> removed )
{
return new WiredNetworkChange( Collections.unmodifiableMap( removed ), Collections.emptyMap() );
return removed.isEmpty() ? EMPTY : new WiredNetworkChange( Collections.unmodifiableMap( removed ), Collections.emptyMap() );
}
public static WiredNetworkChange changeOf( Map<String, IPeripheral> oldPeripherals, Map<String, IPeripheral> newPeripherals )
{
// Handle the trivial cases, where all peripherals have been added or removed.
if( oldPeripherals.isEmpty() && newPeripherals.isEmpty() )
{
return EMPTY;
}
else if( oldPeripherals.isEmpty() )
{
return new WiredNetworkChange( Collections.emptyMap(), newPeripherals );
}
else if( newPeripherals.isEmpty() )
{
return new WiredNetworkChange( oldPeripherals, Collections.emptyMap() );
}
Map<String, IPeripheral> added = new HashMap<>( newPeripherals );
Map<String, IPeripheral> removed = new HashMap<>();