diff options
| author | bptato <nincsnevem662@gmail.com> | 2024-09-19 17:46:27 +0200 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2024-09-22 22:44:53 +0200 |
| commit | 080493c058f52a5c20638f1b975d032af45f4d3f (patch) | |
| tree | 60e6ba6b3cb967d29d349018b3f315e7637b4b9e /src/io | |
| parent | e23fa780cf2fff7146efcd64b2806ce428858b80 (diff) | |
| download | chawan-080493c058f52a5c20638f1b975d032af45f4d3f.tar.gz | |
loader: mmap intermediate image files, misc refactoring
* refactor parseHeader
* optimize response blob()
* add direct "to cache" mode for loader requests which sets stdout to a
file, and use it for image processing
* move image resizing into a separate process
* mmap cache files in between processing steps when possible
At last, resize is no longer a part of image decoding. Also, it feels
much nicer to keep encoded image data in the same cache as everything
else.
The mmap operations *should* be more efficient than copying the whole
RGBA data through a pipe. In practice, it only makes a difference for
loading (well, now just mmapping) the encoded image into the pager,
where it singlehandedly speeds up image display by 10x on my test image.
For the other steps, the unfortunate fact that "tocache" must delay the
next fork/exec in the pipeline until the entire image is processed seems
to equal out any wins we might have gotten from skipping a single raw
RGBA copy.
I have tried moving the delay before the exec (it's possible with yet
another pipe), but it didn't help much and made the code much
uglier. (Not that tocache didn't, but I can live with this...)
Diffstat (limited to 'src/io')
| -rw-r--r-- | src/io/dynstream.nim | 82 | ||||
| -rw-r--r-- | src/io/tempfile.nim | 2 |
2 files changed, 83 insertions, 1 deletions
diff --git a/src/io/dynstream.nim b/src/io/dynstream.nim index 2bfa02f5..129ecf05 100644 --- a/src/io/dynstream.nim +++ b/src/io/dynstream.nim @@ -169,6 +169,88 @@ proc newPosixStream*(path: string; flags, mode: cint): PosixStream = return nil return newPosixStream(fd) +type + MaybeMappedMemory* = ptr MaybeMappedMemoryObj + + MaybeMappedMemoryObj = object + p0: pointer + p0len: int + fromMmap: bool + p*: ptr UncheckedArray[uint8] + len*: int + +# Read data of size "len", or mmap it if the stream is a file. +proc recvDataLoopOrMmap*(ps: PosixStream; len = -1): MaybeMappedMemory = + var stats: Stat + if fstat(ps.fd, stats) != -1 and S_ISREG(stats.st_mode): + let srcOff = lseek(ps.fd, 0, SEEK_CUR) # skip headers + if len != -1: + doAssert len == stats.st_size - srcOff + let len = int(stats.st_size) - srcOff + let p0 = mmap(nil, len, PROT_READ, MAP_SHARED, ps.fd, 0) + if p0 == MAP_FAILED: + return nil + let p1 = addr cast[ptr UncheckedArray[uint8]](p0)[srcOff] + let res = create(MaybeMappedMemoryObj) + res[] = MaybeMappedMemoryObj( + p0: p0, + p0len: int(stats.st_size), + p: cast[ptr UncheckedArray[uint8]](p1), + len: len, + fromMmap: true + ) + return res + let p = cast[ptr UncheckedArray[uint8]](alloc(len)) + ps.recvDataLoop(p, len) + let res = create(MaybeMappedMemoryObj) + res[] = MaybeMappedMemoryObj( + p0: p, + p0len: len, + p: p, + len: len, + fromMmap: false + ) + return res + +proc maybeMmapForSend*(ps: PosixStream; len: int): MaybeMappedMemory = + var stats: Stat + if fstat(0, stats) != -1 and S_ISREG(stats.st_mode): + let p0 = mmap(nil, len, PROT_WRITE, MAP_SHARED, ps.fd, 0) + if p0 == MAP_FAILED: + return nil + ps.seek(len - 1) + ps.sendDataLoop([char(0)]) + let res = create(MaybeMappedMemoryObj) + res[] = MaybeMappedMemoryObj( + p0: p0, + p0len: int(stats.st_size), + p: cast[ptr UncheckedArray[uint8]](p0), + len: len, + fromMmap: true + ) + return res + let p = cast[ptr UncheckedArray[uint8]](alloc(len)) + let res = create(MaybeMappedMemoryObj) + res[] = MaybeMappedMemoryObj( + p0: p, + p0len: len, + p: p, + len: len, + fromMmap: false + ) + return res + +proc sendDataLoop*(ps: PosixStream; mem: MaybeMappedMemory) = + # only send if not mmapped; otherwise everything is already where it should be + if not mem.fromMmap: + ps.sendDataLoop(mem.p, mem.len) + +proc dealloc*(mem: MaybeMappedMemory) = + if mem.fromMmap: + discard munmap(mem.p0, mem.p0len) + else: + dealloc(mem.p0) + type SocketStream* = ref object of PosixStream source*: Socket diff --git a/src/io/tempfile.nim b/src/io/tempfile.nim index 44c63297..132a12b1 100644 --- a/src/io/tempfile.nim +++ b/src/io/tempfile.nim @@ -1,6 +1,6 @@ import std/os -var tmpf_seq: int +var tmpf_seq = 0 proc getTempFile*(tmpdir: string; ext = ""): string = if not dirExists(tmpdir): createDir(tmpdir) |