Skip to content

Commit

Permalink
Optimised LRU cache.
Browse files Browse the repository at this point in the history
Signed-off-by: Kenneth J. Shackleton <[email protected]>
  • Loading branch information
kennethshackleton committed Jun 9, 2024
1 parent 2fbda51 commit c4edeb3
Show file tree
Hide file tree
Showing 15 changed files with 1,200 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ open class LruCacheBenchmark {
@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntry(input: CacheInput) = input.cache.run {
this["1", {}]
get("1") {}
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntryWithEviction(input: CacheInput) = input.cache.run {
this["1", {}]
this["2", {}]
get("1") {}
get("2") {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.FastStringMap
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 MapInput {
internal lateinit var map: FastStringMap<Any>

@Setup(Level.Iteration)
fun setUp() {
map = FastStringMap(1)
}
}

open class FastStringMapBenchmark {
@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntry(input: MapInput) = input.map.run {
getEntryElsePut("1") { "" }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntryWithCollision(input: MapInput) = input.map.run {
getEntryElsePut("1") { "" }
getEntryElsePut("2") { "" }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getThenRemoveEntry(input: MapInput) = input.map.run {
getEntryElsePut("1") { "" }
removeEntry("1")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.jdk.benchmarks

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 ArrayInput {
internal lateinit var array: Array<Any>

@Setup(Level.Iteration)
fun setUp() {
array = Array(2) { Any() }
}
}

open class ArrayBenchmark {
@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getFirst(input: ArrayInput) = input.array.run {
firstOrNull()
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntries(input: ArrayInput) = input.array.run {
firstOrNull()
this[1]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.jdk.benchmarks

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 ArrayListInput {
internal lateinit var list: ArrayList<Any>

@Setup(Level.Iteration)
fun setUp() {
list = ArrayList<Any>(1).apply {
add(Any())
add(Any())
}
}
}

open class ArrayListBenchmark {
@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getFirst(input: ArrayListInput) = input.list.run {
firstOrNull()
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntries(input: ArrayListInput) = input.list.run {
firstOrNull()
this[1]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.jdk.benchmarks

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 HashMapInput {
internal lateinit var smallMap: HashMap<String, Any>
internal lateinit var largeMap: HashMap<String, Any>

@Setup(Level.Iteration)
fun setUp() {
smallMap = HashMap(1)
largeMap = HashMap(64)
}
}

open class FastStringMapBenchmark {
@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntry(input: HashMapInput) = input.smallMap.run {
getOrPut("1") { "" }
}

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

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getEntryWithCollision(input: HashMapInput) = input.smallMap.run {
getOrPut("1") { "" }
getOrPut("2") { "" }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
fun getThenRemoveEntry(input: HashMapInput) = input.smallMap.run {
getOrPut("1") { "" }
remove("1")
}
}
Loading

0 comments on commit c4edeb3

Please sign in to comment.