about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-09-23 00:05:02 +0200
committerbptato <nincsnevem662@gmail.com>2023-09-23 00:05:02 +0200
commitaa8f96765d1ddd85d0273d01cc9524514b6fe21f (patch)
tree846e1c81cda2ad5973d2b59f030ad491dd2eb921 /src/io
parent1ef033b1025f818a8b5875a51cf019e41f11f767 (diff)
downloadchawan-aa8f96765d1ddd85d0273d01cc9524514b6fe21f.tar.gz
buffer: make clone fork()
Makes e.g. on-page anchor navigation near-instantaneous. Well, as
instantaneous as a fork can be. In any case, it's a lot faster
than loading the entire page anew.

This involves duplicating open resources (file descriptors, etc.),
which is not exactly trivial. For now we have a huge clone() procedure
that does an ok-ish job at it, but there remains a lot of room for
improvement.

e.g. cloning is still broken in some cases:

* As noted in the comments, TeeStream'ing the input stream for any
  buffer is a horrible idea, as readout in the cloned buffer now
  depends on the original buffer also reading from the stream. (So
  e.g. if you clone, then kill the old buffer without waiting for
  the new one to load, the new buffer gets stuck.)
* Timeouts/intervals are broken in cloned buffers. The timeout
  module probably needs a redesign to fix this.
* If you clone before connect2, the cloned buffer gets stuck.

The previous solution was even worse (i.e. broken in more cases),
so this is still an improvement. For example, this fixes some issues
with mailcap handling (removes the "set the Content-Type of htmloutput
buffers to text/html" hack), does not reload all resources, does not
completely break if the buffer is cloned during loading, etc.
Diffstat (limited to 'src/io')
-rw-r--r--src/io/multistream.nim27
-rw-r--r--src/io/teestream.nim3
2 files changed, 30 insertions, 0 deletions
diff --git a/src/io/multistream.nim b/src/io/multistream.nim
new file mode 100644
index 00000000..33038ad6
--- /dev/null
+++ b/src/io/multistream.nim
@@ -0,0 +1,27 @@
+# MultiStream: write to several streams at once when writing to a single
+# stream.
+# See TeeStream for a pull version.
+
+import streams
+
+type MultiStream = ref object of Stream
+  s1: Stream
+  s2: Stream
+
+proc tsClose(s: Stream) =
+  let s = cast[MultiStream](s)
+  s.s1.close()
+  s.s2.close()
+
+proc msWriteData(s: Stream, buffer: pointer, bufLen: int) =
+  let s = cast[MultiStream](s)
+  s.s1.writeData(buffer, bufLen)
+  s.s2.writeData(buffer, bufLen)
+
+proc newMultiStream*(s1, s2: Stream, closedest = true): MultiStream =
+  return MultiStream(
+    s1: s1,
+    s2: s2,
+    closeImpl: tsClose,
+    writeDataImpl: msWriteData
+  )
diff --git a/src/io/teestream.nim b/src/io/teestream.nim
index 81c9e2f0..a8f5792e 100644
--- a/src/io/teestream.nim
+++ b/src/io/teestream.nim
@@ -1,3 +1,6 @@
+# TeeStream: write to another stream when reading from one stream.
+# See MultiStream for a push version.
+
 import streams
 
 type TeeStream = ref object of Stream