revert: remove safeToString_ and recursive toString handling due to memory leak on runtime errors
The safeToString_ mechanism introduced in 40897f33 used per-fiber maps and lists to detect recursive toString calls, but leaked memory when runtime errors occurred before cleanup. This reverts to direct toString implementations for List and Map, removing the recursive detection logic and updating tests to mark self-referencing containers as TODO.
This commit is contained in:
+9
-39
@@ -127,34 +127,6 @@ class WhereSequence is Sequence {
|
||||
}
|
||||
|
||||
class String is Sequence {
|
||||
// Avoids recursively calling [toString] on [object] and overflowing the
|
||||
// stack.
|
||||
//
|
||||
// If we are already within a call to [safeToString_] on [object] in this
|
||||
// fiber, then this returns "...". Otherwise, it returns the result of
|
||||
// calling [ifUnseen].
|
||||
static safeToString_(object, ifUnseen) {
|
||||
if (__seenByFiber == null) __seenByFiber = new Map
|
||||
var seen = __seenByFiber[Fiber.current]
|
||||
if (seen == null) {
|
||||
__seenByFiber[Fiber.current] = seen = new List
|
||||
}
|
||||
|
||||
// See if we are recursing on it.
|
||||
for (outer in seen) {
|
||||
if (Object.same(outer, object)) return "..."
|
||||
}
|
||||
|
||||
seen.add(object)
|
||||
|
||||
var result = ifUnseen.call()
|
||||
|
||||
seen.removeAt(-1)
|
||||
if (seen.count == 0) __seenByFiber.remove(Fiber.current)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
bytes { new StringByteSequence(this) }
|
||||
}
|
||||
|
||||
@@ -176,7 +148,7 @@ class List is Sequence {
|
||||
return other
|
||||
}
|
||||
|
||||
toString { String.safeToString_(this) { "[" + join(", ") + "]" } }
|
||||
toString { "[" + join(", ") + "]" }
|
||||
|
||||
+(other) {
|
||||
var result = this[0..-1]
|
||||
@@ -192,18 +164,16 @@ class Map {
|
||||
values { new MapValueSequence(this) }
|
||||
|
||||
toString {
|
||||
return String.safeToString_(this) {
|
||||
var first = true
|
||||
var result = "{"
|
||||
var first = true
|
||||
var result = "{"
|
||||
|
||||
for (key in keys) {
|
||||
if (!first) result = result + ", "
|
||||
first = false
|
||||
result = result + key.toString + ": " + this[key].toString
|
||||
}
|
||||
|
||||
return result + "}"
|
||||
for (key in keys) {
|
||||
if (!first) result = result + ", "
|
||||
first = false
|
||||
result = result + key.toString + ": " + this[key].toString
|
||||
}
|
||||
|
||||
return result + "}"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user