diff options
author | bptato <nincsnevem662@gmail.com> | 2023-09-23 00:05:02 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-09-23 00:05:02 +0200 |
commit | aa8f96765d1ddd85d0273d01cc9524514b6fe21f (patch) | |
tree | 846e1c81cda2ad5973d2b59f030ad491dd2eb921 /src/loader/loaderhandle.nim | |
parent | 1ef033b1025f818a8b5875a51cf019e41f11f767 (diff) | |
download | chawan-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/loader/loaderhandle.nim')
-rw-r--r-- | src/loader/loaderhandle.nim | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/loader/loaderhandle.nim b/src/loader/loaderhandle.nim index 2c999813..d8d01bb2 100644 --- a/src/loader/loaderhandle.nim +++ b/src/loader/loaderhandle.nim @@ -1,6 +1,7 @@ import net import streams +import io/multistream import io/posixstream import io/serialize import io/socketstream @@ -13,6 +14,7 @@ type LoaderHandle* = ref object # conditions that would be difficult to untangle. canredir: bool sostream: Stream # saved ostream when redirected + sostream_suspend: Stream # saved ostream when suspended # Create a new loader handle, with the output stream ostream. proc newLoaderHandle*(ostream: Stream, canredir: bool): LoaderHandle = @@ -21,6 +23,22 @@ proc newLoaderHandle*(ostream: Stream, canredir: bool): LoaderHandle = proc getFd*(handle: LoaderHandle): int = return int(SocketStream(handle.ostream).source.getFd()) +proc addOutputStream*(handle: LoaderHandle, stream: Stream) = + if likely(handle.sostream_suspend != nil): + let ms = newMultiStream(handle.sostream_suspend, stream) + handle.sostream_suspend = ms + else: + # In buffer, addOutputStream is used as follows: + # * suspend handle + # * tee handle (-> call addOutputStream) + # * resume handle + # This means that this code path will never be executed, as + # sostream_suspend is never nil when the function is called. + # (Feel free to remove this assertion if this changes.) + doAssert false + let ms = newMultiStream(handle.ostream, stream) + handle.ostream = ms + proc sendResult*(handle: LoaderHandle, res: int): bool = try: handle.ostream.swrite(res) @@ -62,6 +80,17 @@ proc sendData*(handle: LoaderHandle, s: string): bool = return handle.sendData(unsafeAddr s[0], s.len) return true +proc suspend*(handle: LoaderHandle) = + handle.sostream_suspend = handle.ostream + handle.ostream = newStringStream() + +proc resume*(handle: LoaderHandle) = + let ss = handle.ostream + handle.ostream = handle.sostream_suspend + handle.sostream_suspend = nil + discard handle.sendData(ss.readAll()) + ss.close() + proc close*(handle: LoaderHandle) = if handle.sostream != nil: try: |