mercredi 6 avril 2016

Performance of Realm transactions vs SQLite

I'm trying to compare the performance of Realm vs SQLite for a toy example. This is my test class:

public class Folder extends RealmObject {
    @PrimaryKey
    @Index
    private int id;
    private String name;
    private int numMessages;
    private int numRead;

    ...

}

I've coded equivalent Realm and SQLite DAOs. All the writes are made inside transactions in both cases. There are no indexes in SQLite other than the PK.

I've run a small informal benchmark (N=10000), with exactly the same input data and in the same order for each persistence mechanism. I've found that:

  1. Batch writes are about the same order, sometimes up to 2x faster in Realm, but sometimes a bit slower.
  2. Deleting and updating one by one (by id) is 6x slower in Realm.
  3. Querying by id is 6x faster in Realm, but when sorting by name it is 1.6x slower.

I'm not really concerned about #1 since this is not a proper benchmark, and times varied a lot from one iteration to the next.

But #2 really surprised me, because times were actually consistent across iterations. Each individual update or delete was done inside a transaction (I usually would batch them in the real thing). This is how I write an instance:

realm.executeTransaction(new Realm.Transaction() {

    @Override
    public void execute(Realm realm) {
        realm.copyToRealmOrUpdate(<some folder>);
    }
});

And this is how I delete by id:

realm.executeTransaction(new Realm.Transaction() {

    @Override
    public void execute(Realm realm) {
        realm.where(Folder.class).equalTo("id", <some id>).findAll().clear();
    }
});

What could be causing the slowdown? Is it the transaction body or the transaction overhead?

About #3 I'm also puzzled about the slightly worse performance when sorting the results. This is how I fetch by id:

Folder result = realm.where(Folder.class).equalTo("id", <some id>).findFirst();

And this is how I fetch everything sorted:

RealmResults<Folder> results = realm.where(Folder.class).findAllSorted("name");

Is the sorting that expensive?

Aucun commentaire:

Enregistrer un commentaire