Repository navigation
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
integerArtCacheAllocis documented as measuring "the cost of allocating a boxed integer that takes advantage of ART's cache", but it never reaches the cache.The test creates its values with the
Integerconstructor (Integer(i.toInt() + 1)), which always allocates a new object.Only
Integer.valueOf()/ autoboxing can return a cached instance.The test body has been written this way since the test was added.
Replacing the constructor with plain autoboxing inside the
measureRepeatedblock is not enough with the current benchmark library.With androidx.benchmark 1.4.0 the block is compiled into a suspend lambda, and in coroutine code the Kotlin compiler boxes primitives through
kotlin.coroutines.jvm.internal.Boxing(https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/src/kotlin/coroutines/jvm/internal/boxing.kt), which calls theIntegerconstructor instead ofInteger.valueOf().Boxing done directly in the block therefore still allocates on every iteration.
This PR moves the boxing into two regular (non-inline) helper functions, where the compiler emits
Integer.valueOf(), and adds a comment explaining why they have to stay that way.Verification
Allocations per iteration (
allocationCountmedian from the benchmark JSON), Samsung Galaxy S24+ (SM-S926B), Android 17,compilationMode = speed, Kotlin 2.2.0 (2.1.10 for the 1.3.3 rows):Integer(...))Integer(...))The 1.3.3 rows were measured on c0b3985; the 1.4.0 rows on current
main.Each row is a single run.
The bytecode matches the numbers (
javap -c):Integer.valueOf()...$$inlined$measureRepeated$1.invokeSuspendand compiles toBoxing.boxInt()To reproduce:
maincurrently does not compile (see #373), and on this device the tests do not start without setting a target SDK for the test APK (see #374).Both fixes were applied locally for every run, together with
androidx.benchmark.profiling.mode=None.