Skip to content

Optimize bytecode generation for synchronized methods #5933

Open
@smarter

Description

@smarter

Given:

class A {
  def foo: String = synchronized {
    ""
  }
}

Dotty generates the equivalent of:

    public String foo() {
        String string;
        A a = this;
        synchronized (a) {
            string = "";
        }
        return string;
    }

whereas Scala 2 generates something equivalent to:

    public synchronized String foo() {
        return "";
    }

which at the very least requires less bytecode. This is implemented by translateSynchronized in https://github.com/scala/scala/blob/2.13.x/src/compiler/scala/tools/nsc/transform/UnCurry.scala, but one has to be careful about not emitting synchronized static methods: scala/bug#11331 scala/scala#7593 scala/scala#7749

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions