Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Constructor inlining #17721

Closed
scf37 opened this issue Jun 3, 2022 · 1 comment
Closed

Constructor inlining #17721

scf37 opened this issue Jun 3, 2022 · 1 comment

Comments

@scf37
Copy link

scf37 commented Jun 3, 2022

Dotty has method inlining but it is incomplete without constructor inlining.

Motivation

trait JsonReader[A] {
  def read(it: JsonIterator): A
}

class SeqJsonReader[A](inner: JsonIterator => A) {
  inline def read(it: JsonIterator): A = ..... // won't inline as expected because constructor parameters are not inlined
}

inline given [A]: SeqJsonReader[A] = new SeqJsonReader(it => summon[JsonReader[A]].read(it)) // summoned read() contents will be inlined

Problem

  class Foo (a: String) {
    inline def foo(): Unit = println(a)
  }

  def print() = new Foo("hello").foo()

Compiles to:

public void print() {
    Foo Foo_this = new Foo("hello");
    Predef$.MODULE$.println((Object)Foo_this.inline$a());
}

which is wasted allocation.

Therefore when inlining method referencing constructor parameters only, there is no need to create object instance.

Possible solutions

  1. Inline classes - kind of explored here https://odersky.github.io/sips//pending/inline-classes.html
  2. Perform this optimization automatically
@TheElectronWill
Copy link
Contributor

TheElectronWill commented Jun 15, 2022

This is related to the idea of an "inliner-optimizer" (quoted in this thread about for loops) that would perform scalar replacement (replacing the fields by local variables and inlining the constructor). I'm actually playing with the idea by implementing a new compiler phase in dotty.

@ckipp01 ckipp01 transferred this issue from lampepfl/dotty-feature-requests May 31, 2023
@scala scala locked and limited conversation to collaborators May 31, 2023
@ckipp01 ckipp01 converted this issue into discussion #17722 May 31, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants