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

optimize def f(args) = synchronized { ... } #64

Closed
scabug opened this issue Sep 9, 2007 · 8 comments
Closed

optimize def f(args) = synchronized { ... } #64

scabug opened this issue Sep 9, 2007 · 8 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Sep 9, 2007

Java:

synchronized String g() { return "str"; }

gets compiled to

synchronized java.lang.String g();
  Signature: ()Ljava/lang/String;
  Code:
   0:   ldc     SI-2; //String str
   2:   areturn

}

Scala:

def f = synchronized { "str" }

gets compiled to

public java.lang.String f();
  Signature: ()Ljava/lang/String;
  Code:
   0:   aload_0
   1:   dup
   2:   astore_1
   3:   monitorenter
   4:   ldc     SI-14; //String str
   6:   aload_1
   7:   monitorexit
   8:   checkcast       SI-16; //class java/lang/String
   11:  areturn
   12:  aload_1
   13:  monitorexit
   14:  athrow
  Exception table:
   from   to  target type
     4     8    12   any

Although Java

String g() { synchronized (this) { return "str"; } }

is also compiled to something like Scala compiler outputs, I think Scalac should optimize special case

def f(args) = synchronized { ... }
@scabug
Copy link
Author

scabug commented Sep 9, 2007

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

@scabug
Copy link
Author

scabug commented Sep 18, 2007

@dragos said:
I improved the dead-code elimination pass to take care of such cases, so compiling with -optimise should remove them. It's still not ideal, so I'll leave this ticket open. I think we should not even generate boxing for synchronized blocks.

@scabug
Copy link
Author

scabug commented Jan 14, 2009

@odersky said:
Milestone postponed deleted

@scabug
Copy link
Author

scabug commented Mar 27, 2010

@dcsobral said:
Replying to [comment:2 dragos]:

I improved the dead-code elimination pass to take care of such cases, so compiling with -optimise should remove them. It's still not ideal, so I'll leave this ticket open. I think we should not even generate boxing for synchronized blocks.

I wonder what optimization you have made... or whether this was reverted at some point. I see no difference in the generated code with recent trunk than what was originally posted.

@scabug
Copy link
Author

scabug commented Mar 28, 2010

@dragos said:
Honestly, I don't remember exactly. Is this about code size alone, or it is less efficient than adding the modifier?

@scabug
Copy link
Author

scabug commented Feb 16, 2012

@khernyo said:
A partial implementation can be found at https://github.com/szabolcsberecz/scala/tree/SI-64

It can transform a method with synchronized body into a synchronized method, but the inlining of these methods are disabled because it would result in code without proper synchronization.

I'm not going to finish it because I can't justify the additional complexity of the inliner.

@scabug
Copy link
Author

scabug commented Aug 16, 2012

@lrytz said:
paul, did you fix this?

@scabug
Copy link
Author

scabug commented Aug 16, 2012

@paulp said:
I participated anyway. Here's what that def f generates in scala now.

  public synchronized java.lang.String f();
    flags: ACC_PUBLIC, ACC_SYNCHRONIZED
    Code:
      stack=1, locals=1, args_size=1
         0: ldc           #12                 // String str
         2: areturn       

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