-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #457 from handymenny/optimize
Optimize space occupied by cache
- Loading branch information
Showing
3 changed files
with
63 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 29 additions & 16 deletions
45
src/main/java/it/smartphonecombo/uecapabilityparser/util/Intern.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,51 @@ | ||
package it.smartphonecombo.uecapabilityparser.util | ||
|
||
import it.smartphonecombo.uecapabilityparser.model.EmptyMimo | ||
import it.smartphonecombo.uecapabilityparser.model.Mimo | ||
import it.smartphonecombo.uecapabilityparser.model.bandwidth.Bandwidth | ||
import it.smartphonecombo.uecapabilityparser.model.bandwidth.EmptyBandwidth | ||
import it.smartphonecombo.uecapabilityparser.model.modulation.EmptyModulation | ||
import it.smartphonecombo.uecapabilityparser.model.modulation.Modulation | ||
import it.smartphonecombo.uecapabilityparser.model.band.IBandDetails | ||
import it.smartphonecombo.uecapabilityparser.model.combo.ICombo | ||
import it.smartphonecombo.uecapabilityparser.model.component.IComponent | ||
|
||
open class InternMap<T>(maxCapacity: Int) { | ||
@Transient private val lock = Any() | ||
|
||
private val internalMap: LinkedHashMap<T, T> = | ||
object : LinkedHashMap<T, T>(minOf(16, maxCapacity), 0.75f) { | ||
object : LinkedHashMap<T, T>(computeInitialCapacity(maxCapacity)) { | ||
override fun removeEldestEntry(eldest: Map.Entry<T, T>): Boolean { | ||
return size > maxCapacity | ||
} | ||
} | ||
|
||
private fun put(value: T): T { | ||
internalMap[value] = value | ||
synchronized(lock) { internalMap[value] = value } | ||
return value | ||
} | ||
|
||
fun intern(value: T): T = internalMap[value] ?: put(value) | ||
|
||
fun contains(value: T): Boolean = internalMap.contains(value) | ||
|
||
fun size() = internalMap.size | ||
|
||
companion object { | ||
private fun computeInitialCapacity(maxCapacity: Int): Int { | ||
// A value that ensures no re-hash is maxCapacity / 0.75 + 1 | ||
// Compute that value / 2 | ||
val initialCapacity = Math.floorDiv(maxCapacity * 2, 3) + 1 | ||
|
||
return maxOf(16, initialCapacity) | ||
} | ||
} | ||
} | ||
|
||
object MimoInternMap : InternMap<Mimo>(100) | ||
private object IBandDetailsInternMap : InternMap<IBandDetails>(1000) | ||
|
||
private object IComponentInternMap : InternMap<IComponent>(10000) | ||
|
||
object ModulationInternMap : InternMap<Modulation>(100) | ||
private object IComboInternMap : InternMap<ICombo>(100000) | ||
|
||
object BandwidthInternMap : InternMap<Bandwidth>(100) | ||
internal fun IBandDetails.intern() = IBandDetailsInternMap.intern(this) | ||
|
||
internal fun Mimo.intern(): Mimo = if (this == EmptyMimo) this else MimoInternMap.intern(this) | ||
internal fun IComponent.intern() = IComponentInternMap.intern(this) | ||
|
||
internal fun Modulation.intern() = | ||
if (this == EmptyModulation) this else ModulationInternMap.intern(this) | ||
internal fun ICombo.intern() = IComboInternMap.intern(this) | ||
|
||
internal fun Bandwidth.intern() = | ||
if (this == EmptyBandwidth) this else BandwidthInternMap.intern(this) | ||
internal fun ICombo.alreadyInterned() = IComboInternMap.contains(this) |
60 changes: 33 additions & 27 deletions
60
src/main/java/it/smartphonecombo/uecapabilityparser/util/Optimization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,48 @@ | ||
package it.smartphonecombo.uecapabilityparser.util | ||
|
||
import it.smartphonecombo.uecapabilityparser.extension.trimToSize | ||
import it.smartphonecombo.uecapabilityparser.extension.typedList | ||
import it.smartphonecombo.uecapabilityparser.model.Capabilities | ||
import it.smartphonecombo.uecapabilityparser.model.band.IBandDetails | ||
import it.smartphonecombo.uecapabilityparser.model.combo.ComboEnDc | ||
import it.smartphonecombo.uecapabilityparser.model.combo.ComboLte | ||
import it.smartphonecombo.uecapabilityparser.model.combo.ComboNr | ||
import it.smartphonecombo.uecapabilityparser.model.combo.ComboNrDc | ||
import it.smartphonecombo.uecapabilityparser.model.combo.ICombo | ||
import it.smartphonecombo.uecapabilityparser.model.component.ComponentNr | ||
import it.smartphonecombo.uecapabilityparser.model.component.IComponent | ||
|
||
internal fun Capabilities.optimize() { | ||
lteBands.forEach { it.optimize() } | ||
nrBands.forEach { it.optimize() } | ||
lteCombos.forEach { it.optimize() } | ||
nrCombos.forEach { it.optimize() } | ||
nrDcCombos.forEach { it.optimize() } | ||
enDcCombos.forEach { it.optimize() } | ||
} | ||
// bands | ||
lteBands = lteBands.map(IBandDetails::intern).typedList() | ||
nrBands = nrBands.map(IBandDetails::intern).typedList() | ||
|
||
internal fun IBandDetails.optimize() { | ||
mimoDL = mimoDL.intern() | ||
mimoUL = mimoUL.intern() | ||
modDL = modDL.intern() | ||
modUL = modUL.intern() | ||
// combos | ||
lteCombos = lteCombos.map(ICombo::optimizeAndIntern).typedList() | ||
nrCombos = nrCombos.map(ICombo::optimizeAndIntern).typedList() | ||
nrDcCombos = nrDcCombos.map(ICombo::optimizeAndIntern).typedList() | ||
enDcCombos = enDcCombos.map(ICombo::optimizeAndIntern).typedList() | ||
} | ||
|
||
internal fun ICombo.optimize() { | ||
masterComponents.trimToSize() | ||
masterComponents.forEach { it.optimize() } | ||
secondaryComponents.trimToSize() | ||
secondaryComponents.forEach { it.optimize() } | ||
private fun ICombo.optimizeAndIntern(): ICombo { | ||
val res = if (alreadyInterned()) this else optimized() | ||
return res.intern() | ||
} | ||
|
||
internal fun IComponent.optimize() { | ||
mimoDL = mimoDL.intern() | ||
mimoUL = mimoUL.intern() | ||
modDL = modDL.intern() | ||
modUL = modUL.intern() | ||
if (this is ComponentNr) { | ||
maxBandwidthDl = maxBandwidthDl.intern() | ||
maxBandwidthUl = maxBandwidthUl.intern() | ||
private fun ICombo.optimized(): ICombo { | ||
val masterComponentsOpt = masterComponents.map(IComponent::intern) | ||
val secondaryComponentsOpt = secondaryComponents.map(IComponent::intern) | ||
|
||
return when (this) { | ||
is ComboLte -> copy(masterComponents = masterComponentsOpt.typedList()) | ||
is ComboNr -> copy(masterComponents = masterComponentsOpt.typedList()) | ||
is ComboNrDc -> | ||
copy( | ||
masterComponents = masterComponentsOpt.typedList(), | ||
secondaryComponents = secondaryComponentsOpt.typedList(), | ||
) | ||
is ComboEnDc -> | ||
copy( | ||
masterComponents = masterComponentsOpt.typedList(), | ||
secondaryComponents = secondaryComponentsOpt.typedList(), | ||
) | ||
} | ||
} |