Skip to content

Commit

Permalink
fix(core): wrong put call, missing buffering
Browse files Browse the repository at this point in the history
  • Loading branch information
zlataovce committed Dec 14, 2023
1 parent c1f74ca commit 50ad2ed
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AncestryTreeBuilder<T : MappingTreeView, E : MappingTreeView.ElementMappin
/**
* Collection that new nodes should be appended to.
*/
private val currentNodes: MutableCollection<MutableNode<E>>
private inline val currentNodes: MutableCollection<MutableNode<E>>
get() = if (buffering) nodeBuffer else nodes

/**
Expand Down Expand Up @@ -214,7 +214,7 @@ class AncestryTreeBuilder<T : MappingTreeView, E : MappingTreeView.ElementMappin
// replaced the putAll implementation here to route everything through the put method
// makes dealing with the first and last items easier
from.forEach { (key, value) ->
delegate[key] = value
put(key, value)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,62 +166,64 @@ fun <T : MappingTreeView, C : MappingTreeView.ClassMappingView, F : MappingTreeV
inheritNamespaces(klass.tree)

klass.forEach klassEach@ { (version, realKlass) ->
val treeAllowedNamespaces = klass.tree.allowedNamespaces[version]
?: error("Version ${version.id} has not been mapped yet")
bufferCycle {
val treeAllowedNamespaces = klass.tree.allowedNamespaces[version]
?: error("Version ${version.id} has not been mapped yet")

val indexNsId = klass.tree.indexNamespaces[version]?.id ?: MappingTreeView.NULL_NAMESPACE_ID
@Suppress("UNCHECKED_CAST")
(realKlass.fields as Collection<F>).forEach { field ->
// try to resolve a node by its index
if (indexNsId != MappingTreeView.NULL_NAMESPACE_ID) {
val nodeIndex = field.getDstName(indexNsId)?.toIntOrNull()
if (nodeIndex != null) {
val node = findByIndex(nodeIndex)
val indexNsId = klass.tree.indexNamespaces[version]?.id ?: MappingTreeView.NULL_NAMESPACE_ID
@Suppress("UNCHECKED_CAST")
(realKlass.fields as Collection<F>).forEach { field ->
// try to resolve a node by its index
if (indexNsId != MappingTreeView.NULL_NAMESPACE_ID) {
val nodeIndex = field.getDstName(indexNsId)?.toIntOrNull()
if (nodeIndex != null) {
val node = findByIndex(nodeIndex)

node[version] = field // it's not necessary to fill in lastNames
return@forEach
node[version] = field // it's not necessary to fill in lastNames
return@forEach
}
}
}

val fieldNameMappings = treeAllowedNamespaces.mapNotNullTo(HashSet(treeAllowedNamespaces.size)) { (ns, id) ->
field.getDstName(id)?.let { name -> NamespacedMapping(ns, name) }
}
val fieldDescMappings = treeAllowedNamespaces.mapNotNullTo(HashSet(treeAllowedNamespaces.size)) { (ns, id) ->
field.getDstDesc(id)?.let { name -> NamespacedMapping(ns, name) }
}
val fieldNameMappings = treeAllowedNamespaces.mapNotNullTo(HashSet(treeAllowedNamespaces.size)) { (ns, id) ->
field.getDstName(id)?.let { name -> NamespacedMapping(ns, name) }
}
val fieldDescMappings = treeAllowedNamespaces.mapNotNullTo(HashSet(treeAllowedNamespaces.size)) { (ns, id) ->
field.getDstDesc(id)?.let { name -> NamespacedMapping(ns, name) }
}

// perf: use array due to marginally better iteration performance
val fieldNameMappingsArray = fieldNameMappings.toTypedArray()
val fieldDescMappingsArray = fieldDescMappings.toTypedArray()

// do we have a node with one or more equal name-descriptor pairs in the last version?
// if we don't, make a new node and append it to the tree
val node = findOrEmpty { node ->
val (lastVersion, lastMapping) = node.last ?: error("Encountered a node without a last mapping")
val lastNames = node.lastNames
?: this@buildAncestryTree.allowedNamespaces[lastVersion]?.mapNotNullTo(HashSet()) { (ns, id) ->
lastMapping.getDstName(id)?.let { name -> NamespacedMapping(ns, name) }
}
?: error("Version ${lastVersion.id} has not been mapped yet")
// perf: use array due to marginally better iteration performance
val fieldNameMappingsArray = fieldNameMappings.toTypedArray()
val fieldDescMappingsArray = fieldDescMappings.toTypedArray()

if (fieldNameMappingsArray.any(lastNames::contains)) {
val lastDescs = node.lastDescs
// do we have a node with one or more equal name-descriptor pairs in the last version?
// if we don't, make a new node and append it to the tree
val node = findOrEmpty { node ->
val (lastVersion, lastMapping) = node.last ?: error("Encountered a node without a last mapping")
val lastNames = node.lastNames
?: this@buildAncestryTree.allowedNamespaces[lastVersion]?.mapNotNullTo(HashSet()) { (ns, id) ->
lastMapping.getDstDesc(id)?.let { name -> NamespacedMapping(ns, name) }
lastMapping.getDstName(id)?.let { name -> NamespacedMapping(ns, name) }
}
?: error("Version ${lastVersion.id} has not been mapped yet")

return@findOrEmpty fieldDescMappingsArray.any(lastDescs::contains)
}
if (fieldNameMappingsArray.any(lastNames::contains)) {
val lastDescs = node.lastDescs
?: this@buildAncestryTree.allowedNamespaces[lastVersion]?.mapNotNullTo(HashSet()) { (ns, id) ->
lastMapping.getDstDesc(id)?.let { name -> NamespacedMapping(ns, name) }
}
?: error("Version ${lastVersion.id} has not been mapped yet")

return@findOrEmpty false
}
return@findOrEmpty fieldDescMappingsArray.any(lastDescs::contains)
}

return@findOrEmpty false
}

// add the entry for this version to the node
val lastValue = node.put(version, field)
if (lastValue == null) {
node.lastNames = fieldNameMappings
node.lastDescs = fieldDescMappings
// add the entry for this version to the node
val lastValue = node.put(version, field)
if (lastValue == null) {
node.lastNames = fieldNameMappings
node.lastDescs = fieldDescMappings
}
}
}
}
Expand Down Expand Up @@ -265,81 +267,83 @@ fun <T : MappingTreeView, C : MappingTreeView.ClassMappingView, M : MappingTreeV
inheritNamespaces(klass.tree)

klass.forEach klassEach@ { (version, realKlass) ->
val treeAllowedNamespaces = klass.tree.allowedNamespaces[version]
?: error("Version ${version.id} has not been mapped yet")

val indexNsId = klass.tree.indexNamespaces[version]?.id ?: MappingTreeView.NULL_NAMESPACE_ID
@Suppress("UNCHECKED_CAST")
(realKlass.methods as Collection<M>).forEach { method ->
val isConstructor = method.isConstructor
when (constructorMode) {
ConstructorComputationMode.EXCLUDE -> {
if (isConstructor) return@forEach
}

ConstructorComputationMode.ONLY -> {
if (!isConstructor) return@forEach
}
bufferCycle {
val treeAllowedNamespaces = klass.tree.allowedNamespaces[version]
?: error("Version ${version.id} has not been mapped yet")

ConstructorComputationMode.INCLUDE -> {}
}
val indexNsId = klass.tree.indexNamespaces[version]?.id ?: MappingTreeView.NULL_NAMESPACE_ID
@Suppress("UNCHECKED_CAST")
(realKlass.methods as Collection<M>).forEach { method ->
val isConstructor = method.isConstructor
when (constructorMode) {
ConstructorComputationMode.EXCLUDE -> {
if (isConstructor) return@forEach
}

// try to resolve a node by its index
if (indexNsId != MappingTreeView.NULL_NAMESPACE_ID) {
val nodeIndex = method.getDstName(indexNsId)?.toIntOrNull()
if (nodeIndex != null) {
val node = findByIndex(nodeIndex)
ConstructorComputationMode.ONLY -> {
if (!isConstructor) return@forEach
}

node[version] = method // it's not necessary to fill in lastNames
return@forEach
ConstructorComputationMode.INCLUDE -> {}
}
}

val methodNameMappings = if (isConstructor) CTOR_NAME_SET else treeAllowedNamespaces.mapNotNullTo(HashSet(treeAllowedNamespaces.size)) { (ns, id) ->
method.getDstName(id)?.let { name -> NamespacedMapping(ns, name) }
}
val methodDescMappings = treeAllowedNamespaces.mapNotNullTo(HashSet(treeAllowedNamespaces.size)) { (ns, id) ->
method.getDstDesc(id)?.let { name -> NamespacedMapping(ns, name) }
}
// try to resolve a node by its index
if (indexNsId != MappingTreeView.NULL_NAMESPACE_ID) {
val nodeIndex = method.getDstName(indexNsId)?.toIntOrNull()
if (nodeIndex != null) {
val node = findByIndex(nodeIndex)

node[version] = method // it's not necessary to fill in lastNames
return@forEach
}
}

// perf: use array due to marginally better iteration performance
val methodNameMappingsArray = methodNameMappings.toTypedArray()
val methodDescMappingsArray = methodDescMappings.toTypedArray()
val methodNameMappings = if (isConstructor) CTOR_NAME_SET else treeAllowedNamespaces.mapNotNullTo(HashSet(treeAllowedNamespaces.size)) { (ns, id) ->
method.getDstName(id)?.let { name -> NamespacedMapping(ns, name) }
}
val methodDescMappings = treeAllowedNamespaces.mapNotNullTo(HashSet(treeAllowedNamespaces.size)) { (ns, id) ->
method.getDstDesc(id)?.let { name -> NamespacedMapping(ns, name) }
}

// do we have a node with one or more equal name-descriptor pairs in the last version?
// if we don't, make a new node and append it to the tree
val node = findOrEmpty { node ->
val (lastVersion, lastMapping) = node.last ?: error("Encountered a node without a last mapping")
val isLastConstructor = lastMapping.isConstructor
// perf: use array due to marginally better iteration performance
val methodNameMappingsArray = methodNameMappings.toTypedArray()
val methodDescMappingsArray = methodDescMappings.toTypedArray()

// FAST PATH: method type mismatch, so skip
if (isConstructor != isLastConstructor) return@findOrEmpty false
// do we have a node with one or more equal name-descriptor pairs in the last version?
// if we don't, make a new node and append it to the tree
val node = findOrEmpty { node ->
val (lastVersion, lastMapping) = node.last ?: error("Encountered a node without a last mapping")
val isLastConstructor = lastMapping.isConstructor

val lastNames = node.lastNames
?: if (isLastConstructor) CTOR_NAME_SET else this@buildAncestryTree.allowedNamespaces[lastVersion]
?.mapNotNullTo(HashSet()) { (ns, id) ->
lastMapping.getDstName(id)?.let { name -> NamespacedMapping(ns, name) }
}
?: error("Version ${lastVersion.id} has not been mapped yet")
// FAST PATH: method type mismatch, so skip
if (isConstructor != isLastConstructor) return@findOrEmpty false

if (methodNameMappingsArray.any(lastNames::contains)) {
val lastDescs = node.lastDescs
?: this@buildAncestryTree.allowedNamespaces[lastVersion]?.mapNotNullTo(HashSet()) { (ns, id) ->
lastMapping.getDstDesc(id)?.let { name -> NamespacedMapping(ns, name) }
}
?: error("Version ${lastVersion.id} has not been mapped yet")
val lastNames = node.lastNames
?: if (isLastConstructor) CTOR_NAME_SET else this@buildAncestryTree.allowedNamespaces[lastVersion]
?.mapNotNullTo(HashSet()) { (ns, id) ->
lastMapping.getDstName(id)?.let { name -> NamespacedMapping(ns, name) }
}
?: error("Version ${lastVersion.id} has not been mapped yet")

if (methodNameMappingsArray.any(lastNames::contains)) {
val lastDescs = node.lastDescs
?: this@buildAncestryTree.allowedNamespaces[lastVersion]?.mapNotNullTo(HashSet()) { (ns, id) ->
lastMapping.getDstDesc(id)?.let { name -> NamespacedMapping(ns, name) }
}
?: error("Version ${lastVersion.id} has not been mapped yet")

return@findOrEmpty methodDescMappingsArray.any(lastDescs::contains)
}

return@findOrEmpty methodDescMappingsArray.any(lastDescs::contains)
return@findOrEmpty false
}

return@findOrEmpty false
}

// add the entry for this version to the node
val lastValue = node.put(version, method)
if (lastValue == null) {
node.lastNames = methodNameMappings
node.lastDescs = methodDescMappings
// add the entry for this version to the node
val lastValue = node.put(version, method)
if (lastValue == null) {
node.lastNames = methodNameMappings
node.lastDescs = methodDescMappings
}
}
}
}
Expand Down

0 comments on commit 50ad2ed

Please sign in to comment.