Skip to content
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

Reduce intermediate allocations when create Options #1421

Merged
merged 1 commit into from
Feb 5, 2024

Conversation

JakeWharton
Copy link
Member

No description provided.

@JakeWharton JakeWharton mentioned this pull request Feb 5, 2024
@@ -40,7 +40,7 @@ class Options private constructor(
// indexes to the caller's indexes.
val list = byteStrings.toMutableList()
list.sort()
val indexes = mutableListOf(*byteStrings.map { -1 }.toTypedArray())
val indexes = MutableList(list.size) { -1 }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be IntArray(list.size) { -1 }, but then the removeAt calls later wouldn't work. There's likely a way to change that logic, but it might be too much hassle.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I didn't want to get too invasive. You would have to track the working length separately and then do a shift to simulate the removeAt. At some point it's diminishing returns since this function is only meant to be called once and then the resulting object cached for long periods of time.

@JakeWharton JakeWharton merged commit 13e2f46 into master Feb 5, 2024
11 checks passed
@JakeWharton JakeWharton deleted the jw.reduce-allocations.2024-02-05 branch February 5, 2024 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants