Compare commits

...

1 commit

Author SHA1 Message Date
fire-light43
274943c1a6
shared buffer to decrease alloc 2026-05-12 16:25:14 +00:00

View file

@ -804,6 +804,7 @@ object VideoDownloadManager {
private suspend fun resolve( private suspend fun resolve(
startByte: Long, startByte: Long,
endByte: Long?, endByte: Long?,
buffer: ByteArray,
callback: (suspend CoroutineScope.(LazyStreamDownloadResponse) -> Unit) callback: (suspend CoroutineScope.(LazyStreamDownloadResponse) -> Unit)
): Long = withContext(Dispatchers.IO) { ): Long = withContext(Dispatchers.IO) {
var currentByte: Long = startByte var currentByte: Long = startByte
@ -822,7 +823,6 @@ object VideoDownloadManager {
) )
val requestStream = request.body.byteStream() val requestStream = request.body.byteStream()
val buffer = ByteArray(bufferSize)
var read: Int var read: Int
try { try {
@ -853,6 +853,7 @@ object VideoDownloadManager {
suspend fun resolveSafe( suspend fun resolveSafe(
index: Int, index: Int,
retries: Int = 3, retries: Int = 3,
buffer: ByteArray,
callback: (suspend CoroutineScope.(LazyStreamDownloadResponse) -> Unit) callback: (suspend CoroutineScope.(LazyStreamDownloadResponse) -> Unit)
): Boolean { ): Boolean {
var start = chuckStartByte.getOrNull(index) ?: return false var start = chuckStartByte.getOrNull(index) ?: return false
@ -861,7 +862,7 @@ object VideoDownloadManager {
for (i in 0 until retries) { for (i in 0 until retries) {
try { try {
// in case // in case
start = resolve(start, end, callback) start = resolve(start, end, buffer, callback)
// no end defined, so we don't care exactly where it ended // no end defined, so we don't care exactly where it ended
if (end == null) return true if (end == null) return true
// we have download more or exactly what we needed // we have download more or exactly what we needed
@ -1158,7 +1159,10 @@ object VideoDownloadManager {
} }
} }
// this will take up the first available job and resolve // Reuse a download buffer to decrease unnecessary alloc
val buffer = ByteArray(items.bufferSize)
// This will take up the first available job and resolve
while (true) { while (true) {
if (!isActive) return@launch if (!isActive) return@launch
@ -1188,7 +1192,7 @@ object VideoDownloadManager {
// in case something has gone wrong set to failed if the fail is not caused by // in case something has gone wrong set to failed if the fail is not caused by
// user cancellation // user cancellation
if (!items.resolveSafe(index, callback = callback)) { if (!items.resolveSafe(index, buffer = buffer, callback = callback)) {
fileMutex.withLock { fileMutex.withLock {
if (metadata.type != DownloadType.IsStopped) { if (metadata.type != DownloadType.IsStopped) {
metadata.type = DownloadType.IsFailed metadata.type = DownloadType.IsFailed