Skip to content

Commit

Permalink
Avoid unnecessary copies in generated VisitorTransforms (#75)
Browse files Browse the repository at this point in the history
* Avoid uneccessary copies in generated VisitorTransforms

Previously, the generated VisitorTransform classes *always* made a
deep copy of the input tree. With this commit, only those nodes which
are actually changed will be copied.

In the transform*() functions of the VisitorTransform super classes,
we now check to see if all of the new child nodes are the same
instance as the original child nodes.  If so, there is no need to
copy the original node.

Note that this does not apply to cross-domain visitor transforms
since the type of the nodes is changing, it is still neccessary to
perform the deep copy.
  • Loading branch information
dlurton authored Jun 13, 2021
1 parent 7e9ae0f commit 3e11da6
Show file tree
Hide file tree
Showing 6 changed files with 1,292 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,38 @@ import com.amazon.ionelement.api.MetaContainer
abstract class DomainVisitorTransformBase {
open fun transformMetas(metas: MetaContainer) = metas

open fun transformAnyElement(node: AnyElement): AnyElement =
// TODO: remove .asAnyElement() below when https://github.com/amzn/ion-element-kotlin/issues/36 is fixed.
node.copy(metas = transformMetas(node.metas)).asAnyElement()
open fun transformAnyElement(node: AnyElement): AnyElement {
val newMetas = transformMetas(node.metas)
return if(node.metas !== newMetas) {
node.copy(metas = newMetas).asAnyElement()
} else {
node
}
}

open fun transformSymbolPrimitive(sym: SymbolPrimitive) =
SymbolPrimitive(
transformSymbolPrimitiveText(sym),
transformSymbolPrimitiveMetas(sym))
open fun transformSymbolPrimitive(sym: SymbolPrimitive): SymbolPrimitive {
val newText = transformSymbolPrimitiveText(sym)
val newMetas = transformSymbolPrimitiveMetas(sym)
return if(sym.text != newText || sym.metas !== newMetas) {
SymbolPrimitive(newText, newMetas)
} else {
sym
}
}

open fun transformSymbolPrimitiveText(sym: SymbolPrimitive) = sym.text

open fun transformSymbolPrimitiveMetas(sym: SymbolPrimitive) = transformMetas(sym.metas)

open fun transformLongPrimitive(lng: LongPrimitive) =
LongPrimitive(
transformLongPrimitiveValue(lng),
transformLongPrimitiveMetas(lng))
open fun transformLongPrimitive(lng: LongPrimitive): LongPrimitive {
val newValue = transformLongPrimitiveValue(lng)
val newMetas = transformLongPrimitiveMetas(lng)
return if(lng.value != newValue || lng.metas !== newMetas) {
LongPrimitive(newValue, newMetas)
} else {
lng
}
}

open fun transformLongPrimitiveValue(sym: LongPrimitive): Long = sym.value

Expand Down
Loading

0 comments on commit 3e11da6

Please sign in to comment.