Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BasicTransformer has exponential complexity #3689

Closed
scabug opened this issue Jul 20, 2010 · 4 comments
Closed

BasicTransformer has exponential complexity #3689

scabug opened this issue Jul 20, 2010 · 4 comments

Comments

@scabug
Copy link

scabug commented Jul 20, 2010

It seems BasicTransformer has exponential complexity on the nesting level of the XML being processed. This is due to this method:

  def transform(ns: Seq[Node]): Seq[Node] = {
    val (xs1, xs2) = ns span (n => unchanged(n, transform(n)))
    
    if (xs2.isEmpty) ns
    else xs1 ++ transform(xs2.head) ++ transform(xs2.tail)
  }

Each modified node is transformed twice: once at the span, and again at the if/else. So, for <a><b><c><d/></c></b></a>, with c being modified, node a gets transformed twice, node b gets transformed four times (twice for each time a is transformed), and node c gets transformed eight times.

@scabug
Copy link
Author

scabug commented Jul 20, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3689?orig=1
Reporter: @dcsobral

@scabug
Copy link
Author

scabug commented Jul 20, 2010

@dcsobral said:
I propose the following rewrite of that method as a fix:

  def transform(ns: Seq[Node]): Seq[Node] = {
    val xs = ns.toStream map transform
    val (xs1, xs2) = xs zip ns span { case (x, n) => unchanged(n, x) }
    
    if (xs2.isEmpty) ns
    else (xs1 map (_._2)) ++ xs2.head._1 ++ transform(ns drop (xs1.length + 1))
  }

I considered modifying unchanged, but, as a protected method, it is part of the API. Also, ns take xs1.length might be better than xs1 map (_._2). This whole method could probably be made more efficient with a carefully constructed while loop, I think, but this, at least, solves the complexity problem.

@scabug
Copy link
Author

scabug commented Jul 17, 2015

@SethTisue said:
The scala-xml library is now community-maintained. Issues with it are now tracked at https://github.com/scala/scala-xml/issues instead of here in the Scala JIRA.

Interested community members: if you consider this issue significant, feel free to open a new issue for it on GitHub, with links in both directions.

@scabug scabug closed this as completed Jul 17, 2015
@scabug
Copy link
Author

scabug commented Jul 19, 2015

Caoilte O'Connor (caoilte) said:
Seems to be covered by
scala/scala-xml#58
and
scala/scala-xml#59

which is encouraging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant