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

Optimised LRU cache. #533

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/SelektExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ fun Project.disableKotlinCompilerAssertions() {
kotlinOptions {
freeCompilerArgs = listOf(
"-Xno-call-assertions",
"-Xno-receiver-assertions",
"-Xno-param-assertions"
"-Xno-param-assertions",
"-Xno-receiver-assertions"
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.CommonLruCache
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 CommonCacheInput {
internal lateinit var cache: CommonLruCache<Any>
internal lateinit var largeCache: CommonLruCache<Any>

@Setup(Level.Iteration)
fun setUp() {
cache = CommonLruCache(1) {}
largeCache = CommonLruCache(64) {}
}
}

open class CommonLruCacheBenchmark {
@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntry(input: CommonCacheInput) = input.cache.run {
get("1") {}
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntryWithEviction(input: CommonCacheInput) = input.cache.run {
get("1") {}
get("2") {}
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntries(input: CommonCacheInput) = input.largeCache.run {
get("1") { "" }
get("2") { "" }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getManyEntries(input: CommonCacheInput) = input.largeCache.run {
get("0") { "" }
get("1") { "" }
get("2") { "" }
get("3") { "" }
get("4") { "" }
get("5") { "" }
get("6") { "" }
get("7") { "" }
get("8") { "" }
get("9") { "" }
get("2") { "" }
get("3") { "" }
get("9") { "" }
get("4") { "" }
get("5") { "" }
get("0") { "" }
get("8") { "" }
get("6") { "" }
get("1") { "" }
get("7") { "" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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>
internal lateinit var largeCache: LinkedLruCache<Any>

@Setup(Level.Iteration)
fun setUp() {
cache = LinkedLruCache(1) {}
largeCache = LinkedLruCache(64) {}
}
}

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") {}
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntries(input: LinkedCacheInput) = input.largeCache.run {
get("1") { "" }
get("2") { "" }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getManyEntries(input: LinkedCacheInput) = input.largeCache.run {
get("0") { "" }
get("1") { "" }
get("2") { "" }
get("3") { "" }
get("4") { "" }
get("5") { "" }
get("6") { "" }
get("7") { "" }
get("8") { "" }
get("9") { "" }
get("2") { "" }
get("3") { "" }
get("9") { "" }
get("4") { "" }
get("5") { "" }
get("0") { "" }
get("8") { "" }
get("6") { "" }
get("1") { "" }
get("7") { "" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.StampedCache
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 StampedCacheInput {
internal lateinit var cache: StampedCache<Any>
internal lateinit var largeCache: StampedCache<Any>

@Setup(Level.Iteration)
fun setUp() {
cache = StampedCache(1) {}
largeCache = StampedCache(64) {}
}
}

open class StampedCacheBenchmark {
@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntry(input: StampedCacheInput) = input.cache.run {
get("1") {}
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntryWithEviction(input: StampedCacheInput) = input.cache.run {
get("1") {}
get("2") {}
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntries(input: StampedCacheInput) = input.largeCache.run {
get("1") { "" }
get("2") { "" }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getManyEntries(input: StampedCacheInput) = input.largeCache.run {
get("0") { "" }
get("1") { "" }
get("2") { "" }
get("3") { "" }
get("4") { "" }
get("5") { "" }
get("6") { "" }
get("7") { "" }
get("8") { "" }
get("9") { "" }
get("2") { "" }
get("3") { "" }
get("9") { "" }
get("4") { "" }
get("5") { "" }
get("0") { "" }
get("8") { "" }
get("6") { "" }
get("1") { "" }
get("7") { "" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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.collections.map.benchmarks

import com.bloomberg.selekt.collections.map.FastLinkedStringMap
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 LinkedMapInput {
internal lateinit var smallMap: FastLinkedStringMap<Any>
internal lateinit var largeMap: FastLinkedStringMap<Any>
internal lateinit var largeAccessMap: FastLinkedStringMap<Any>

@Setup(Level.Iteration)
fun setUp() {
smallMap = FastLinkedStringMap(1, 1) {}
largeMap = FastLinkedStringMap(64, 64, false) {}
largeAccessMap = FastLinkedStringMap(64, 64, true) {}
}
}

open class FastLinkedStringMapBenchmark {
@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntry(input: LinkedMapInput) = input.smallMap.run {
getElsePut("1") { "" }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntries(input: LinkedMapInput) = input.largeMap.run {
getElsePut("1") { "" }
getElsePut("2") { "" }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntriesDifferentLengths(input: LinkedMapInput) = input.largeMap.run {
getElsePut("1") { "" }
getElsePut("23") { "" }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntriesAccessOrder(input: LinkedMapInput) = input.largeAccessMap.run {
getElsePut("1") { "" }
getElsePut("2") { "" }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntriesWithCollision(input: LinkedMapInput) = input.smallMap.run {
getElsePut("1") { "" }
getElsePut("2") { "" }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntriesDifferentLengthsWithCollision(input: LinkedMapInput) = input.smallMap.run {
getElsePut("1") { "" }
getElsePut("23") { "" }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getThenRemoveEntry(input: LinkedMapInput) = input.smallMap.run {
getElsePut("1") { "" }
removeEntry("1")
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getThenRemoveKey(input: LinkedMapInput) = input.smallMap.run {
getElsePut("1") { "" }
removeKey("1")
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getManyEntriesAccessOrder(input: LinkedMapInput) = input.largeAccessMap.run {
getEntryElsePut("0") { "" }
getEntryElsePut("1") { "" }
getEntryElsePut("2") { "" }
getEntryElsePut("3") { "" }
getEntryElsePut("4") { "" }
getEntryElsePut("5") { "" }
getEntryElsePut("6") { "" }
getEntryElsePut("7") { "" }
getEntryElsePut("8") { "" }
getEntryElsePut("9") { "" }
getEntryElsePut("2") { "" }
getEntryElsePut("3") { "" }
getEntryElsePut("9") { "" }
getEntryElsePut("4") { "" }
getEntryElsePut("5") { "" }
getEntryElsePut("0") { "" }
getEntryElsePut("8") { "" }
getEntryElsePut("6") { "" }
getEntryElsePut("1") { "" }
getEntryElsePut("7") { "" }
}
}
Loading
Loading