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

Generate UUIDs on demand #8728

Merged
merged 5 commits into from
Jan 12, 2024
Merged

Generate UUIDs on demand #8728

merged 5 commits into from
Jan 12, 2024

Conversation

hubertp
Copy link
Contributor

@hubertp hubertp commented Jan 10, 2024

Pull Request Description

Trying to avoid expensive UUID.randomUUID() unless we reallly need it.

Closes #8716.

Important Notes

Some improvement:
Screenshot from 2024-01-11 15-16-10

FWIW Total Time for a Hello World example

Before
Screenshot from 2024-01-12 17-45-56

After
Screenshot from 2024-01-12 17-46-13

Memory usage

Before
Screenshot from 2024-01-12 17-54-54

After
Screenshot from 2024-01-12 17-54-36

Checklist

Please ensure that the following checklist has been satisfied before submitting the PR:

  • Screenshots/screencasts have been attached, if there are any visual changes. For interactive or animated visual changes, a screencast is preferred.
  • All code follows the
    Scala,
    Java,
    and
    Rust
    style guides. In case you are using a language not listed above, follow the Rust style guide.
  • All code has been tested:
    • Unit tests have been written where possible.
    • If GUI codebase was changed, the GUI was tested when built using ./run ide build.

Trying to avoid expensive `UUID.randomUUID()` unless we reallly need it.
@hubertp hubertp added the CI: No changelog needed Do not require a changelog entry for this PR. label Jan 10, 2024
Copy link
Member

@JaroslavTulach JaroslavTulach left a comment

Choose a reason for hiding this comment

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

I am glad the with LazyId solution seems to be working. Everything is nicely green. And the startup benchmarks seem to be a bit improved too.

Can you manually check the amount of UUID in memory before and now at the end of execution? The memory consumption should dramatically decrease, right?

Measuring how much UUID has been allocated on the heap (and GCed) would be interesting as well, but I am not sure how to perform such measurement reliably.

@@ -412,7 +411,7 @@ case object LambdaShorthandToLambda extends IRPass {
)

val lambdaArg = DefinitionArgument.Specified(
scrutineeName.copy(id = IR.randomId),
scrutineeName.copy(id = null),
Copy link
Member

Choose a reason for hiding this comment

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

null is "cheaper" value than any other value. Occupies nothing in the heap.

with IRKind.Primitive {
var id: UUID @Identifier = randomId
with IRKind.Primitive
with LazyId {
Copy link
Member

Choose a reason for hiding this comment

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

LazyId trait! That sounds promising.

private[this] var _id: UUID @Identifier = null

override def id: UUID @Identifier = {
_id
Copy link
Member

Choose a reason for hiding this comment

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

I thought that the

    if (_id == null) {
      _id = randomId()
    }
    _id

would be here, in this id method. That this id should never return null.

Wouldn't it be getter to:

override def id: UUID @Identifier = getId()

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem is that in IR definitions we use .id all over the place and we do not want to initialize it at that point. Only at the point when it is actually used, .getId(), outside of IR.

Copy link
Member

Choose a reason for hiding this comment

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

Can you make id private then? Package private or subtree private? Or there could be:

def isIdInitialized(): Boolean

and we could write

id = if (keepIdentifiers && isIdInitialized()) getId() else null

everywhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

If it is only used within IRs, we can mark it protected

@hubertp
Copy link
Contributor Author

hubertp commented Jan 12, 2024

Added screenshots that illustrate improvement on a simple Hello World program.

@hubertp hubertp added the CI: Clean build required CI runners will be cleaned before and after this PR is built. label Jan 12, 2024
Copy link
Member

@Akirathan Akirathan left a comment

Choose a reason for hiding this comment

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

Very nice and elegant improvement. Congratulations.

@hubertp hubertp added the CI: Ready to merge This PR is eligible for automatic merge label Jan 12, 2024
@mergify mergify bot merged commit 5b91f16 into develop Jan 12, 2024
29 checks passed
@mergify mergify bot deleted the wip/hubert/8716-uuid branch January 12, 2024 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: Clean build required CI runners will be cleaned before and after this PR is built. CI: No changelog needed Do not require a changelog entry for this PR. CI: Ready to merge This PR is eligible for automatic merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Delay UUID generation in IRs
4 participants