Skip to content

Commit

Permalink
Generate UUIDs on demand (#8728)
Browse files Browse the repository at this point in the history
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](https://github.com/enso-org/enso/assets/292128/d8800490-6676-4b71-b178-7ce2e79942e5)

FWIW Total Time for a Hello World example

Before
![Screenshot from 2024-01-12 17-45-56](https://github.com/enso-org/enso/assets/292128/c0bfe7c5-c0a5-4375-8dd9-afb0714ae6c4)

After
![Screenshot from 2024-01-12 17-46-13](https://github.com/enso-org/enso/assets/292128/ea76c413-018f-4b67-9777-85378eb38210)

Memory usage

Before
![Screenshot from 2024-01-12 17-54-54](https://github.com/enso-org/enso/assets/292128/280b1eff-e019-4241-a2a1-07445949d285)

After
![Screenshot from 2024-01-12 17-54-36](https://github.com/enso-org/enso/assets/292128/b6524c8b-2a38-4e51-85eb-63142420f2ff)
  • Loading branch information
hubertp authored Jan 12, 2024
1 parent 972b359 commit 5b91f16
Show file tree
Hide file tree
Showing 38 changed files with 356 additions and 368 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ case object AliasAnalysis extends IRPass {
Occurrence.Def(
occurrenceId,
name.name,
binding.getId,
binding.getId(),
binding.getExternalId,
isSuspended
)
Expand Down Expand Up @@ -525,7 +525,7 @@ case object AliasAnalysis extends IRPass {
val definition = Graph.Occurrence.Def(
occurrenceId,
selfName.name,
arg.getId,
arg.getId(),
arg.getExternalId
)
scope.addDefinition(definition)
Expand Down Expand Up @@ -557,7 +557,7 @@ case object AliasAnalysis extends IRPass {
val definition = Graph.Occurrence.Def(
occurrenceId,
name.name,
arg.getId,
arg.getId(),
arg.getExternalId,
susp
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.enso.compiler.pass.desugar

import org.enso.compiler.context.{FreshNameSupply, InlineContext, ModuleContext}
import org.enso.compiler.core.IR
import org.enso.compiler.core.ir.{
CallArgument,
DefinitionArgument,
Expand Down Expand Up @@ -412,7 +411,7 @@ case object LambdaShorthandToLambda extends IRPass {
)

val lambdaArg = DefinitionArgument.Specified(
scrutineeName.copy(id = IR.randomId),
scrutineeName.copy(id = null),
None,
None,
suspended = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ trait IRUtils {
for {
metadata <- ir.getMetadata(DataflowAnalysis)
key = DataflowAnalysis.DependencyInfo.Type
.Static(literal.getId, literal.getExternalId)
.Static(literal.getId(), literal.getExternalId)
dependents <- metadata.dependents.get(key)
} yield {
dependents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ final class EnsureCompiledJob(
_
) =>
DataflowAnalysis.DependencyInfo.Type.Static(
err.getId,
err.getId(),
err.getExternalId
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,8 @@ default String pretty() {
*
* @return the node's identifier
*/
default @Identifier UUID getId() {
return id();
}

/** A unique identifier for a piece of IR. */
@Identifier
UUID id();
UUID getId();

/** Storage for compiler diagnostics related to the IR node. */
DiagnosticStorage diagnostics();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.enso.compiler.core.ir

import org.enso.compiler.core.{IR, Identifier}
import org.enso.compiler.core.IR.randomId
import org.enso.compiler.core.Implicits.{ShowPassData, ToStringHelper}

import java.util.UUID
Expand Down Expand Up @@ -49,8 +48,8 @@ object CallArgument {
passData: MetadataStorage = new MetadataStorage(),
diagnostics: DiagnosticStorage = DiagnosticStorage()
) extends CallArgument
with IRKind.Primitive {
var id: UUID @Identifier = randomId
with IRKind.Primitive
with LazyId {

/** Creates a copy of `this`.
*
Expand Down Expand Up @@ -110,7 +109,7 @@ object CallArgument {
if (keepMetadata) passData.duplicate else new MetadataStorage(),
diagnostics =
if (keepDiagnostics) diagnostics.copy else DiagnosticStorage(),
id = if (keepIdentifiers) id else randomId
id = if (keepIdentifiers) id else null
)

/** @inheritdoc */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.enso.compiler.core.ir

import org.enso.compiler.core.Implicits.{ShowPassData, ToStringHelper}
import org.enso.compiler.core.{IR, Identifier}
import org.enso.compiler.core.IR.randomId

import java.util.UUID
import scala.jdk.FunctionConverters.enrichAsScalaFromFunction
Expand Down Expand Up @@ -64,8 +63,8 @@ object DefinitionArgument {
passData: MetadataStorage = new MetadataStorage(),
diagnostics: DiagnosticStorage = DiagnosticStorage()
) extends DefinitionArgument
with IRKind.Primitive {
var id: UUID @Identifier = randomId
with IRKind.Primitive
with LazyId {

/** Creates a copy of `this`.
*
Expand Down Expand Up @@ -140,7 +139,7 @@ object DefinitionArgument {
if (keepMetadata) passData.duplicate else new MetadataStorage(),
diagnostics =
if (keepDiagnostics) diagnostics.copy else DiagnosticStorage(),
id = if (keepIdentifiers) id else randomId
id = if (keepIdentifiers) id else null
)

/** @inheritdoc */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.enso.compiler.core.ir

import org.enso.compiler.core.Implicits.{ShowPassData, ToStringHelper}
import org.enso.compiler.core.{IR, Identifier}
import org.enso.compiler.core.IR.randomId

import java.util.UUID

Expand All @@ -19,8 +18,8 @@ sealed case class Empty(
) extends IR
with Expression
with Diagnostic
with IRKind.Primitive {
var id: UUID @Identifier = randomId
with IRKind.Primitive
with LazyId {

/** Creates a copy of `this`
*
Expand Down Expand Up @@ -54,7 +53,7 @@ sealed case class Empty(
if (keepMetadata) passData.duplicate else new MetadataStorage(),
diagnostics =
if (keepDiagnostics) diagnostics.copy else DiagnosticStorage(),
id = if (keepIdentifiers) id else randomId
id = if (keepIdentifiers) id else null
)

/** @inheritdoc */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.enso.compiler.core.ir
import org.enso.compiler.core.Implicits.{ShowPassData, ToStringHelper}
import org.enso.compiler.core.IR
import org.enso.compiler.core.Identifier
import org.enso.compiler.core.IR.{indentLevel, mkIndent, randomId}
import org.enso.compiler.core.IR.{indentLevel, mkIndent}

import java.util.UUID
import scala.jdk.FunctionConverters.enrichAsScalaFromFunction
Expand Down Expand Up @@ -63,8 +63,8 @@ object Expression {
passData: MetadataStorage = new MetadataStorage(),
diagnostics: DiagnosticStorage = DiagnosticStorage()
) extends Expression
with IRKind.Primitive {
var id: UUID @Identifier = randomId
with IRKind.Primitive
with LazyId {

/** Creates a copy of `this`.
*
Expand Down Expand Up @@ -125,7 +125,7 @@ object Expression {
if (keepMetadata) passData.duplicate else new MetadataStorage(),
diagnostics =
if (keepDiagnostics) diagnostics.copy else DiagnosticStorage(),
id = if (keepIdentifiers) id else randomId
id = if (keepIdentifiers) id else null
)

/** @inheritdoc */
Expand Down Expand Up @@ -189,9 +189,8 @@ object Expression {
passData: MetadataStorage = new MetadataStorage(),
diagnostics: DiagnosticStorage = DiagnosticStorage()
) extends Expression
with IRKind.Primitive {

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

/** Creates a copy of `this`.
*
Expand Down Expand Up @@ -241,7 +240,7 @@ object Expression {
if (keepMetadata) passData.duplicate else new MetadataStorage(),
diagnostics =
if (keepDiagnostics) diagnostics.copy else DiagnosticStorage(),
id = if (keepIdentifiers) id else randomId
id = if (keepIdentifiers) id else null
)

/** @inheritdoc */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.enso.compiler.core.ir

import org.enso.compiler.core.Implicits.{ShowPassData, ToStringHelper}
import org.enso.compiler.core.{IR, Identifier}
import org.enso.compiler.core.IR.randomId

import java.util.UUID

Expand Down Expand Up @@ -51,7 +50,8 @@ object Function {
passData: MetadataStorage,
diagnostics: DiagnosticStorage
) extends Function
with IRKind.Primitive {
with IRKind.Primitive
with LazyId {
def this(
arguments: List[DefinitionArgument],
body: Expression,
Expand All @@ -62,8 +62,7 @@ object Function {
) = {
this(arguments, Seq(body), location, canBeTCO, passData, diagnostics)
}
var id: UUID @Identifier = randomId
override lazy val body = bodySeq.head
override lazy val body = bodySeq.head

/** Creates a copy of `this`.
*
Expand Down Expand Up @@ -118,7 +117,7 @@ object Function {
if (keepMetadata) passData.duplicate else new MetadataStorage(),
diagnostics =
if (keepDiagnostics) diagnostics.copy else DiagnosticStorage(),
id = if (keepIdentifiers) id else randomId
id = if (keepIdentifiers) id else null
)

/** @inheritdoc */
Expand Down Expand Up @@ -204,8 +203,8 @@ object Function {
passData: MetadataStorage = new MetadataStorage(),
diagnostics: DiagnosticStorage = DiagnosticStorage()
) extends Function
with IRKind.Sugar {
var id: UUID @Identifier = randomId
with IRKind.Sugar
with LazyId {

/** Creates a copy of `this`.
*
Expand Down Expand Up @@ -276,7 +275,7 @@ object Function {
if (keepMetadata) passData.duplicate else new MetadataStorage(),
diagnostics =
if (keepDiagnostics) diagnostics.copy else DiagnosticStorage(),
id = if (keepIdentifiers) id else randomId
id = if (keepIdentifiers) id else null
)

/** @inheritdoc */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.enso.compiler.core.ir

import org.enso.compiler.core.IR.randomId
import org.enso.compiler.core.{IR, Identifier}

import java.util.UUID

trait LazyId { self: IR =>
private[this] var _id: UUID @Identifier = null

protected def id: UUID @Identifier = {
_id
}

override def getId(): UUID @Identifier = {
if (_id == null) {
_id = randomId()
}
_id
}

def id_=(id: UUID @Identifier) = {
assert(_id == null)
_id = id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.enso.compiler.core.ir

import org.enso.compiler.core.Implicits.{ShowPassData, ToStringHelper}
import org.enso.compiler.core.{CompilerError, IR, Identifier}
import org.enso.compiler.core.IR.randomId

import java.util.UUID

Expand Down Expand Up @@ -42,8 +41,8 @@ object Literal {
location: Option[IdentifiedLocation],
passData: MetadataStorage = new MetadataStorage(),
diagnostics: DiagnosticStorage = DiagnosticStorage()
) extends Literal {
var id: UUID @Identifier = randomId
) extends Literal
with LazyId {

/** Creates a copy of `this`.
*
Expand Down Expand Up @@ -81,7 +80,7 @@ object Literal {
if (keepMetadata) passData.duplicate else new MetadataStorage(),
diagnostics =
if (keepDiagnostics) diagnostics.copy else DiagnosticStorage(),
id = if (keepIdentifiers) id else randomId
id = if (keepIdentifiers) id else null
)

/** @inheritdoc */
Expand Down Expand Up @@ -169,8 +168,8 @@ object Literal {
location: Option[IdentifiedLocation],
passData: MetadataStorage = new MetadataStorage(),
diagnostics: DiagnosticStorage = DiagnosticStorage()
) extends Literal {
var id: UUID @Identifier = randomId
) extends Literal
with LazyId {

/** Creates a copy of `this`.
*
Expand Down Expand Up @@ -206,7 +205,7 @@ object Literal {
if (keepMetadata) passData.duplicate else new MetadataStorage(),
diagnostics =
if (keepDiagnostics) diagnostics.copy else DiagnosticStorage(),
id = if (keepIdentifiers) id else randomId
id = if (keepIdentifiers) id else null
)

/** @inheritdoc */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.enso.compiler.core.ir

import org.enso.compiler.core.{IR, Identifier}
import org.enso.compiler.core.IR.randomId
import org.enso.compiler.core.Implicits.{ShowPassData, ToStringHelper}
import org.enso.compiler.core.ir.module.scope.{Definition, Export, Import}

Expand Down Expand Up @@ -32,8 +31,8 @@ final case class Module(
passData: MetadataStorage = new MetadataStorage(),
diagnostics: DiagnosticStorage = DiagnosticStorage()
) extends IR
with IRKind.Primitive {
var id: UUID @Identifier = randomId
with IRKind.Primitive
with LazyId {

/** Creates a copy of `this`.
*
Expand Down Expand Up @@ -98,7 +97,7 @@ final case class Module(
if (keepMetadata) passData.duplicate else new MetadataStorage(),
diagnostics =
if (keepDiagnostics) diagnostics.copy else DiagnosticStorage(),
id = if (keepIdentifiers) id else randomId
id = if (keepIdentifiers) id else null
)

/** @inheritdoc */
Expand Down
Loading

0 comments on commit 5b91f16

Please sign in to comment.