From 6c970f4c9f7b37f6bfb217753dc65b1af3ebe40f Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 15 Jan 2019 17:52:38 +0200 Subject: [PATCH] Remove remaining recursive memory manager operations. (#2541) --- backend.native/tests/runtime/text/utf8.kt | 2 +- .../tests/runtime/workers/freeze3.kt | 2 +- runtime/src/main/cpp/Memory.cpp | 70 ++++++++++++------- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/backend.native/tests/runtime/text/utf8.kt b/backend.native/tests/runtime/text/utf8.kt index a91045fde6c..c36d203959b 100644 --- a/backend.native/tests/runtime/text/utf8.kt +++ b/backend.native/tests/runtime/text/utf8.kt @@ -66,7 +66,7 @@ fun checkThrows(e: KClass, string: String, action: () -> Unit) { assertTrue(e.isInstance(exception),""" Wrong exception was thrown for string: $string Expected: ${e.qualifiedName} - Actual: ${exception!!::class.qualifiedName} + Actual: ${exception::class.qualifiedName} """.trimIndent()) } diff --git a/backend.native/tests/runtime/workers/freeze3.kt b/backend.native/tests/runtime/workers/freeze3.kt index 50bea669284..f221b06e4c0 100644 --- a/backend.native/tests/runtime/workers/freeze3.kt +++ b/backend.native/tests/runtime/workers/freeze3.kt @@ -13,7 +13,7 @@ object Immutable { var x = 1 } -@kotlin.native.ThreadLocal +@ThreadLocal object Mutable { var x = 2 } diff --git a/runtime/src/main/cpp/Memory.cpp b/runtime/src/main/cpp/Memory.cpp index c05904354fa..7f681517632 100644 --- a/runtime/src/main/cpp/Memory.cpp +++ b/runtime/src/main/cpp/Memory.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. + * Copyright 2010-2018 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -1685,12 +1685,17 @@ bool ClearSubgraphReferences(ObjHeader* root, bool checked) { * - not 'marked' and not 'seen' as WHITE marker (object is unprocessed) * When we see GREY during DFS, it means we see cycle. */ -void depthFirstTraversal(ContainerHeader* container, bool* hasCycles, - KRef* firstBlocker, KStdVector& order) { - // Mark GRAY. - container->setSeen(); +void depthFirstTraversal(ContainerHeader* start, bool* hasCycles, + KRef* firstBlocker, KStdVector* order) { + ContainerHeaderDeque toVisit; + toVisit.push_back(start); + start->setSeen(); - traverseContainerReferredObjects(container, [hasCycles, firstBlocker, &order](ObjHeader* obj) { + while (!toVisit.empty()) { + auto* container = toVisit.front(); + toVisit.pop_front(); + + traverseContainerReferredObjects(container, [hasCycles, firstBlocker, &order, &toVisit](ObjHeader* obj) { if (*firstBlocker != nullptr) return; if (obj->has_meta_object() && ((obj->meta_object()->flags_ & MF_NEVER_FROZEN) != 0)) { @@ -1704,26 +1709,39 @@ void depthFirstTraversal(ContainerHeader* container, bool* hasCycles, // Go deeper if WHITE. if (!objContainer->seen() && !objContainer->marked()) { - depthFirstTraversal(objContainer, hasCycles, firstBlocker, order); + // Mark GRAY. + objContainer->setSeen(); + toVisit.push_front(objContainer); } } - }); - // Mark BLACK. - container->resetSeen(); - container->mark(); - order.push_back(container); + }); + // Mark BLACK. + container->resetSeen(); + container->mark(); + order->push_back(container); + } } -void traverseStronglyConnectedComponent(ContainerHeader* container, - KStdUnorderedMap> const& reversedEdges, - KStdVector& component) { - component.push_back(container); - container->mark(); - auto it = reversedEdges.find(container); - RuntimeAssert(it != reversedEdges.end(), "unknown node during condensation building"); - for (auto* nextContainer : it->second) { - if (!nextContainer->marked()) - traverseStronglyConnectedComponent(nextContainer, reversedEdges, component); +void traverseStronglyConnectedComponent(ContainerHeader* start, + KStdUnorderedMap> const* reversedEdges, + KStdVector* component) { + ContainerHeaderDeque toVisit; + toVisit.push_back(start); + start->mark(); + + while (!toVisit.empty()) { + auto* container = toVisit.front(); + toVisit.pop_front(); + component->push_back(container); + auto it = reversedEdges->find(container); + RuntimeAssert(it != reversedEdges->end(), "unknown node during condensation building"); + for (auto* nextContainer : it->second) { + if (!nextContainer->marked()) { + nextContainer->mark(); + toVisit.push_front(nextContainer); + } + } } } @@ -1775,10 +1793,10 @@ void freezeCyclic(ContainerHeader* rootContainer, const KStdVectormarked()) continue; KStdVector component; - traverseStronglyConnectedComponent(container, reversedEdges, component); + traverseStronglyConnectedComponent(container, &reversedEdges, &component); MEMORY_LOG("SCC:\n"); #if TRACE_MEMORY - for (auto c : component) + for (auto c: component) konan::consolePrintf(" %p\n", c); #endif components.push_back(std::move(component)); @@ -1832,7 +1850,7 @@ void freezeCyclic(ContainerHeader* rootContainer, const KStdVectorhas_meta_object() && ((root->meta_object()->flags_ & MF_NEVER_FROZEN) != 0) ? root : nullptr; KStdVector order; - depthFirstTraversal(rootContainer, &hasCycles, &firstBlocker, order); + depthFirstTraversal(rootContainer, &hasCycles, &firstBlocker, &order); if (firstBlocker != nullptr) { ThrowFreezingException(root, firstBlocker); }