Skip to content

Commit

Permalink
[core] Optimize ExceptionCollector, flatten suppressed exceptions and…
Browse files Browse the repository at this point in the history
… add them only to the last exception
  • Loading branch information
Him188 committed Aug 26, 2022
1 parent c1213bb commit 8dcc930
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
25 changes: 23 additions & 2 deletions mirai-core-utils/src/commonMain/kotlin/ExceptionCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public open class ExceptionCollector {
@Volatile
private var last: Throwable? = null
private val hashCodes = mutableSetOf<Long>()
private val suppressedList = mutableListOf<Throwable>()

/**
* @return `true` if [e] is new.
Expand All @@ -52,7 +53,8 @@ public open class ExceptionCollector {
}

protected open fun addSuppressed(receiver: Throwable, e: Throwable) {
receiver.addSuppressed(e)
suppressedList.add(e)
// receiver.addSuppressed(e)
}

public fun collectGet(e: Throwable?): Throwable {
Expand All @@ -66,7 +68,23 @@ public open class ExceptionCollector {
*/
public fun collectException(e: Throwable?): Boolean = collect(e)

public fun getLast(): Throwable? = last
/**
* Adds [suppressedList] to suppressed exceptions of [last]
*/
@Synchronized
private fun bake() {
last?.let { last ->
for (suppressed in suppressedList.asReversed()) {
last.addSuppressed(suppressed)
}
}
suppressedList.clear()
}

public fun getLast(): Throwable? {
bake()
return last
}

@TerminalOperation // to give it a color for a clearer control flow
public fun collectThrow(exception: Throwable): Nothing {
Expand Down Expand Up @@ -97,6 +115,7 @@ public open class ExceptionCollector {
public fun dispose() { // help gc
this.last = null
this.hashCodes.clear()
this.suppressedList.clear()
}

public companion object {
Expand Down Expand Up @@ -130,6 +149,8 @@ public inline fun <R> ExceptionCollector.withExceptionCollector(action: Exceptio
return action()
} catch (e: Throwable) {
collectThrow(e)
} finally {
dispose()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class ExceptionCollectorTest {
collector.collect(IllegalStateException())

assertIs<IllegalStateException>(collector.getLast())
assertTrue { collector.getLast()!!.suppressedExceptions.single() is IllegalArgumentException }
assertTrue { collector.getLast()!!.suppressedExceptions[0] is IllegalArgumentException }
assertEquals(2, collector.asSequence().count())
}

Expand All @@ -45,8 +45,8 @@ internal class ExceptionCollectorTest {
collector.collect(IllegalStateException())

assertIs<IllegalStateException>(collector.getLast())
assertTrue { collector.getLast()!!.suppressedExceptions.single() is IllegalArgumentException }
assertTrue { collector.getLast()!!.suppressedExceptions.single().suppressedExceptions.single() is IndexOutOfBoundsException }
assertTrue { collector.getLast()!!.suppressedExceptions[0] is IllegalArgumentException }
assertTrue { collector.getLast()!!.suppressedExceptions[1] is IndexOutOfBoundsException }
assertEquals(3, collector.asSequence().count())
}

Expand Down

0 comments on commit 8dcc930

Please sign in to comment.