Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to thread safe regex match caching #4

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import xyz.wingio.syntakts.parser.addTextRule
import xyz.wingio.syntakts.style.StyledTextBuilder
import xyz.wingio.syntakts.util.Logger
import xyz.wingio.syntakts.util.LoggerImpl
import xyz.wingio.syntakts.util.SynchronizedCache
import xyz.wingio.syntakts.util.Stack
import xyz.wingio.syntakts.util.firstMapOrNull

Expand Down Expand Up @@ -237,7 +238,7 @@ public class Syntakts<C> internal constructor(
if(debugOptions.enableLogging) debugOptions.logger.debug(message)
}

private val cache: MutableMap<String, MatchResult?> = mutableMapOf()
private val cache: SynchronizedCache<String, MatchResult> = SynchronizedCache()

/**
* Parse an input using the specified [rules]
Expand Down Expand Up @@ -273,11 +274,11 @@ public class Syntakts<C> internal constructor(
rules.firstMapOrNull { rule ->
val key = "${rule.regex}-$inspectionSource-$lastCapture"

val matchResult = if(cache.containsKey(key))
val matchResult = if(cache.hasKey(key))
cache[key]
else
rule.match(inspectionSource, lastCapture).apply {
if(cache.size > 10_000) cache.remove(cache.keys.first())
if(cache.size > 10_000) cache.removeFirst()
cache[key] = this
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package xyz.wingio.syntakts.util

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock

/**
* Thread safe cache store, all writes are restricted with a [Mutex] to prevent [ConcurrentModificationException]s
*/
public class SynchronizedCache<K, V> {

private val scope: CoroutineScope = CoroutineScope(Dispatchers.Main.immediate)
private val mutex: Mutex = Mutex(false)
private val cache: MutableMap<K, V?> = mutableMapOf()

/**
* Number of cached entities
*/
public val size: Int get() = cache.size

public operator fun set(key: K, value: V?) {
scope.launch {
mutex.withLock {
cache[key] = value
}
}
}

public operator fun get(key: K): V? {
return cache[key]
}

/**
* Returns true if this cache already contains an element with the given [key]
*/
public fun hasKey(key: K): Boolean {
return cache.containsKey(key)
}

/**
* Removes the first element of this cache
*/
public fun removeFirst() {
scope.launch {
mutex.withLock {
cache.remove(cache.keys.firstOrNull())
}
}
}

}
Loading