Optimised config hashing

This commit is contained in:
Auxilor
2023-07-20 12:19:10 +01:00
parent f1bef38046
commit 27da03e8db
4 changed files with 11 additions and 18 deletions

View File

@@ -16,11 +16,16 @@ open class EcoConfig(
private val values = ConcurrentHashMap<String, Any?>()
@Transient
var injections = ConcurrentHashMap<String, InjectablePlaceholder>()
private val injections = mutableMapOf<String, InjectablePlaceholder>()
fun init(values: Map<String, Any?>) {
@Transient
private var injectionHash = 0
fun init(values: Map<String, Any?>, injections: Map<String, InjectablePlaceholder>) {
this.values.clear()
this.values.putAll(values.normalizeToConfig(this.type))
this.addInjectablePlaceholder(injections.values)
}
override fun toPlaintext(): String {
@@ -179,6 +184,7 @@ open class EcoConfig(
override fun addInjectablePlaceholder(placeholders: Iterable<InjectablePlaceholder>) {
for (placeholder in placeholders) {
injections[placeholder.pattern.pattern()] = placeholder
injectionHash = injectionHash xor placeholder.hashCode()
}
}
@@ -239,18 +245,6 @@ open class EcoConfig(
}
override fun hashCode(): Int {
/*
The keys are completely redundant, as they are only used to prevent
duplicate keys in the map. Therefore, we can ignore them and just
hash the actual placeholder values.
*/
var injectionHash = 0
injections.forEachValue(5) {
injectionHash = injectionHash xor (it.hashCode() shl 5)
}
// hashCode() has to compute extremely quickly, so we're using bitwise, because why not?
// Fucking filthy to use identityHashCode here, but it should be extremely fast
val identityHash = System.identityHashCode(this)

View File

@@ -10,7 +10,6 @@ class EcoConfigSection(
injections: Map<String, InjectablePlaceholder> = emptyMap()
) : EcoConfig(type) {
init {
this.init(values)
this.injections = ConcurrentHashMap(injections)
this.init(values, injections)
}
}

View File

@@ -91,7 +91,7 @@ open class EcoLoadableConfig(
protected fun init(reader: Reader) {
val string = reader.readToString()
makeHeader(string)
super.init(type.toMap(string))
super.init(type.toMap(string), emptyMap())
}
fun init(file: File) {

View File

@@ -50,7 +50,7 @@ open class EcoUpdatableConfig(
val reader = BufferedReader(InputStreamReader(newIn, StandardCharsets.UTF_8))
val config = EcoConfigSection(type, emptyMap())
config.init(type.toMap(reader.readToString()))
config.init(type.toMap(reader.readToString()), emptyMap())
return config
}