Skip to content

Commit

Permalink
⚡️ Improve getLastUse performance
Browse files Browse the repository at this point in the history
Previously, the getLastUse flow would simply grab all uses and
programmatically calculate the latest.

This commit moves this behavior to the database in order to enhance
performance. Performance was demonstrated in a test ensuring the
execution takes less than the original implementation.

Closes #108

Signed-off-by: Leonardo Colman Lopes <[email protected]>
  • Loading branch information
LeoColman committed Oct 10, 2022
1 parent 19717cf commit 2952062
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package br.com.colman.petals.use.repository
import br.com.colman.petals.UseQueries
import com.squareup.sqldelight.runtime.coroutines.asFlow
import com.squareup.sqldelight.runtime.coroutines.mapToList
import com.squareup.sqldelight.runtime.coroutines.mapToOneOrNull
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import timber.log.Timber
Expand All @@ -23,7 +24,7 @@ class UseRepository(
useQueries.insert(use.toEntity())
}

fun getLastUse() = all().map { it.maxByOrNull { it.date } }
fun getLastUse() = useQueries.selectLast().asFlow().mapToOneOrNull().map { it?.toUse() }

fun getLastUseDate() = getLastUse().map { it?.date }

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/sqldelight/br/com/colman/petals/Use.sq
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ CREATE TABLE Use(
insert:
INSERT INTO Use(date, amount_grams, cost_per_gram, id) VALUES ?;

selectLast:
SELECT * FROM Use ORDER BY date DESC LIMIT 1;

selectAll:
SELECT * FROM Use;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
package br.com.colman.petals.use.repository

import br.com.colman.petals.Database
import br.com.colman.petals.use.io.useArb
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver.Companion.IN_MEMORY
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldBeEmpty
import io.kotest.matchers.longs.shouldBeLessThan
import io.kotest.matchers.shouldBe
import io.kotest.property.arbitrary.take
import kotlinx.coroutines.flow.first
import java.math.BigDecimal
import kotlin.system.measureTimeMillis

class UseRepositoryTest : FunSpec({

Expand Down Expand Up @@ -72,5 +76,15 @@ class UseRepositoryTest : FunSpec({
target.getLastUseDate().first() shouldBe use.date
}

test("Last use performance") {
useArb.take(100_000).map(Use::toEntity).forEach(database.useQueries::insert)

measureTimeMillis {
target.getLastUseDate().first()
} shouldBeLessThan measureTimeMillis {
target.all().first().maxByOrNull { it.date }
}
}

isolationMode = IsolationMode.InstancePerTest
})

0 comments on commit 2952062

Please sign in to comment.