Avoid reallocation inserting logs into database

This commit is contained in:
Jordan Rose
2025-08-04 10:55:22 -07:00
committed by Cody Henthorne
parent 57454a2661
commit 7074684474
2 changed files with 2 additions and 2 deletions

View File

@@ -160,7 +160,7 @@ class LogDatabase private constructor(
private val readableDatabase: SQLiteDatabase get() = openHelper.readableDatabase
private val writableDatabase: SQLiteDatabase get() = openHelper.writableDatabase
fun insert(logs: List<LogEntry>, currentTime: Long) {
fun insert(logs: Sequence<LogEntry>, currentTime: Long) {
writableDatabase.withinTransaction { db ->
logs.forEach { log ->
db.insertInto(TABLE_NAME)

View File

@@ -126,7 +126,7 @@ class PersistentLogger private constructor(application: Application) : Log.Logge
override fun run() {
while (true) {
requests.blockForRequests(buffer)
db.logs.insert(buffer.flatMap { requestToEntries(it) }, System.currentTimeMillis())
db.logs.insert(buffer.asSequence().flatMap { requestToEntries(it) }, System.currentTimeMillis())
buffer.clear()
requests.notifyFlushed()
}