Skip to content

Commit 47f79dd

Browse files
Merge pull request #24 from runetopic/jordan
Cache Library Speed and Memory Improvements
2 parents cad0f45 + a1dcdba commit 47f79dd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+545
-773
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ A cache library written in Kotlin.
2424
# Implementation
2525
Just use cache if you do not require any of the revision specific loaders.
2626
```groovy
27-
cache = { module = "com.runetopic.cache:cache", version.ref "1.4.19-SNAPSHOT" }
28-
loader = { module = "com.runetopic.cache:loader", version.ref "647.6.4-SNAPSHOT" }
27+
cache = { module = "com.runetopic.cache:cache", version.ref "2.0.0-SNAPSHOT" }
2928
```
3029

3130
```groovy

build.gradle.kts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
11
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
2+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3+
import org.jetbrains.kotlin.gradle.tasks.UsesKotlinJavaToolchain
24

5+
@Suppress("DSL_SCOPE_VIOLATION")
36
plugins {
4-
kotlin("jvm")
7+
alias(deps.plugins.jvm)
58
}
69

7-
configure(allprojects) {
10+
allprojects {
811
group = "com.runetopic.cache"
912

10-
plugins.withType<KotlinPluginWrapper> {
11-
java.sourceCompatibility = JavaVersion.VERSION_16
12-
java.targetCompatibility = JavaVersion.VERSION_16
13-
14-
tasks {
15-
compileKotlin {
16-
kotlinOptions.jvmTarget = "1.8"
17-
}
18-
compileTestKotlin {
19-
kotlinOptions.jvmTarget = "1.8"
20-
}
21-
}
13+
apply {
14+
plugin("org.jetbrains.kotlin.jvm")
2215
}
23-
}
2416

25-
configure(subprojects) {
2617
plugins.withType<KotlinPluginWrapper> {
2718
dependencies {
2819
implementation(kotlin("stdlib"))
2920
}
3021
}
22+
23+
tasks.withType<Test> {
24+
dependencies {
25+
testImplementation(kotlin("test"))
26+
}
27+
}
28+
29+
kotlin {
30+
jvmToolchain {
31+
languageVersion.set(JavaLanguageVersion.of(JavaVersion.VERSION_11.majorVersion))
32+
}
33+
}
34+
35+
java {
36+
toolchain {
37+
languageVersion.set(JavaLanguageVersion.of(JavaVersion.VERSION_11.majorVersion))
38+
}
39+
}
3140
}

cache/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
signing
55
}
66

7-
version = "1.6.0"
7+
version = "2.0.0-SNAPSHOT"
88

99
java {
1010
withJavadocJar()
@@ -72,5 +72,5 @@ dependencies {
7272
implementation("com.michael-bull.kotlin-inline-logger:kotlin-inline-logger:1.0.4")
7373
implementation("org.slf4j:slf4j-simple:2.0.4")
7474
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.14.0")
75-
implementation("com.runetopic.cryptography:cryptography:1.0.6-SNAPSHOT")
75+
implementation("com.runetopic.cryptography:cryptography:1.2.0-SNAPSHOT")
7676
}

cache/src/main/kotlin/com/runetopic/cache/codec/IFileCodec.kt renamed to cache/src/main/kotlin/com/runetopic/cache/codec/ArchiveCodec.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package com.runetopic.cache.codec
44
* @author Tyler Telis
55
* @email <xlitersps@gmail.com>
66
*/
7-
internal interface IFileCodec {
8-
fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray
7+
internal interface ArchiveCodec {
98
fun decompress(data: ByteArray, length: Int, keys: IntArray): ByteArray
109
}

cache/src/main/kotlin/com/runetopic/cache/codec/CodecType.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@ package com.runetopic.cache.codec
22

33
import com.runetopic.cache.codec.impl.BZip2Codec
44
import com.runetopic.cache.codec.impl.GZipCodec
5-
import com.runetopic.cache.codec.impl.NoFileCodec
65

76
/**
87
* @author Tyler Telis
98
* @email <xlitersps@gmail.com>
109
*
1110
* @author Jordan Abraham
1211
*/
13-
internal sealed class CodecType(
14-
val codec: IFileCodec
15-
) {
16-
object BadCodec : CodecType(NoFileCodec())
17-
object NoCodec : CodecType(NoFileCodec())
18-
object BZipCodec : CodecType(BZip2Codec())
19-
object GZipCodec : CodecType(GZipCodec())
12+
internal class CodecType {
13+
companion object {
14+
val bzip = BZip2Codec()
15+
val gzip = GZipCodec()
16+
}
2017
}

cache/src/main/kotlin/com/runetopic/cache/codec/Container.kt

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

cache/src/main/kotlin/com/runetopic/cache/codec/ContainerCodec.kt

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.runetopic.cache.codec
2+
3+
import java.nio.ByteBuffer
4+
5+
/**
6+
* @author Tyler Telis
7+
* @email <xlitersps@gmail.com>
8+
*/
9+
data class DecompressedArchive(
10+
val buffer: ByteBuffer,
11+
val compression: Int,
12+
val revision: Int,
13+
val crc: Int
14+
)

cache/src/main/kotlin/com/runetopic/cache/codec/Decompression.kt

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,23 @@
11
package com.runetopic.cache.codec.impl
22

3-
import com.runetopic.cache.codec.IFileCodec
3+
import com.runetopic.cache.codec.ArchiveCodec
44
import com.runetopic.cache.store.Constants.BZIP_HEADER
55
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream
6-
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream
76
import org.apache.commons.compress.utils.IOUtils
87
import java.io.ByteArrayInputStream
98
import java.io.ByteArrayOutputStream
10-
import java.io.InputStream
11-
import java.util.Arrays
9+
import java.lang.System.arraycopy
1210

1311
/**
1412
* @author Tyler Telis
1513
* @email <xlitersps@gmail.com>
1614
*/
17-
internal class BZip2Codec : IFileCodec {
18-
override fun compress(data: ByteArray, length: Int, keys: IntArray): ByteArray {
19-
val stream: InputStream = ByteArrayInputStream(data)
20-
val bout = ByteArrayOutputStream()
21-
BZip2CompressorOutputStream(bout, 1).use { os -> IOUtils.copy(stream, os) }
22-
23-
val buffer = bout.toByteArray()
24-
25-
assert(BZIP_HEADER[0] == buffer[0])
26-
assert(BZIP_HEADER[1] == buffer[1])
27-
assert(BZIP_HEADER[2] == buffer[2])
28-
assert(BZIP_HEADER[3] == buffer[3])
29-
30-
return Arrays.copyOfRange(buffer, BZIP_HEADER.size, buffer.size)
31-
}
32-
33-
override fun decompress(data: ByteArray, length: Int, keys: IntArray): ByteArray {
34-
val buffer = ByteArray(length + BZIP_HEADER.size)
35-
36-
System.arraycopy(BZIP_HEADER, 0, buffer, 0, BZIP_HEADER.size)
37-
System.arraycopy(data, 0, buffer, BZIP_HEADER.size, length)
38-
39-
val stream = ByteArrayOutputStream()
40-
BZip2CompressorInputStream(ByteArrayInputStream(buffer)).use { IOUtils.copy(it, stream) }
41-
return stream.toByteArray()
15+
internal class BZip2Codec : ArchiveCodec {
16+
override fun decompress(data: ByteArray, length: Int, keys: IntArray): ByteArray = with(ByteArray(length + BZIP_HEADER.size)) {
17+
arraycopy(BZIP_HEADER, 0, this, 0, BZIP_HEADER.size)
18+
arraycopy(data, 0, this, BZIP_HEADER.size, length)
19+
ByteArrayOutputStream()
20+
.apply { BZip2CompressorInputStream(ByteArrayInputStream(this@with)).use { IOUtils.copy(it, this) } }
21+
.toByteArray()
4222
}
4323
}

0 commit comments

Comments
 (0)