From 3af0f6fc7fe1a889b33ed857d0dc946a73ef3101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20D=C3=BCsing?= Date: Wed, 20 Jul 2022 15:34:52 +0200 Subject: [PATCH] clear entire type caches and refill them from array on flush --- .../br/src/main/scala/org/opalj/br/Type.scala | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/OPAL/br/src/main/scala/org/opalj/br/Type.scala b/OPAL/br/src/main/scala/org/opalj/br/Type.scala index 420eeb3eab..dc7fd9c450 100644 --- a/OPAL/br/src/main/scala/org/opalj/br/Type.scala +++ b/OPAL/br/src/main/scala/org/opalj/br/Type.scala @@ -1125,13 +1125,17 @@ object ObjectType { updateObjectTypes() try { - // Remove all non-predefined OTs from the cache - Range(highestPredefinedTypeId + 1, objectTypes.length) - .foreach(i => cache.remove(objectTypes(i).fqn)) + // Clear the entire cache + cache.clear() // Truncate the ObjectType cache array to lose all not-predefined ObjectTypes objectTypes = JArrays.copyOf(objectTypes, highestPredefinedTypeId + 1) + // Refill the cache using the objectTypes array + objectTypes.foreach { ot => + cache.put(ot.fqn, new WeakReference[ObjectType](ot)) + } + // Reset ID counter to highest id in the cache nextId.set(highestPredefinedTypeId + 1) } finally { @@ -1615,13 +1619,20 @@ object ArrayType { // we might delete the predefined types updateArrayTypes() - // Remove all non-predefined ATs from the cache - Range(-lowestPredefinedTypeId + 1, arrayTypes.length) - .foreach(i => cache.remove(arrayTypes(i).componentType)) + // Clear the entire cache + cache.clear() // Reset array to only contain predefined ATs arrayTypes = JArrays.copyOf(arrayTypes, -lowestPredefinedTypeId + 1) + // Refill the cache using the arrayTypes array + arrayTypes.foreach { at => + // arrayTypes(0) is gonna be null, so we need this guard + if (at != null) { + cache.put(at.componentType, new WeakReference[ArrayType](at)) + } + } + // Reset id counter nextId.set(lowestPredefinedTypeId - 1)