Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 37 additions & 21 deletions sjsonnet/src/sjsonnet/ByteRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class ByteRenderer(out: OutputStream = new java.io.ByteArrayOutputStream(), inde
obj: Val.Obj,
matDepth: Int,
ctx: Materializer.MaterializeContext)(implicit evaluator: EvalScope): Unit = {
if (!ctx.enterObject(obj))
if (!ctx.enterContainer(obj))
Error.fail("Stackoverflow while materializing, possibly due to recursive value", obj.pos)
try {
obj.triggerAllAsserts(ctx.brokenAssertionLogic)
Expand All @@ -287,7 +287,7 @@ class ByteRenderer(out: OutputStream = new java.io.ByteArrayOutputStream(), inde
materializeDirectGenericObj(obj, matDepth, ctx)
}
} finally {
ctx.exitObject(obj)
ctx.exitContainer(obj)
}
}

Expand Down Expand Up @@ -495,15 +495,10 @@ class ByteRenderer(out: OutputStream = new java.io.ByteArrayOutputStream(), inde
ctx: Materializer.MaterializeContext)(implicit evaluator: EvalScope): Unit = {
val len = xs.length

// Inline of visitArray — open bracket
elemBuilder.append('[')
newlineBuffered = true
depth += 1
resetEmpty()

// Fast paths for compact numeric arrays: emit numbers directly without per-element dispatch.
xs match {
case range: Val.RangeArr if range.isCompactRange =>
openArrBracket()
var i = 0
while (i < len) {
markNonEmpty()
Expand All @@ -512,7 +507,9 @@ class ByteRenderer(out: OutputStream = new java.io.ByteArrayOutputStream(), inde
commaBuffered = true
i += 1
}
closeArrBracket()
case ba: Val.ByteArr =>
openArrBracket()
val bytes = ba.rawBytes
var i = 0
while (i < len) {
Expand All @@ -522,23 +519,42 @@ class ByteRenderer(out: OutputStream = new java.io.ByteArrayOutputStream(), inde
commaBuffered = true
i += 1
}
closeArrBracket()
case _ =>
var i = 0
while (i < len) {
val childVal = xs.value(i)

markNonEmpty()
flushBuffer()

// Render element directly — no flush overhead
materializeChild(childVal, matDepth, ctx)

commaBuffered = true
i += 1
if (!ctx.enterContainer(xs))
Error.fail("Stackoverflow while materializing, possibly due to recursive value", xs.pos)
try {
openArrBracket()
var i = 0
while (i < len) {
val childVal = xs.value(i)

markNonEmpty()
flushBuffer()

// Render element directly — no flush overhead
materializeChild(childVal, matDepth, ctx)

commaBuffered = true
i += 1
}
closeArrBracket()
} finally {
ctx.exitContainer(xs)
}
}
}

/** Open an array bracket and initialize depth/empty state. */
@inline private def openArrBracket(): Unit = {
elemBuilder.append('[')
newlineBuffered = true
depth += 1
resetEmpty()
}

// Inline of visitEnd — close bracket
/** Close an array bracket, handling empty vs non-empty formatting. */
@inline private def closeArrBracket(): Unit = {
commaBuffered = false
newlineBuffered = false
val wasEmpty = isEmpty
Expand Down
Loading
Loading