Skip to content

Commit

Permalink
Use inline class for ngrams to reduce memory footprint (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
pemistahl committed May 16, 2022
1 parent 0f18387 commit 8f61113
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

@JvmInline
@Serializable(with = NgramSerializer::class)
internal data class Ngram(val value: String) : Comparable<Ngram> {
internal value class Ngram(val value: String) : Comparable<Ngram> {
init {
require(value.length in 0..5) {
"length of ngram '$value' is not in range 0..5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,17 @@ class LanguageDetectorTest {
// ngram probability lookup

private fun ngramProbabilityProvider() = listOf(
arguments(ENGLISH, Ngram("a"), 0.01),
arguments(ENGLISH, Ngram("lt"), 0.12),
arguments(ENGLISH, Ngram("ter"), 0.21),
arguments(ENGLISH, Ngram("alte"), 0.25),
arguments(ENGLISH, Ngram("alter"), 0.29),

arguments(GERMAN, Ngram("t"), 0.08),
arguments(GERMAN, Ngram("er"), 0.18),
arguments(GERMAN, Ngram("alt"), 0.22),
arguments(GERMAN, Ngram("lter"), 0.28),
arguments(GERMAN, Ngram("alter"), 0.30)
arguments(ENGLISH, "a", 0.01),
arguments(ENGLISH, "lt", 0.12),
arguments(ENGLISH, "ter", 0.21),
arguments(ENGLISH, "alte", 0.25),
arguments(ENGLISH, "alter", 0.29),

arguments(GERMAN, "t", 0.08),
arguments(GERMAN, "er", 0.18),
arguments(GERMAN, "alt", 0.22),
arguments(GERMAN, "lter", 0.28),
arguments(GERMAN, "alter", 0.30)
)

@ParameterizedTest
Expand Down

0 comments on commit 8f61113

Please sign in to comment.