CBL 2.6.0 Performance: saving 11,000 MutableDocuments takes 20 seconds; what am I doing wrong?

**Disclaimer: CBL user **

I guess it is worth comparing it to a baseline app. Here’s a single Activity creating 10,000 tiny documents:

public class MainActivity extends AppCompatActivity {

    private static final String DATABASE_NAME = "db";
    private final String TAG = "CblDemo";
    private Database database;
    private int noOfDocuments = 10000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(v -> new Thread(this::createDocumentsInThread).start());
        findViewById(R.id.button2).setOnClickListener(v -> new Thread(this::createDocumentsInThreadInBatch).start());
    }

    private void createDocumentsInThread() {
        long t0 = System.currentTimeMillis();
        /* Create database */
        try {
            createDatabase();
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
        long t1 = System.currentTimeMillis();
        Log.d(TAG, "time in ms to create database = " + (t1 - t0));
        for (int i = 0; i < noOfDocuments; i++) {
            try {
                createDocument(i);
            } catch (CouchbaseLiteException e) {
                e.printStackTrace();
            }
        }
        long t2 = System.currentTimeMillis();
        Log.d(TAG, "time in ms to store documents = " + (t2 - t1));
    }

    private void createDocumentsInThreadInBatch() {
        long t0 = System.currentTimeMillis();
        /* Create database */
        try {
            createDatabase();
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
        long t1 = System.currentTimeMillis();
        Log.d(TAG, "time in ms to create database = " + (t1 - t0));
        try {
            database.inBatch(() -> {
                for (int i = 0; i < noOfDocuments; i++) {
                    try {
                        createDocument(i);
                    } catch (CouchbaseLiteException e) {
                        e.printStackTrace();
                    }
                }
                long t2 = System.currentTimeMillis();
                Log.d(TAG, "time in ms to store documents = " + (t2 - t1));
            });
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
    }

    private void createDatabase() throws CouchbaseLiteException {
        CouchbaseLite.init(MainActivity.this);
        DatabaseConfiguration configuration = new DatabaseConfiguration();
        database = new Database(DATABASE_NAME, configuration);
    }

    private void createDocument(int i) throws CouchbaseLiteException {
        MutableDocument mutableDocument = new MutableDocument();
        mutableDocument.setInt("no", i);
        database.save(mutableDocument);
    }
}

Layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="Start!"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="In Batch!"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.cbl.demobulkinsert"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.couchbase.lite:couchbase-lite-android:2.6.2'
}

Test device: Xiaomi Mi A2 (~160 USD on Amazon.com). App was reinstalled between test runs. Here are my test results:

Debug apk
Normal insert
D/CblDemo: time in ms to create database = 47
D/CblDemo: time in ms to store documents = 5200
In batch insert
D/CblDemo: time in ms to create database = 74
D/CblDemo: time in ms to store documents = 1466

Release apk
Normal insert
D/CblDemo: time in ms to create database = 46
D/CblDemo: time in ms to store documents = 5035
In batch insert
D/CblDemo: time in ms to create database = 53
D/CblDemo: time in ms to store documents = 1092

Inserting documents in batch increases performance by a lot! Running the test with a release build shows a further performance increase. I think it all comes down to how complex the documents are and how big their body is. This also includes field names, e.g. field name ‘s’ vs field name ‘street_name’. Writing this I found another performance increase.

// Replacing this line
MutableDocument mutableDocument = new MutableDocument();
// with
MutableDocument mutableDocument = new MutableDocument(String.valueOf(i));

This may not be recommended but it does improve insert times. Could you share your numbers running the code above? Or could you share demo code so I can do a test run?