Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Loop while there is work left to do based on whether we have finalized all chunks yet.
- while( NumChunksLeftToFinalize )
- {
- // If true we are waiting for async tasks to complete and should wait to complete some
- // if there are no async tasks finishing this iteration.
- bool bNeedToWaitForAsyncTask = false;
- // Try to kick off async tasks if there are chunks left to kick off.
- if( NumChunksLeftToKickOff )
- {
- // Find free index based on looking at uncompressed size. We can't use the thread counter
- // for this as that might be a chunk ready for finalization.
- int32 FreeIndex = INDEX_NONE;
- for( int32 i=0; i<MaxConcurrentAsyncChunks; i++ )
- {
- if( !AsyncChunkIndex[i] )
- {
- FreeIndex = i;
- check(AsyncChunks[FreeIndex].IsIdle()); // this is not supposed to be in use
- break;
- }
- }
- // Kick off async compression task if we found a chunk for it.
- if( FreeIndex != INDEX_NONE )
- {
- FAsyncCompressionChunk& NewChunk = AsyncChunks[FreeIndex].GetTask();
- // 2 times the uncompressed size should be more than enough; the compressed data shouldn't be that much larger
- NewChunk.CompressedSize = 2 * GSavingCompressionChunkSize;
- // Allocate compressed buffer placeholder on first use.
- if( NewChunk.CompressedBuffer == NULL )
- {
- NewChunk.CompressedBuffer = FMemory::Malloc( NewChunk.CompressedSize );
- }
- // By default everything is chunked up into GSavingCompressionChunkSize chunks.
- NewChunk.UncompressedSize = FMath::Min( BytesRemainingToKickOff, (int64)GSavingCompressionChunkSize );
- check(NewChunk.UncompressedSize>0);
- // Need to serialize source data if passed in pointer is an FArchive.
- if( bTreatBufferAsFileReader )
- {
- // Allocate memory on first use. We allocate the maximum amount to allow reuse.
- if( !NewChunk.UncompressedBuffer )
- {
- NewChunk.UncompressedBuffer = FMemory::Malloc(GSavingCompressionChunkSize);
- }
- ((FArchive*)V)->Serialize(NewChunk.UncompressedBuffer, NewChunk.UncompressedSize);
- }
- // Advance src pointer by amount to be compressed.
- else
- {
- NewChunk.UncompressedBuffer = SrcBuffer;
- SrcBuffer += NewChunk.UncompressedSize;
- }
- // Update status variables for tracking how much work is left, what to do next.
- BytesRemainingToKickOff -= NewChunk.UncompressedSize;
- AsyncChunkIndex[FreeIndex] = CurrentChunkIndex++;
- NewChunk.Flags = Flags;
- NumChunksLeftToKickOff--;
- AsyncChunks[FreeIndex].StartBackgroundTask();
- }
- // No chunks were available to use, complete some
- else
- {
- bNeedToWaitForAsyncTask = true;
- }
- }
- // Index of oldest chunk, needed as we need to serialize in order.
- int32 OldestAsyncChunkIndex = INDEX_NONE;
- for( int32 i=0; i<MaxConcurrentAsyncChunks; i++ )
- {
- check(AsyncChunkIndex[i] == 0 || AsyncChunkIndex[i] >= RetireChunkIndex);
- check(AsyncChunkIndex[i] < RetireChunkIndex + MaxConcurrentAsyncChunks);
- if (AsyncChunkIndex[i] == RetireChunkIndex)
- {
- OldestAsyncChunkIndex = i;
- }
- }
- check(OldestAsyncChunkIndex != INDEX_NONE); // the retire chunk better be outstanding
- bool ChunkReady;
- if (bNeedToWaitForAsyncTask)
- {
- // This guarantees that the async work has finished, doing it on this thread if it hasn't been started
- AsyncChunks[OldestAsyncChunkIndex].EnsureCompletion();
- ChunkReady = true;
- }
- else
- {
- ChunkReady = AsyncChunks[OldestAsyncChunkIndex].IsDone();
- }
- if (ChunkReady)
- {
- FAsyncCompressionChunk& DoneChunk = AsyncChunks[OldestAsyncChunkIndex].GetTask();
- // Serialize the data via archive.
- Serialize( DoneChunk.CompressedBuffer, DoneChunk.CompressedSize );
- // Update associated chunk.
- int64 CompressionChunkIndex = RetireChunkIndex++;
- check(CompressionChunkIndex<TotalChunkCount);
- CompressionChunks[CompressionChunkIndex].CompressedSize = DoneChunk.CompressedSize;
- CompressionChunks[CompressionChunkIndex].UncompressedSize = DoneChunk.UncompressedSize;
- // Keep track of total compressed size, stored in first chunk.
- CompressionChunks[0].CompressedSize += DoneChunk.CompressedSize;
- // Clean up chunk. Src and dst buffer are not touched as the contain allocations we keep till the end.
- AsyncChunkIndex[OldestAsyncChunkIndex] = 0;
- DoneChunk.CompressedSize = 0;
- DoneChunk.UncompressedSize = 0;
- // Finalized one :)
- NumChunksLeftToFinalize--;
- bNeedToWaitForAsyncTask = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment