Skip to content

Commit 140ec3d

Browse files
feat: Add forceFlush method to FlateWriter for improved data handling
- Introduced forceFlush method to ensure all buffered data is output immediately. - Updated flushOutput method to only flush when the buffer exceeds 256KB, optimizing system calls. - Modified storeFast and close methods to utilize forceFlush for final data output. Log: Enhance data flushing mechanisms in FlateWriter
1 parent a53da92 commit 140ec3d

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

3rdparty/pzip/include/pzip/fast_deflate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ class FlateWriter {
500500
void storeFast();
501501
size_t fillBlock(const uint8_t* data, size_t size);
502502
void flushOutput();
503+
void forceFlush();
503504

504505
// 获取当前使用的编码器
505506
FastGen* encoder() const { return useL1_ ? static_cast<FastGen*>(encoderL1_.get())

3rdparty/pzip/src/fast_deflate.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,8 +1170,18 @@ size_t FlateWriter::fillBlock(const uint8_t* data, size_t size) {
11701170
return n;
11711171
}
11721172

1173-
// 将 writer_ 中的数据刷新到输出目标
1173+
// 将 writer_ 中的数据刷新到输出目标(当缓冲区足够大时)
11741174
void FlateWriter::flushOutput() {
1175+
auto& buf = writer_->data();
1176+
// 只在缓冲区超过 256KB 时才刷新,减少系统调用次数
1177+
if (buf.size() >= 256 * 1024 && output_) {
1178+
output_(buf.data(), buf.size());
1179+
buf.clear();
1180+
}
1181+
}
1182+
1183+
// 强制刷新所有数据
1184+
void FlateWriter::forceFlush() {
11751185
auto& buf = writer_->data();
11761186
if (!buf.empty() && output_) {
11771187
output_(buf.data(), buf.size());
@@ -1212,7 +1222,7 @@ void FlateWriter::storeFast() {
12121222

12131223
tokens_.reset();
12141224
windowEnd_ = 0;
1215-
flushOutput(); // 立即刷新到输出
1225+
flushOutput();
12161226
}
12171227

12181228
// 参照 Go compressor.write
@@ -1247,7 +1257,7 @@ void FlateWriter::close() {
12471257
}
12481258

12491259
writer_->flush();
1250-
flushOutput(); // 最后刷新
1260+
forceFlush(); // 强制刷新所有剩余数据
12511261
}
12521262

12531263
} // namespace pzip

0 commit comments

Comments
 (0)