Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/streaming_flv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,12 @@ impl StreamingFlvParser {
let data = array.to_vec();

if request_len <= CACHE_CHUNK_BYTES {
*self.cache.borrow_mut() = Some(CachedChunk { offset, data: data.clone() });
let ret = data[..length.min(data.len())].to_vec();
*self.cache.borrow_mut() = Some(CachedChunk { offset, data });
Ok(ret)
} else {
Ok(data)
}
Comment on lines 214 to 220
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This is a great optimization! You can further simplify the code by extracting the duplicated logic to create the return value. This avoids repeating the slicing and allocation logic in both branches of the if statement, making the code cleaner.

        let ret = data[..length.min(data.len())].to_vec();
        if request_len <= CACHE_CHUNK_BYTES {
            *self.cache.borrow_mut() = Some(CachedChunk { offset, data });
        }
        Ok(ret)


Ok(data[..length.min(data.len())].to_vec())
}

/// 解析 FLV 头部
Expand Down