Added port cache, improved efficiency for getPort

This commit is contained in:
PeratX 2017-05-01 16:19:15 +08:00
parent ef2729d781
commit 820b8d53c3
5 changed files with 23 additions and 18 deletions

View File

@ -272,7 +272,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
mAbout = null; mAbout = null;
mHosts = null; mHosts = null;
instance = null; instance = null;
System.gc();
} }
@Override @Override

View File

@ -90,7 +90,5 @@ public class AboutFragment extends Fragment {
mWebView.destroy(); mWebView.destroy();
mWebView = null; mWebView = null;
} }
System.gc();
} }
} }

View File

@ -25,6 +25,7 @@ import org.itxtech.daedalus.util.DnsServerHelper;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
@ -73,9 +74,7 @@ public class DnsTestFragment extends Fragment {
add(DnsServerHelper.getAddressByDescription(Daedalus.getInstance(), spinnerServerChoice.getSelectedItem().toString())); add(DnsServerHelper.getAddressByDescription(Daedalus.getInstance(), spinnerServerChoice.getSelectedItem().toString()));
String servers = Daedalus.getPrefs().getString("dns_test_servers", ""); String servers = Daedalus.getPrefs().getString("dns_test_servers", "");
if (!servers.equals("")) { if (!servers.equals("")) {
for (String server : servers.split(",")) { addAll(Arrays.asList(servers.split(",")));
add(server);
}
} }
}}; }};
DNSClient client = new DNSClient(null); DNSClient client = new DNSClient(null);
@ -164,8 +163,6 @@ public class DnsTestFragment extends Fragment {
mRunnable = null; mRunnable = null;
mHandler.shutdown(); mHandler.shutdown();
mHandler = null; mHandler = null;
System.gc();
} }
private static void stopThread() { private static void stopThread() {

View File

@ -121,6 +121,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
} }
Daedalus.initHostsResolver(); Daedalus.initHostsResolver();
DnsServerHelper.buildPortCache();
dnsQueryTimes = 0; dnsQueryTimes = 0;
if (this.mThread == null) { if (this.mThread == null) {
@ -181,8 +182,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
dnsQueryTimes = 0; dnsQueryTimes = 0;
HostsResolver.clean(); HostsResolver.clean();
DnsServerHelper.cleanPortCache();
System.gc();
} }

View File

@ -5,6 +5,7 @@ import org.itxtech.daedalus.Daedalus;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
/** /**
* Daedalus Project * Daedalus Project
@ -17,19 +18,29 @@ import java.util.ArrayList;
* the Free Software Foundation, version 3. * the Free Software Foundation, version 3.
*/ */
public class DnsServerHelper { public class DnsServerHelper {
public static int getPortOrDefault(InetAddress address, int defaultPort) { private static HashMap<String, Integer> portCache = null;
String hostAddress = address.getHostAddress();
for (DnsServer server : Daedalus.DNS_SERVERS) { public static void cleanPortCache() {
if (server.getAddress().equals(hostAddress)) { portCache = null;
return server.getPort();
} }
public static void buildPortCache() {
portCache = new HashMap<>();
for (DnsServer server : Daedalus.DNS_SERVERS) {
portCache.put(server.getAddress(), server.getPort());
} }
for (CustomDnsServer server : Daedalus.configurations.getCustomDnsServers()) { for (CustomDnsServer server : Daedalus.configurations.getCustomDnsServers()) {
if (server.getAddress().equals(hostAddress)) { portCache.put(server.getAddress(), server.getPort());
return server.getPort();
} }
}
public static int getPortOrDefault(InetAddress address, int defaultPort) {
String hostAddress = address.getHostAddress();
if (portCache.containsKey(hostAddress)) {
return portCache.get(hostAddress);
} }
return defaultPort; return defaultPort;