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

Compute pool metrics #2534

Merged
merged 8 commits into from
Nov 14, 2021
Merged

Compute pool metrics #2534

merged 8 commits into from
Nov 14, 2021

Conversation

vasilmkd
Copy link
Member

I've been putting off doing this for so long.

@vasilmkd vasilmkd requested a review from armanbilge November 13, 2021 18:34
armanbilge
armanbilge previously approved these changes Nov 13, 2021
Copy link
Member

@armanbilge armanbilge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't have experience with MBeans but this seems straightforward enough 👍

Comment on lines +569 to +570
private[unsafe] def blockedWorkerThreadCounterForwarder: AtomicInteger =
blockedWorkerThreadCounter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pedagogical: why keep this behind a forwarder?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of this BS:

class Stupid {
  val blah = 5
}

compiles to the following bytecode:

public class cats.effect.Stupid {
  private final int blah;

  private volatile boolean bitmap$init$0;

  public int blah();
    Code:
       0: aload_0
       1: getfield      #15                 // Field bitmap$init$0:Z
       4: ifeq          14
       7: aload_0
       8: getfield      #17                 // Field blah:I
      11: goto          24
      14: new           #19                 // class scala/UninitializedFieldError
      17: dup
      18: ldc           #21                 // String Uninitialized field: /Users/vasil/Code/cats-effect/core/shared/src/main/scala/cats/effect/Stupid.scala: 20
      20: invokespecial #25                 // Method scala/UninitializedFieldError."<init>":(Ljava/lang/String;)V
      23: athrow
      24: pop
      25: aload_0
      26: getfield      #17                 // Field blah:I
      29: ireturn

  public cats.effect.Stupid();
    Code:
       0: aload_0
       1: invokespecial #30                 // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iconst_5
       6: putfield      #17                 // Field blah:I
       9: aload_0
      10: iconst_1
      11: putfield      #15                 // Field bitmap$init$0:Z
      14: return
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The blah() getter is synchronized on the bitmap field, which is volatile, incurring unnecessary synchronization points.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I thought this happened even for private[this] fields which is why you moved everything to the constructor?

Copy link
Member Author

@vasilmkd vasilmkd Nov 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does in the constructor. But for private[this] fields, the getter is not automatically generated by the compiler. A "forwarder" method accesses the field raw.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class Forwarder {
  private[this] val blah: Int = 5

  def blahForwarder: Int = blah
}

compiles to

public class cats.effect.Forwarder {
  private final int blah;

  private volatile boolean bitmap$init$0;

  public int blahForwarder();
    Code:
       0: aload_0
       1: getfield      #16                 // Field blah:I
       4: ireturn

  public cats.effect.Forwarder();
    Code:
       0: aload_0
       1: invokespecial #22                 // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iconst_5
       6: putfield      #16                 // Field blah:I
       9: aload_0
      10: iconst_1
      11: putfield      #24                 // Field bitmap$init$0:Z
      14: return
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forwarder is a Cats Effect nomenclature. I think I heard Daniel say it once, or use it, and I continued doing so. It doesn't have any meaning. It's the same as getter, the point is to write it ourselves and circumvent the synchronization that the Scala compiler would do otherwise. 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🤓 I'm very glad there's a workaround! But I wish I understood why the Scala compiler does this.

@vasilmkd vasilmkd merged commit ec05778 into typelevel:series/3.x Nov 14, 2021
@vasilmkd vasilmkd deleted the compute-pool-metrics branch November 14, 2021 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants