Tweak the inlining of stream.write

This commit is contained in:
Zahary Karadjov 2020-05-05 19:23:28 +03:00
commit 0d90e444c2
No known key found for this signature in database
GPG key ID: C8936F8A3073D609

View file

@ -445,23 +445,22 @@ proc tryMovingToNextPage(c: var WriteCursor) =
# pre-allocated cursor span, which is considered a Defect (a range error) # pre-allocated cursor span, which is considered a Defect (a range error)
fsAssert false, "Attempt to write past the end of a cursor" fsAssert false, "Attempt to write past the end of a cursor"
template writeByteImpl(s: OutputStream, b: byte, awaiter, writeOp, drainOp: untyped) = template writeToNewSpanImpl(s: OutputStream, b: byte, awaiter, writeOp, drainOp: untyped) =
if atEnd(s.span): # Unsafe memory outputs don't use pages at all, so if our cursor
# Unsafe memory outputs don't use pages at all, so if our cursor # reached here, this is a range violation defect:
# reached here, this is a range violation defect: fsAssert canExtendOutput(s)
fsAssert canExtendOutput(s)
if s.vtable == nil or s.extCursorsCount > 0: if s.vtable == nil or s.extCursorsCount > 0:
# This is the main cursor of a stream, but we are either not # This is the main cursor of a stream, but we are either not
# ready to flush due to outstanding delayed writes or this is # ready to flush due to outstanding delayed writes or this is
# just a memory output stream. In both cases, we just need to # just a memory output stream. In both cases, we just need to
# allocate more memory and continue writing: # allocate more memory and continue writing:
addPage(s) addPage(s)
elif s.buffers == nil: elif s.buffers == nil:
awaiter s.vtable.writeOp(nil, unsafeAddr b, 1) awaiter s.vtable.writeOp(nil, unsafeAddr b, 1)
else: else:
trackWrittenToEnd(s.buffers) trackWrittenToEnd(s.buffers)
awaiter drainOp(s, nil, 0) awaiter drainOp(s, nil, 0)
writeByte(s.span, b) writeByte(s.span, b)
@ -474,8 +473,15 @@ proc write*(c: var WriteCursor, b: byte) =
writeByte(c.span, b) writeByte(c.span, b)
proc write*(s: OutputStream, b: byte) = proc writeToNewSpan(s: OutputStream, b: byte) =
writeByteImpl(s, b, noAwait, writeSync, drainAllBuffersSync) writeToNewSpanImpl(s, b, noAwait, writeSync, drainAllBuffersSync)
template write*(sp: OutputStream, b: byte) =
let s = sp
if hasRunway(s.span):
writeByte(s.span, b)
else:
writeToNewSpan(s, b)
proc write*(sp: AsyncOutputStream, b: byte) = proc write*(sp: AsyncOutputStream, b: byte) =
let s = OutputStream sp let s = OutputStream sp
@ -484,8 +490,11 @@ proc write*(sp: AsyncOutputStream, b: byte) =
writeByte(s.span, b) writeByte(s.span, b)
template writeAndWait*(sp: AsyncOutputStream, b: byte) = template writeAndWait*(sp: AsyncOutputStream, b: byte) =
let s = sp let s = OutputStream sp
writeByteImpl(s, b, fsAwait, writeAsync, drainAllBuffersAsync) if hasRunway(s.span):
writeByte(s.span, b)
else:
writeToNewSpanImpl(s, b, fsAwait, writeAsync, drainAllBuffersAsync)
template write*(s: AsyncOutputStream, x: char) = template write*(s: AsyncOutputStream, x: char) =
write s, byte(x) write s, byte(x)