From ff1400be4b7aece169da9a920ee9ed598e7dba63 Mon Sep 17 00:00:00 2001 From: KH Shi Date: Wed, 8 Nov 2023 14:50:15 +0000 Subject: [PATCH] Replace HashSet with ArraySet in RaParams ArraySet is designed to be more memory afficient than HashSet. It is suitable to store small numbers of items. Test: atest TetheringPrivilegedTests TetheringIntegrationTests \ TetheringTests Bug: 261923493 Change-Id: I2bd2eded801cc05ce53ef9f9486b13faf1772d22 --- Tethering/src/android/net/ip/IpServer.java | 17 +++++++++-------- .../net/ip/RouterAdvertisementDaemon.java | 13 +++++++------ .../net/ip/RouterAdvertisementDaemonTest.java | 4 ++-- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Tethering/src/android/net/ip/IpServer.java b/Tethering/src/android/net/ip/IpServer.java index 79d9a23820..aaa36d2b37 100644 --- a/Tethering/src/android/net/ip/IpServer.java +++ b/Tethering/src/android/net/ip/IpServer.java @@ -60,6 +60,7 @@ import android.os.Handler; import android.os.Message; import android.os.RemoteException; import android.os.ServiceSpecificException; +import android.util.ArraySet; import android.util.Log; import android.util.SparseArray; @@ -899,7 +900,7 @@ public class IpServer extends StateMachineShim { } private void configureLocalIPv6Routes( - HashSet deprecatedPrefixes, HashSet newPrefixes) { + ArraySet deprecatedPrefixes, ArraySet newPrefixes) { // [1] Remove the routes that are deprecated. if (!deprecatedPrefixes.isEmpty()) { removeRoutesFromLocalNetwork(getLocalRoutesFor(mIfaceName, deprecatedPrefixes)); @@ -907,7 +908,7 @@ public class IpServer extends StateMachineShim { // [2] Add only the routes that have not previously been added. if (newPrefixes != null && !newPrefixes.isEmpty()) { - HashSet addedPrefixes = (HashSet) newPrefixes.clone(); + ArraySet addedPrefixes = new ArraySet(newPrefixes); if (mLastRaParams != null) { addedPrefixes.removeAll(mLastRaParams.prefixes); } @@ -919,7 +920,7 @@ public class IpServer extends StateMachineShim { } private void configureLocalIPv6Dns( - HashSet deprecatedDnses, HashSet newDnses) { + ArraySet deprecatedDnses, ArraySet newDnses) { // TODO: Is this really necessary? Can we not fail earlier if INetd cannot be located? if (mNetd == null) { if (newDnses != null) newDnses.clear(); @@ -940,7 +941,7 @@ public class IpServer extends StateMachineShim { // [2] Add only the local DNS IP addresses that have not previously been added. if (newDnses != null && !newDnses.isEmpty()) { - final HashSet addedDnses = (HashSet) newDnses.clone(); + final ArraySet addedDnses = new ArraySet(newDnses); if (mLastRaParams != null) { addedDnses.removeAll(mLastRaParams.dnses); } @@ -1547,7 +1548,7 @@ public class IpServer extends StateMachineShim { // Accumulate routes representing "prefixes to be assigned to the local // interface", for subsequent modification of local_network routing. private static ArrayList getLocalRoutesFor( - String ifname, HashSet prefixes) { + String ifname, ArraySet prefixes) { final ArrayList localRoutes = new ArrayList(); for (IpPrefix ipp : prefixes) { localRoutes.add(new RouteInfo(ipp, null, ifname, RTN_UNICAST)); @@ -1578,8 +1579,8 @@ public class IpServer extends StateMachineShim { /** Get IPv6 prefixes from LinkProperties */ @NonNull @VisibleForTesting - static HashSet getTetherableIpv6Prefixes(@NonNull Collection addrs) { - final HashSet prefixes = new HashSet<>(); + static ArraySet getTetherableIpv6Prefixes(@NonNull Collection addrs) { + final ArraySet prefixes = new ArraySet<>(); for (LinkAddress linkAddr : addrs) { if (linkAddr.getPrefixLength() != RFC7421_PREFIX_LENGTH) continue; prefixes.add(new IpPrefix(linkAddr.getAddress(), RFC7421_PREFIX_LENGTH)); @@ -1588,7 +1589,7 @@ public class IpServer extends StateMachineShim { } @NonNull - private HashSet getTetherableIpv6Prefixes(@NonNull LinkProperties lp) { + private ArraySet getTetherableIpv6Prefixes(@NonNull LinkProperties lp) { return getTetherableIpv6Prefixes(lp.getLinkAddresses()); } } diff --git a/Tethering/src/android/net/ip/RouterAdvertisementDaemon.java b/Tethering/src/android/net/ip/RouterAdvertisementDaemon.java index 50d6c4b18d..d848ea8a63 100644 --- a/Tethering/src/android/net/ip/RouterAdvertisementDaemon.java +++ b/Tethering/src/android/net/ip/RouterAdvertisementDaemon.java @@ -41,6 +41,7 @@ import android.net.util.SocketUtils; import android.system.ErrnoException; import android.system.Os; import android.system.StructTimeval; +import android.util.ArraySet; import android.util.Log; import com.android.internal.annotations.GuardedBy; @@ -134,23 +135,23 @@ public class RouterAdvertisementDaemon { public boolean hasDefaultRoute; public byte hopLimit; public int mtu; - public HashSet prefixes; - public HashSet dnses; + public ArraySet prefixes; + public ArraySet dnses; public RaParams() { hasDefaultRoute = false; hopLimit = DEFAULT_HOPLIMIT; mtu = IPV6_MIN_MTU; - prefixes = new HashSet(); - dnses = new HashSet(); + prefixes = new ArraySet(); + dnses = new ArraySet(); } public RaParams(RaParams other) { hasDefaultRoute = other.hasDefaultRoute; hopLimit = other.hopLimit; mtu = other.mtu; - prefixes = (HashSet) other.prefixes.clone(); - dnses = (HashSet) other.dnses.clone(); + prefixes = new ArraySet(other.prefixes); + dnses = new ArraySet(other.dnses); } /** diff --git a/Tethering/tests/privileged/src/android/net/ip/RouterAdvertisementDaemonTest.java b/Tethering/tests/privileged/src/android/net/ip/RouterAdvertisementDaemonTest.java index dac5b63fe2..90ceaa1c85 100644 --- a/Tethering/tests/privileged/src/android/net/ip/RouterAdvertisementDaemonTest.java +++ b/Tethering/tests/privileged/src/android/net/ip/RouterAdvertisementDaemonTest.java @@ -47,6 +47,7 @@ import android.os.IBinder; import android.os.Looper; import android.os.RemoteException; import android.os.ServiceSpecificException; +import android.util.ArraySet; import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; @@ -77,7 +78,6 @@ import org.mockito.MockitoAnnotations; import java.net.Inet6Address; import java.net.InetAddress; import java.nio.ByteBuffer; -import java.util.HashSet; @RunWith(AndroidJUnit4.class) @SmallTest @@ -236,7 +236,7 @@ public final class RouterAdvertisementDaemonTest { final RdnssOption rdnss = Struct.parse(RdnssOption.class, RdnssBuf); final String msg = rdnss.lifetime > 0 ? "Unknown dns" : "Unknown deprecated dns"; - final HashSet dnses = + final ArraySet dnses = rdnss.lifetime > 0 ? mNewParams.dnses : mOldParams.dnses; assertNotNull(msg, dnses);