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

collection.immutable.Map.apply is inefficient #8519

Closed
scabug opened this issue Apr 20, 2014 · 2 comments
Closed

collection.immutable.Map.apply is inefficient #8519

scabug opened this issue Apr 20, 2014 · 2 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Apr 20, 2014

as per the discussion on google groups: https://groups.google.com/d/msg/scala-user/WV-3xN8eRQE/cpTHqlie-B4J

the Map.apply methods for the immutable data structures (and getOrElse) are internally creating Somes which are thrown away.

Profiling shows that this creates a non-negligible cost.

Implementation specific overrides of apply() and getOrElse() would remove the performance penalty, whilst introducing no changes to the semantics of these core classes.

This may result in some (trivial) code duplication in the implementations.

Related to #4469

object Something extends App {
  println("hello world")

  val strings = Array("foo", "bar", "baz", "quux")

  def get(i: Int): String = {
    if (i < 1000000000) strings(i&3)
    else throw new Exception("Too big")
  }

  var sum: Long = 0

  (1 to 20) foreach {j =>
    // create a big object to simulate medium-lived active objects on the heap
    val big = (1 to 1000000).toList
    (1 to 100000000) foreach { i =>
      sum += get(i).length
    }
    // just to keep the ref alive
    require(big.nonEmpty)
  }

  val uptime = java.lang.management.ManagementFactory.getRuntimeMXBean().getUptime()
  println("goodbye world, I took " + uptime)
}

takes about 5 - 6 seconds as above, and 6 - 7 seconds when the "strings(i&3)" is converted to "Some(strings(i&3)).get", mimicking the Map.apply behaviour.

Further analysis available in the lions-share sbt plugin example: https://github.com/fommil/lions-share/

@scabug
Copy link
Author

scabug commented Apr 20, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8519?orig=1
Reporter: Samuel Halliday (fommil)

@scabug
Copy link
Author

scabug commented Nov 25, 2014

@Ichoran said:
scala/scala#4155

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

2 participants