-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds a pool for PresenceBitmap instances. #1044
Open
tgregg
wants to merge
1
commit into
ion-11-encoding-optimize-initial-expression-array-size-session-pools
Choose a base branch
from
ion-11-encoding-optimize-initial-expression-array-size-session-pools-merge-presencebitmap-pool
base: ion-11-encoding-optimize-initial-expression-array-size-session-pools
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,8 +3,13 @@ | |||||||||||||
package com.amazon.ion.impl.bin | ||||||||||||||
|
||||||||||||||
import com.amazon.ion.* | ||||||||||||||
import com.amazon.ion.impl.bin.PresenceBitmap.Companion.EXPRESSION | ||||||||||||||
import com.amazon.ion.impl.bin.PresenceBitmap.Companion.GROUP | ||||||||||||||
import com.amazon.ion.impl.bin.PresenceBitmap.Companion.VOID | ||||||||||||||
import com.amazon.ion.impl.macro.* | ||||||||||||||
import com.amazon.ion.impl.macro.Macro.* | ||||||||||||||
import com.amazon.ion.impl.macro.MacroEvaluator.* | ||||||||||||||
import java.util.* | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Utility class for setting, storing, reading, and writing presence bits. | ||||||||||||||
|
@@ -70,16 +75,40 @@ internal class PresenceBitmap( | |||||||||||||
|
||||||||||||||
const val MAX_SUPPORTED_PARAMETERS = PB_SLOTS_PER_LONG * 4 | ||||||||||||||
|
||||||||||||||
private val ZERO_PARAMETERS: PresenceBitmap = allRequired(0) | ||||||||||||||
private val ONE_REQUIRED_PARAMETER: PresenceBitmap = allRequired(1) | ||||||||||||||
private val TWO_REQUIRED_PARAMETERS: PresenceBitmap = allRequired(2) | ||||||||||||||
private val THREE_REQUIRED_PARAMETERS: PresenceBitmap = allRequired(3) | ||||||||||||||
private val FOUR_REQUIRED_PARAMETERS: PresenceBitmap = allRequired(4) | ||||||||||||||
private val ZERO_PARAMETERS: PresenceBitmap = allRequired(0) { PresenceBitmap() } | ||||||||||||||
private val ONE_REQUIRED_PARAMETER: PresenceBitmap = allRequired(1) { PresenceBitmap() } | ||||||||||||||
private val TWO_REQUIRED_PARAMETERS: PresenceBitmap = allRequired(2) { PresenceBitmap() } | ||||||||||||||
private val THREE_REQUIRED_PARAMETERS: PresenceBitmap = allRequired(3) { PresenceBitmap() } | ||||||||||||||
private val FOUR_REQUIRED_PARAMETERS: PresenceBitmap = allRequired(4) { PresenceBitmap() } | ||||||||||||||
|
||||||||||||||
/** Pool for PresenceBitmap instances. */ | ||||||||||||||
class PooledFactory { | ||||||||||||||
|
||||||||||||||
private var index = 0 | ||||||||||||||
|
||||||||||||||
private var pool: Array<PresenceBitmap?> = Array(32) { null } | ||||||||||||||
|
||||||||||||||
/** Gets an instance from the pool, allocating a new one only if necessary. The returned instance must be reset. */ | ||||||||||||||
fun get(): PresenceBitmap { | ||||||||||||||
if (index >= pool.size) { | ||||||||||||||
pool = pool.copyOf(pool.size * 2) | ||||||||||||||
} | ||||||||||||||
if (pool[index] == null) { | ||||||||||||||
pool[index] = PresenceBitmap() | ||||||||||||||
} | ||||||||||||||
return pool[index++]!! | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** Clears the pool. Calling this method invalidates all instances previously returned by [get]. */ | ||||||||||||||
fun clear() { | ||||||||||||||
index = 0 | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** Creates a PresenceBitmap for the given number of required parameters */ | ||||||||||||||
private fun allRequired(numberOfParameters: Int): PresenceBitmap { | ||||||||||||||
private inline fun allRequired(numberOfParameters: Int, supplier: () -> PresenceBitmap): PresenceBitmap { | ||||||||||||||
if (numberOfParameters > MAX_SUPPORTED_PARAMETERS) throw IonException("Macros with more than 128 parameters are not supported by this implementation.") | ||||||||||||||
val bitmap = PresenceBitmap(emptyList(), 0, numberOfParameters) | ||||||||||||||
val bitmap = supplier().reset(emptyList(), 0, numberOfParameters) | ||||||||||||||
for (i in 0 until numberOfParameters) { | ||||||||||||||
bitmap.setUnchecked(i, EXPRESSION) | ||||||||||||||
} | ||||||||||||||
|
@@ -88,22 +117,30 @@ internal class PresenceBitmap( | |||||||||||||
|
||||||||||||||
/** Creates or reuses a [PresenceBitmap] for the given signature. */ | ||||||||||||||
@JvmStatic | ||||||||||||||
fun create(signature: List<Parameter>): PresenceBitmap { | ||||||||||||||
fun create(signature: List<Parameter>, pool: PooledFactory): PresenceBitmap { | ||||||||||||||
if (signature.size > MAX_SUPPORTED_PARAMETERS) throw IonException("Macros with more than 128 parameters are not supported by this implementation.") | ||||||||||||||
// Calculate the actual number of presence bits that will be encoded for the given signature. | ||||||||||||||
val nonRequiredParametersCount = signature.count { it.cardinality != ParameterCardinality.ExactlyOne } | ||||||||||||||
val usePresenceBits = nonRequiredParametersCount > PRESENCE_BITS_SIZE_THRESHOLD || signature.any { it.type.taglessEncodingKind != null } | ||||||||||||||
var nonRequiredParametersCount = 0 | ||||||||||||||
var usePresenceBits = false | ||||||||||||||
for (i in signature.indices) { | ||||||||||||||
val parameter = signature[i] | ||||||||||||||
if (parameter.cardinality != ParameterCardinality.ExactlyOne) { | ||||||||||||||
nonRequiredParametersCount++ | ||||||||||||||
} | ||||||||||||||
usePresenceBits = usePresenceBits or (parameter.type.taglessEncodingKind != null) | ||||||||||||||
} | ||||||||||||||
usePresenceBits = usePresenceBits or (nonRequiredParametersCount > PRESENCE_BITS_SIZE_THRESHOLD) | ||||||||||||||
Comment on lines
+130
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
val size = if (usePresenceBits) nonRequiredParametersCount else 0 | ||||||||||||||
return if (size > 0) { | ||||||||||||||
PresenceBitmap(signature, nonRequiredParametersCount, signature.size) | ||||||||||||||
pool.get().reset(signature, nonRequiredParametersCount, signature.size) | ||||||||||||||
} else { | ||||||||||||||
when (signature.size) { | ||||||||||||||
0 -> ZERO_PARAMETERS | ||||||||||||||
1 -> ONE_REQUIRED_PARAMETER | ||||||||||||||
2 -> TWO_REQUIRED_PARAMETERS | ||||||||||||||
3 -> THREE_REQUIRED_PARAMETERS | ||||||||||||||
4 -> FOUR_REQUIRED_PARAMETERS | ||||||||||||||
else -> allRequired(signature.size) | ||||||||||||||
else -> allRequired(signature.size, pool::get) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
@@ -122,6 +159,21 @@ internal class PresenceBitmap( | |||||||||||||
val byteSize: Int | ||||||||||||||
get() = size divideByRoundingUp PB_SLOTS_PER_BYTE | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Resets this [PresenceBitmap] for the given signature, size, and parameter count. After this method is called, | ||||||||||||||
* callers must set the bitmap as necessary, such as by calling [readFrom]. | ||||||||||||||
*/ | ||||||||||||||
fun reset(signature: List<Parameter>, size: Int, totalParameterCount: Int): PresenceBitmap { | ||||||||||||||
this.signature = signature | ||||||||||||||
this.size = size | ||||||||||||||
this.totalParameterCount = totalParameterCount | ||||||||||||||
a = 0 | ||||||||||||||
b = 0 | ||||||||||||||
c = 0 | ||||||||||||||
d = 0 | ||||||||||||||
return this | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** Resets this [PresenceBitmap] for the given signature. */ | ||||||||||||||
fun initialize(signature: List<Parameter>) { | ||||||||||||||
if (signature.size > MAX_SUPPORTED_PARAMETERS) throw IonException("Macros with more than 128 parameters are not supported by this implementation.") | ||||||||||||||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The result is the same, but the new (uglier) version achieves two things:
ArrayList$Itr
allocations that are implicit to Kotlin'sList.count
andList.any
methods. These were showing up prominently in memory profiles. This change alone lowered allocation rate from 147 KB/op to 139 KB/op.