-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d01470f
commit 25f45e6
Showing
15 changed files
with
553 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
selekt-java/src/jmh/kotlin/com/bloomberg/selekt/cache/benchmarks/LinkedLruCacheBenchmark.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2024 Bloomberg Finance L.P. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.bloomberg.selekt.cache.benchmarks | ||
|
||
import com.bloomberg.selekt.cache.LinkedLruCache | ||
import org.openjdk.jmh.annotations.Benchmark | ||
import org.openjdk.jmh.annotations.BenchmarkMode | ||
import org.openjdk.jmh.annotations.Level | ||
import org.openjdk.jmh.annotations.Mode | ||
import org.openjdk.jmh.annotations.Scope | ||
import org.openjdk.jmh.annotations.Setup | ||
import org.openjdk.jmh.annotations.State | ||
|
||
@State(Scope.Thread) | ||
open class LinkedCacheInput { | ||
internal lateinit var cache: LinkedLruCache<Any> | ||
|
||
@Setup(Level.Iteration) | ||
fun setUp() { | ||
cache = LinkedLruCache(1) {} | ||
} | ||
} | ||
|
||
open class LinkedLruCacheBenchmark { | ||
@Benchmark | ||
@BenchmarkMode(Mode.Throughput) | ||
fun getEntry(input: LinkedCacheInput) = input.cache.run { | ||
get("1") {} | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.Throughput) | ||
fun getEntryWithEviction(input: LinkedCacheInput) = input.cache.run { | ||
get("1") {} | ||
get("2") {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
selekt-java/src/main/kotlin/com/bloomberg/selekt/cache/CommonLruCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2024 Bloomberg Finance L.P. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.bloomberg.selekt.cache | ||
|
||
class CommonLruCache<T : Any>( | ||
@PublishedApi | ||
internal val maxSize: Int, | ||
disposal: (T) -> Unit | ||
) { | ||
@PublishedApi | ||
internal var cache: Any = StampedCache(maxSize, disposal) | ||
|
||
fun evict(key: String) { | ||
when (val cache = cache) { | ||
is StampedCache<*> -> cache.evict(key) | ||
is LinkedLruCache<*> -> cache.evict(key) | ||
else -> error("Unrecognized cache class: {}") | ||
} | ||
} | ||
|
||
fun evictAll() { | ||
when (val cache = cache) { | ||
is StampedCache<*> -> cache.evictAll() | ||
is LinkedLruCache<*> -> cache.evictAll() | ||
else -> error("Unrecognized cache class: {}") | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
inline fun get(key: String, supplier: () -> T): T = when (cache) { | ||
is StampedCache<*> -> (cache as StampedCache<T>).let { | ||
it.get(key) { | ||
supplier().also { value -> | ||
if (it.shouldTransform()) { | ||
// Adding another entry to the cache will necessitate the removal of the | ||
// least recently used entry first to honour our maximum size constraint. | ||
// For the implementation of the store currently assigned, this is an O(N) | ||
// operation. We transform to an O(1) implementation. | ||
transform() | ||
(this@CommonLruCache.cache as LinkedLruCache<T>).store.put(key, value) | ||
} | ||
} | ||
} | ||
} | ||
is LinkedLruCache<*> -> (cache as LinkedLruCache<T>).get(key, supplier) | ||
else -> error("Unrecognized cache class: {}") | ||
} | ||
|
||
fun containsKey(key: String) = when (val cache = cache) { | ||
is StampedCache<*> -> cache.containsKey(key) | ||
is LinkedLruCache<*> -> cache.containsKey(key) | ||
else -> error("Unrecognized cache class: {}") | ||
} | ||
|
||
@PublishedApi | ||
internal fun StampedCache<T>.shouldTransform() = (store.size >= maxSize) | ||
|
||
@PublishedApi | ||
internal fun transform() { | ||
(cache as StampedCache<*>).asLruCache().also { | ||
cache = it | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
selekt-java/src/main/kotlin/com/bloomberg/selekt/cache/StampedCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2024 Bloomberg Finance L.P. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.bloomberg.selekt.cache | ||
|
||
import com.bloomberg.selekt.collections.map.FastStampedStringMap | ||
import javax.annotation.concurrent.NotThreadSafe | ||
|
||
@NotThreadSafe | ||
class StampedCache<T : Any>( | ||
capacity: Int, | ||
@PublishedApi | ||
@JvmField | ||
internal val disposal: (T) -> Unit | ||
) { | ||
@PublishedApi | ||
@JvmField | ||
internal var store = FastStampedStringMap(capacity = capacity, disposal = disposal) | ||
|
||
fun evict(key: String) { | ||
store.removeKey(key) | ||
} | ||
|
||
fun evictAll() { | ||
store.clear() | ||
} | ||
|
||
inline fun get(key: String, supplier: () -> T): T = store.getElsePut(key, supplier) | ||
|
||
fun containsKey(key: String) = store.containsKey(key) | ||
|
||
internal fun asLruCache() = LinkedLruCache( | ||
maxSize = store.size, | ||
store = store.asLinkedMap(store.size, disposal) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.