Skip to content

Commit 211f853

Browse files
Make examples/map subdirectory containing map examples. This allows for more subdirectories under examples if needed in future.
1 parent e1b30ff commit 211f853

File tree

8 files changed

+48
-22
lines changed

8 files changed

+48
-22
lines changed
File renamed without changes.

examples/map/settings.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
rootProject.name = "native_memory_allocator"
2+
3+
include("map")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.target.nativememoryallocator.examples.map
2+
3+
/**
4+
* A demo cache object containing a single field [s] of type [String].
5+
*/
6+
data class CacheObject(
7+
val s: String,
8+
)

examples/src/main/kotlin/com/target/nativememoryallocator/examples/OffHeapDemo.kt renamed to examples/map/src/main/kotlin/com/target/nativememoryallocator/examples/map/OffHeapDemo.kt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.target.nativememoryallocator.examples
1+
package com.target.nativememoryallocator.examples.map
22

33
import com.target.nativememoryallocator.allocator.NativeMemoryAllocatorBuilder
44
import com.target.nativememoryallocator.buffer.OnHeapMemoryBuffer
@@ -11,22 +11,15 @@ import kotlin.random.Random
1111

1212
private val logger = KotlinLogging.logger {}
1313

14-
private class CacheObjectSerializer : NativeMemoryMapSerializer<CacheObject> {
15-
16-
override fun deserializeFromOnHeapMemoryBuffer(onHeapMemoryBuffer: OnHeapMemoryBuffer): CacheObject {
17-
return CacheObject(
18-
s = String(onHeapMemoryBuffer.toTrimmedArray()),
19-
)
20-
}
21-
22-
override fun serializeToByteArray(value: CacheObject): ByteArray {
23-
return value.s.toByteArray()
24-
}
25-
26-
}
2714

2815
/**
2916
* Demo application that puts 20,000 [CacheObject] instances into a [NativeMemoryMap].
17+
*
18+
* Each [CacheObject] instances contains a random string of length 500KB.
19+
*
20+
* This is a total of 10 GB of data.
21+
*
22+
* [OnHeapDemo] is the same application using normal on-heap storage for comparison.
3023
*/
3124
class OffHeapDemo {
3225

@@ -90,6 +83,20 @@ class OffHeapDemo {
9083

9184
}
9285

86+
private class CacheObjectSerializer : NativeMemoryMapSerializer<CacheObject> {
87+
88+
override fun deserializeFromOnHeapMemoryBuffer(onHeapMemoryBuffer: OnHeapMemoryBuffer): CacheObject {
89+
return CacheObject(
90+
s = String(onHeapMemoryBuffer.toTrimmedArray()),
91+
)
92+
}
93+
94+
override fun serializeToByteArray(value: CacheObject): ByteArray {
95+
return value.s.toByteArray()
96+
}
97+
98+
}
99+
93100
suspend fun main() {
94101
withContext(Dispatchers.Default) {
95102
OffHeapDemo().run()

examples/src/main/kotlin/com/target/nativememoryallocator/examples/OnHeapDemo.kt renamed to examples/map/src/main/kotlin/com/target/nativememoryallocator/examples/map/OnHeapDemo.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.target.nativememoryallocator.examples
1+
package com.target.nativememoryallocator.examples.map
22

33
import kotlinx.coroutines.*
44
import mu.KotlinLogging
@@ -9,6 +9,13 @@ private val logger = KotlinLogging.logger {}
99

1010
/**
1111
* Demo application that puts 20,000 [CacheObject] instances into a [ConcurrentHashMap].
12+
*
13+
* Each [CacheObject] instances contains a random string of length 500KB.
14+
*
15+
* This is a total of 10 GB of data stored on the normal Java heap.
16+
*
17+
* This application does not make use of any features of the native_memory_allocator library.
18+
* It is provided for comparison with [OffHeapDemo].
1219
*/
1320
class OnHeapDemo {
1421

examples/src/main/kotlin/com/target/nativememoryallocator/examples/StringUtils.kt renamed to examples/map/src/main/kotlin/com/target/nativememoryallocator/examples/map/StringUtils.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
package com.target.nativememoryallocator.examples
1+
package com.target.nativememoryallocator.examples.map
22

33
private val charset = ('a'..'z') + ('A'..'Z') + ('0'..'9')
44

5+
/**
6+
* Build a random alphanumeric string.
7+
*
8+
* @param [length] random string length to build.
9+
* @return random string
10+
*/
511
fun buildRandomString(length: Int): String {
612
val stringBuilder = StringBuilder(length)
713
(0 until length).forEach { _ ->

examples/src/main/kotlin/com/target/nativememoryallocator/examples/CacheObject.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
rootProject.name = "native_memory_allocator"
22

3-
include("examples")
3+
include("examples:map")

0 commit comments

Comments
 (0)