瀏覽代碼

LibCompress: Make DeflateCompressor::write() use loop, not recursion

This is performance-neutral, but Instruments.app had a hard time
visualizing the very deeply nested stack frames here.

No behavior change.
Nico Weber 2 年之前
父節點
當前提交
dfb45705e6
共有 1 個文件被更改,包括 10 次插入8 次删除
  1. 10 8
      Userland/Libraries/LibCompress/Deflate.cpp

+ 10 - 8
Userland/Libraries/LibCompress/Deflate.cpp

@@ -477,16 +477,18 @@ ErrorOr<size_t> DeflateCompressor::write(ReadonlyBytes bytes)
 {
     VERIFY(!m_finished);
 
-    if (bytes.size() == 0)
-        return 0; // recursion base case
+    size_t total_written = 0;
+    while (!bytes.is_empty()) {
+        auto n_written = bytes.copy_trimmed_to(pending_block().slice(m_pending_block_size));
+        m_pending_block_size += n_written;
 
-    auto n_written = bytes.copy_trimmed_to(pending_block().slice(m_pending_block_size));
-    m_pending_block_size += n_written;
+        if (m_pending_block_size == block_size)
+            TRY(flush());
 
-    if (m_pending_block_size == block_size)
-        TRY(flush());
-
-    return n_written + TRY(write(bytes.slice(n_written)));
+        bytes = bytes.slice(n_written);
+        total_written += n_written;
+    }
+    return total_written;
 }
 
 bool DeflateCompressor::is_eof() const