Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6895497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:54:57+00:00 2026-05-27T06:54:57+00:00

Recent events on the blogosphere have indicated that a possible performance problem with Scala

  • 0

Recent events on the blogosphere have indicated that a possible performance problem with Scala is its use of closures to implement for.

What are the reasons for this design decision, as opposed to a C or Java-style “primitive for” – that is one which will be turned into a simple loop?

(I’m making a distinction between Java’s for and its “foreach” construct here, as the latter involves an implicit Iterator).

More detail, following up from Peter. This bit of Scala:

  object ScratchFor {
    def main(args : Array[String]) : Unit = {
      for (val s <- args) {
        println(s)
      }
    }
  }

creates 3 classes: ScratchFor$$anonfun$main$1.class ScratchFor$.class ScratchFor.class

ScratchFor::main just forwards to the companion object, ScratchFor$.MODULE$::main which spins up an ScratchFor$$anonfun$main$1 (which is an implementation of AbstractFunction1).

It’s in the apply() method of this anonymous inner impl of AbstractFunction1 that the actual code lives, which is effectively the loop body.

I don’t see HotSpot being able to rewrite this into a simple loop. Happy to be proved wrong on this, though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:54:58+00:00Added an answer on May 27, 2026 at 6:54 am

    Traditional for loops are clumsy, verbose and error-prone. I think it is proof enough of this that “for-each” loops where added to Java, C# and C++, but if you want more details you may check item 46 of Effective Java.

    Now, for-each loops are still much faster than Scala for-comprehension, but they are also much less powerful (and more clumsy) because they cannot return values. If you want to transform or filter a collection (or do both to a group of collections), you’ll still have to handle all the mechanical details of constructing the result collection in addition to computing the values. Not to mention it inevitably uses some mutable state.

    Finally, even though for-each loops are adequate enough for collections, they are not suited to other monadic classes (of which collections are a subset of).

    So Scala has a general method which takes care of all of the above. Yes, it is slower, but the goal is to have the compiler effectively optimise it well enough so that this doesn’t become a hindrance (and, of course, JIT could help here as well).

    That has not been accomplished to this date, but -optimise has reduced a lot of ground between common for-each loops and for-comprehensions on the latest versions of Scala. If performance is essential, you can always use while or tail recursion.

    Now, it would be possibly for Scala to have common for loops or for-each loops as special cases specifically targeted at performance issues (since for-comprehensions can do everything they do). However, that violates two principles that guide Scala’s design:

    1. Reduce complexity. Yes, contrary to what some say, that is a design goal, and special cases that serve no other purpose other than optimise performance — even though a workable solution exists for performance cases — would needlessly increase the complexity of the language.

    2. Scalability. This is in the sense that the use can scale the language for any size of problem by writing libraries. The point here is that having the compiler optimise one particular class, such as Range, would make it impossible for the user to create a replacement class that would perform just as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a SQL table that is populated with events from our website (mostly
Recent conversations with colleagues have produced varying points of view on this matter. What
A recent question about string literals in .NET caught my eye. I know that
A recent question about StyleCop alerted me to the use of tools to enforce
A recent problem* left me wondering whether there is a text editor out there
A recent post by John Gruber notes that the following legalese: 3.3.1 — Applications
Due to recent events, i am trying to figure out how much debugging logs
This stems from a recent couple of posts I've made on events and memory
I have the parent model of user and user has_many :events . From the
I come from a strong Java background and in recent years have been also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.