Skip to content

Commit

Permalink
Warm up LiveRecipientCache at launch.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Oct 18, 2019
1 parent feccee5 commit 8c03745
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/org/thoughtcrime/securesms/ApplicationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public void onCreate() {
public void onStart(@NonNull LifecycleOwner owner) {
isAppVisible = true;
Log.i(TAG, "App is now visible.");
ApplicationDependencies.getRecipientCache().warmUp();
executePendingContactSync();
KeyCachingService.onAppForegrounded(this);
}
Expand Down
4 changes: 0 additions & 4 deletions src/org/thoughtcrime/securesms/recipients/LiveRecipient.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ public void removeForeverObserver(@NonNull RecipientForeverObserver observer) {

if (Util.isMainThread()) {
Log.w(TAG, "[Resolve][MAIN] " + getId(), new Throwable());
} else {
Log.d(TAG, "[Resolve][" + Thread.currentThread().getName() + "] " + getId());
}

Recipient updated = fetchRecipientFromDisk(getId());
Expand Down Expand Up @@ -154,8 +152,6 @@ public void refresh() {

if (Util.isMainThread()) {
Log.w(TAG, "[Refresh][MAIN] " + getId(), new Throwable());
} else {
Log.d(TAG, "[Refresh][" + Thread.currentThread().getName() + "] " + getId());
}

Recipient recipient = fetchRecipientFromDisk(getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,51 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;

import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import androidx.lifecycle.MutableLiveData;

import com.annimon.stream.Stream;

import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.RecipientDatabase;
import org.thoughtcrime.securesms.database.RecipientDatabase.MissingRecipientError;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.database.model.ThreadRecord;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.util.LRUCache;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class LiveRecipientCache {

private static final String TAG = Log.tag(LiveRecipientCache.class);

private static final int CACHE_MAX = 1000;
private static final int CACHE_WARM_MAX = 500;

private final Context context;
private final RecipientDatabase recipientDatabase;
private final Map<RecipientId, LiveRecipient> recipients;
private final LiveRecipient unknown;

private RecipientId localRecipientId;
private boolean warmedUp;

@SuppressLint("UseSparseArrays")
public LiveRecipientCache(@NonNull Context context) {
this.context = context.getApplicationContext();
this.recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
this.recipients = new LRUCache<>(1000);
this.recipients = new LRUCache<>(CACHE_MAX);
this.unknown = new LiveRecipient(context, new MutableLiveData<>(), Recipient.UNKNOWN);
}

Expand Down Expand Up @@ -71,4 +86,33 @@ public LiveRecipientCache(@NonNull Context context) {

return getLive(localRecipientId).resolve();
}

@AnyThread
public synchronized void warmUp() {
if (warmedUp) {
return;
} else {
warmedUp = true;
}

SignalExecutors.BOUNDED.execute(() -> {
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(context);

try (ThreadDatabase.Reader reader = threadDatabase.readerFor(threadDatabase.getConversationList())) {
int i = 0;
ThreadRecord record = null;
List<Recipient> recipients = new ArrayList<>();

while ((record = reader.getNext()) != null && i < CACHE_WARM_MAX) {
recipients.add(record.getRecipient());
i++;
}

Log.d(TAG, "Warming up " + recipients.size() + " recipients.");

Collections.reverse(recipients);
Stream.of(recipients).map(Recipient::getId).forEach(this::getLive);
}
});
}
}

0 comments on commit 8c03745

Please sign in to comment.