-
-
Notifications
You must be signed in to change notification settings - Fork 58
fix: Bound live build output memory #1642
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
Open
mvanhorn
wants to merge
2
commits into
appdevforall:stage
Choose a base branch
from
mvanhorn:fix/1367-bound-build-output-memory
base: stage
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
165 changes: 165 additions & 0 deletions
165
app/src/main/java/com/itsaky/androidide/fragments/output/BuildOutputBuffer.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| /* | ||
| * This file is part of AndroidIDE. | ||
| * | ||
| * AndroidIDE is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * AndroidIDE is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.itsaky.androidide.fragments.output | ||
|
|
||
| import kotlinx.coroutines.channels.Channel | ||
|
|
||
| /** | ||
| * Thread-safe pending build output with fixed memory and batch budgets. | ||
| * | ||
| * Inputs are indivisible unless one exceeds [maxPendingChars], in which case only its newest tail | ||
| * is retained. Older pending output is evicted first, while batches preserve the retained order. | ||
| */ | ||
| internal class BuildOutputBuffer( | ||
| private val maxPendingChars: Int = DEFAULT_MAX_PENDING_CHARS, | ||
| private val maxBatchChars: Int = DEFAULT_MAX_BATCH_CHARS, | ||
| ) { | ||
| data class Batch( | ||
| val text: String, | ||
| val sessionToken: Int, | ||
| val sourceChars: Int, | ||
| ) | ||
|
|
||
| private sealed interface Entry { | ||
| val sessionToken: Int | ||
|
|
||
| data class Text( | ||
| val value: String, | ||
| override val sessionToken: Int, | ||
| ) : Entry | ||
|
|
||
| data class Omission( | ||
| var lineCount: Long, | ||
| var sourceChars: Int, | ||
| override val sessionToken: Int, | ||
| ) : Entry | ||
| } | ||
|
|
||
| private val entries = ArrayDeque<Entry>() | ||
| private val available = Channel<Unit>(Channel.CONFLATED) | ||
| private val lock = Any() | ||
| private var retainedChars = 0 | ||
|
|
||
| internal val pendingChars: Int | ||
| get() = synchronized(lock) { retainedChars } | ||
|
|
||
| init { | ||
| require(maxPendingChars > 0) { "maxPendingChars must be positive" } | ||
| require(maxBatchChars > 0) { "maxBatchChars must be positive" } | ||
| } | ||
|
|
||
| fun offer( | ||
| text: String, | ||
| sessionToken: Int, | ||
| ) { | ||
| if (text.isEmpty()) return | ||
| val normalized = if (text.endsWith('\n')) text else "$text\n" | ||
| synchronized(lock) { | ||
| val existingOmission = entries.firstOrNull() as? Entry.Omission | ||
| if (existingOmission != null) entries.removeFirst() | ||
|
|
||
| var retained = normalized | ||
| var omittedLines = existingOmission?.lineCount ?: 0 | ||
| var omittedChars = existingOmission?.sourceChars ?: 0 | ||
| if (retained.length > maxPendingChars) { | ||
| val droppedPrefix = retained.dropLast(maxPendingChars) | ||
| omittedLines += lineCount(droppedPrefix) | ||
| omittedChars = saturatedAdd(omittedChars, droppedPrefix.length) | ||
| retained = retained.takeLast(maxPendingChars) | ||
| } | ||
| while (retainedChars > maxPendingChars - retained.length) { | ||
| val evicted = entries.removeFirst() as Entry.Text | ||
| retainedChars -= evicted.value.length | ||
| omittedLines += lineCount(evicted.value) | ||
| omittedChars = saturatedAdd(omittedChars, evicted.value.length) | ||
| } | ||
|
|
||
| if (omittedLines > 0) { | ||
| entries.addFirst(Entry.Omission(omittedLines, omittedChars, sessionToken)) | ||
| } | ||
| entries.addLast(Entry.Text(retained, sessionToken)) | ||
| retainedChars += retained.length | ||
| available.trySend(Unit) | ||
| } | ||
| } | ||
|
|
||
| suspend fun takeBatch(): Batch { | ||
| while (true) { | ||
| available.receive() | ||
| val batch = synchronized(lock) { takeAvailableBatch() } | ||
| if (batch != null) return batch | ||
| } | ||
| } | ||
|
|
||
| fun clear() { | ||
| synchronized(lock) { | ||
| entries.clear() | ||
| retainedChars = 0 | ||
| while (available.tryReceive().isSuccess) { | ||
| // Discard stale wakeups from the cleared build session. | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun takeAvailableBatch(): Batch? { | ||
| if (entries.isEmpty()) return null | ||
| val sessionToken = entries.first().sessionToken | ||
| val batch = StringBuilder(minOf(retainedChars, maxBatchChars)) | ||
| var sourceChars = 0 | ||
| while (entries.isNotEmpty()) { | ||
| val entry = entries.first() | ||
| if (entry.sessionToken != sessionToken) break | ||
| val value = | ||
| when (entry) { | ||
| is Entry.Text -> entry.value | ||
| is Entry.Omission -> omissionMarker(entry.lineCount) | ||
| } | ||
| if (batch.isNotEmpty() && batch.length + value.length > maxBatchChars) break | ||
|
|
||
| entries.removeFirst() | ||
| batch.append(value) | ||
| when (entry) { | ||
| is Entry.Text -> { | ||
| retainedChars -= entry.value.length | ||
| sourceChars = saturatedAdd(sourceChars, entry.value.length) | ||
| } | ||
| is Entry.Omission -> sourceChars = saturatedAdd(sourceChars, entry.sourceChars) | ||
| } | ||
| } | ||
| if (entries.isNotEmpty()) available.trySend(Unit) | ||
| return Batch(batch.toString(), sessionToken, sourceChars) | ||
| } | ||
|
|
||
| private fun lineCount(text: String): Long = | ||
| text.count { it == '\n' }.toLong() + if (text.endsWith('\n')) 0 else 1 | ||
|
|
||
| private fun saturatedAdd( | ||
| left: Int, | ||
| right: Int, | ||
| ): Int = (left.toLong() + right).coerceAtMost(Int.MAX_VALUE.toLong()).toInt() | ||
|
|
||
| private fun omissionMarker(lineCount: Long): String { | ||
| val noun = if (lineCount == 1L) "line" else "lines" | ||
| return "[$lineCount build output $noun omitted]\n" | ||
| } | ||
|
|
||
| companion object { | ||
| private const val DEFAULT_MAX_PENDING_CHARS = 256 * 1024 | ||
| private const val DEFAULT_MAX_BATCH_CHARS = 32 * 1024 | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
F09 follow-up — could you move this string to
strings.xmlbefore merge?The pluralization fixes "1 build output lines omitted", thanks. The localization half is still open, and it is cheaper to close now than when I filed it: because
appendOutputpersists to the session file before offering to the buffer (BuildOutputFragment.kt:398-403), the marker no longer reachesbuild_output_session.txt, share/copy, orBuildOutputProvider. It is editor-only text now, so there is no stable on-disk form to protect and the whole string can be localized.Home for it:
resources/src/main/res/values/strings.xml, next tomsg_no_filter_matches(line 689).One thing to know before you pick a form: this repo currently has no
<plurals>resource and nogetQuantityStringcall anywhere. So a<plurals name="msg_build_output_lines_omitted">is the correct Android mechanism and I'd take it, but it would be the first, and it lands in 12values-*dirs. If you'd rather not be the one to introduce that, a plain<string>with a%1$dplaceholder and your existing singular/plural branch kept in code is fine by me — the point of the finding is that the text stops being a Kotlin literal, not which resource type carries it.Two ways to keep
BuildOutputBufferfree of aContextand unit-testable — your call:formatOmission: (Long) -> String, defaulted to the current literal for tests; the fragment passes the resolved-string lambda.omittedLines: LongtoBatchand format on Main influshToEditor. AnOmissionis only ever added withaddFirstand there is at most one, so when a batch contains a marker it is always at index 0 ofbatch.text— prepending in the fragment preserves order.