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 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 scabug commented Apr 20, 2014

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

Loading

@scabug
Copy link
Author

@scabug scabug commented Nov 25, 2014

Loading

@scabug scabug closed this Jan 22, 2015
@scabug scabug added this to the 2.12.0-M1 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants