Improving degraded performance due to unnecessary object allocation. (#799)

Improving degraded performance due to unnecessary object allocation.

Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/799
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-Authored-By: opyale <opyale@noreply.codeberg.org>
Co-Committed-By: opyale <opyale@noreply.codeberg.org>
This commit is contained in:
opyale 2021-01-01 18:30:30 +01:00 committed by 6543
parent fef1597522
commit 5d2bb02b2d
3 changed files with 28 additions and 23 deletions

View File

@ -20,7 +20,7 @@ import okhttp3.OkHttpClient;
public class PicassoService { public class PicassoService {
private static PicassoService picassoService; private static PicassoService picassoService;
private static File cachePath; private final File cachePath;
private Picasso picasso; private Picasso picasso;
private PicassoService(Context context) { private PicassoService(Context context) {
@ -54,7 +54,6 @@ public class PicassoService {
Log.e("PicassoService", e.toString()); Log.e("PicassoService", e.toString());
} }
} }
public Picasso get() { public Picasso get() {

View File

@ -10,8 +10,8 @@ import org.mian.gitnex.interfaces.ApiInterface;
import org.mian.gitnex.interfaces.WebInterface; import org.mian.gitnex.interfaces.WebInterface;
import java.io.File; import java.io.File;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
@ -29,8 +29,8 @@ import retrofit2.converter.scalars.ScalarsConverterFactory;
public class RetrofitClient { public class RetrofitClient {
private static final Map<String, ApiInterface> apiInterfaces = new HashMap<>(); private static final Map<String, ApiInterface> apiInterfaces = new ConcurrentHashMap<>();
private static final Map<String, WebInterface> webInterfaces = new HashMap<>(); private static final Map<String, WebInterface> webInterfaces = new ConcurrentHashMap<>();
private static Retrofit createRetrofit(Context context, String instanceUrl) { private static Retrofit createRetrofit(Context context, String instanceUrl) {
@ -58,15 +58,13 @@ public class RetrofitClient {
.addInterceptor(chain -> { .addInterceptor(chain -> {
Request request = chain.request(); Request request = chain.request();
if(connToInternet) {
request = request.newBuilder().header("Cache-Control", "public, max-age=" + 60).build(); request = connToInternet ?
} request.newBuilder().header("Cache-Control", "public, max-age=" + 60).build() :
else { request.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 30).build();
request = request.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 30).build();
}
return chain.proceed(request); return chain.proceed(request);
}); });
return new Retrofit.Builder() return new Retrofit.Builder()
@ -85,12 +83,12 @@ public class RetrofitClient {
return null; return null;
} }
public static synchronized ApiInterface getApiInterface(Context context) { public static ApiInterface getApiInterface(Context context) {
return getApiInterface(context, TinyDB.getInstance(context).getString("instanceUrl")); return getApiInterface(context, TinyDB.getInstance(context).getString("instanceUrl"));
} }
public static synchronized WebInterface getWebInterface(Context context) { public static WebInterface getWebInterface(Context context) {
String instanceUrl = TinyDB.getInstance(context).getString("instanceUrl"); String instanceUrl = TinyDB.getInstance(context).getString("instanceUrl");
instanceUrl = instanceUrl.substring(0, instanceUrl.lastIndexOf("api/v1/")); instanceUrl = instanceUrl.substring(0, instanceUrl.lastIndexOf("api/v1/"));
@ -99,22 +97,30 @@ public class RetrofitClient {
} }
public static synchronized ApiInterface getApiInterface(Context context, String url) { public static ApiInterface getApiInterface(Context context, String url) {
if(!apiInterfaces.containsKey(url)) {
ApiInterface apiInterface = createRetrofit(context, url) ApiInterface apiInterface = createRetrofit(context, url)
.create(ApiInterface.class); .create(ApiInterface.class);
apiInterfaces.put(url, apiInterface); apiInterfaces.put(url, apiInterface);
return apiInterface;
}
return apiInterfaces.get(url); return apiInterfaces.get(url);
} }
public static synchronized WebInterface getWebInterface(Context context, String url) { public static WebInterface getWebInterface(Context context, String url) {
if(!webInterfaces.containsKey(url)) {
WebInterface webInterface = createRetrofit(context, url) WebInterface webInterface = createRetrofit(context, url)
.create(WebInterface.class); .create(WebInterface.class);
webInterfaces.put(url, webInterface); webInterfaces.put(url, webInterface);
return webInterface;
}
return webInterfaces.get(url); return webInterfaces.get(url);
} }

View File

@ -56,7 +56,7 @@ public class MemorizingTrustManager implements X509TrustManager {
private Handler masterHandler; private Handler masterHandler;
private SharedPreferences keyStoreStorage; private SharedPreferences keyStoreStorage;
private KeyStore appKeyStore; private KeyStore appKeyStore;
private X509TrustManager defaultTrustManager; private final X509TrustManager defaultTrustManager;
private X509TrustManager appTrustManager; private X509TrustManager appTrustManager;
/** /**
@ -634,7 +634,7 @@ public class MemorizingTrustManager implements X509TrustManager {
class MemorizingHostnameVerifier implements HostnameVerifier { class MemorizingHostnameVerifier implements HostnameVerifier {
private HostnameVerifier defaultVerifier; private final HostnameVerifier defaultVerifier;
MemorizingHostnameVerifier(HostnameVerifier wrapped) { MemorizingHostnameVerifier(HostnameVerifier wrapped) {