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;
mHosts = null;
instance = null;
System.gc();
}
@Override

View File

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

View File

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

View File

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

View File

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