Skip to content

Commit

Permalink
merge: Some profile-guided optimization (#1305)
Browse files Browse the repository at this point in the history
Recursive `buildNested` has made the profiler output harder to read:

![image](https://github.com/user-attachments/assets/719c6741-7e96-4d56-9551-f121bc0bc9ce)

After turning it into iteration, it's easier:

![image](https://github.com/user-attachments/assets/2f785a9c-320d-49e3-849c-7eef762c30bf)

The `sourcePosForSubExpr` is taking a lot of time. Shit!
  • Loading branch information
ice1000 authored Feb 4, 2025
2 parents 5eb8aa7 + 0505c78 commit 06bc281
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ record Default(@NotNull MutableMap<Path, byte[]> output) implements AsmOutputCol
public Default() { this(MutableMap.create()); }

public static @NotNull Path from(@NotNull ImmutableSeq<String> components) {
return Path.of(components.getFirst(), components.view().drop(1).toArray(new String[components.size() - 1]));
return Path.of(components.getFirst(), components.view().drop(1).toArray(String[]::new));
}

public static @NotNull Path getPath(@NotNull ClassDesc className) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,8 @@ public AsmClassBuilder(
)));
}

public @NotNull ClassDesc owner() {
return classData.className();
}

public @NotNull ClassDesc ownerSuper() {
return classData.classSuper();
}
public @NotNull ClassDesc owner() { return classData.className(); }
public @NotNull ClassDesc ownerSuper() { return classData.classSuper(); }

@Override
public void buildNestedClass(@NotNull CompiledAya compiledAya, @NotNull String name, @NotNull Class<?> superclass, @NotNull Consumer<FreeClassBuilder> builder) {
Expand Down Expand Up @@ -178,7 +173,8 @@ public void postBuild() {
InnerClassInfo.of(owner().nested(cd), Optional.of(owner()), Optional.of(cd), ClassData.AF_NESTED));
} else {
assert nestedMembers.isEmpty();
innerClassesEntries = ImmutableSeq.of(InnerClassInfo.of(owner(), Optional.of(outerClassData.data().classSuper()), Optional.of(outerClassData.thisName()), ClassData.AF_NESTED));
innerClassesEntries = ImmutableSeq.of(InnerClassInfo.of(owner(), Optional.of(outerClassData.data().classSuper()),
Optional.of(outerClassData.thisName()), ClassData.AF_NESTED));
}

if (innerClassesEntries.isNotEmpty()) {
Expand Down
24 changes: 13 additions & 11 deletions syntax/src/main/java/org/aya/syntax/concrete/Expr.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import kala.collection.SeqView;
import kala.collection.immutable.ImmutableSeq;
import kala.collection.mutable.MutableArrayList;
import kala.control.Either;
import kala.value.MutableValue;
import org.aya.generic.AyaDocile;
Expand Down Expand Up @@ -449,8 +450,7 @@ public void forEach(@NotNull Consumer<Expr> f) {
///
/// @param generator `x * y` part above
/// @param binds `x <- [1, 2, 3], y <- [4, 5, 6]` part above
/// @param names the bind (`>>=`) function, it is [#monadBind] in default,
/// the pure (`return`) function, it is [#functorPure] in default
/// @param names bind (`>>=`) is [#monadBind] by default and pure (`return`) is [#functorPure] by default
/// @apiNote a ArrayCompBlock will be desugar to a do-block. For the example above,
/// it will be desugared to `do x <- [1, 2, 3], y <- [4, 5, 6], return x * y`
public record CompBlock(
Expand All @@ -474,9 +474,7 @@ public void forEach(@NotNull PosedConsumer<Expr> f) {
}
}

/**
* helper constructor, also find constructor calls easily in IDE
*/
/// Helper constructor, also find constructor calls easily in IDE
public static Array newList(@NotNull ImmutableSeq<WithPos<Expr>> exprs) {
return new Array(Either.right(new ElementList(exprs)));
}
Expand Down Expand Up @@ -639,11 +637,15 @@ record Match(
@NotNull WithPos<E> body,
@NotNull BiFunction<P, WithPos<E>, E> constructor
) {
if (params.isEmpty()) return body;
var drop = params.drop(1);
var subPos = body.sourcePos().sourcePosForSubExpr(sourcePos.file(),
drop.map(SourceNode::sourcePos));
return new WithPos<>(sourcePos, constructor.apply(params.getFirst(),
buildNested(subPos, drop, body, constructor)));
record StackData<P>(P p, SourcePos pos) { }
var subPoses = MutableArrayList.<StackData<P>>create();
while (!params.isEmpty()) {
subPoses.append(new StackData<>(params.getFirst(), sourcePos));
params = params.drop(1);
sourcePos = body.sourcePos().sourcePosForSubExpr(sourcePos.file(),
params.map(SourceNode::sourcePos));
}
return subPoses.foldRight(body, (data, acc) ->
new WithPos<>(data.pos, constructor.apply(data.p, acc)));
}
}
29 changes: 7 additions & 22 deletions syntax/src/main/java/org/aya/syntax/ref/ModulePath.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.syntax.ref;

Expand All @@ -10,37 +10,22 @@
import java.io.Serializable;

public record ModulePath(@NotNull ImmutableSeq<String> module) implements Serializable {
public static @NotNull ModulePath of(@NotNull String... names) {
return new ModulePath(ImmutableSeq.from(names));
}
public static @NotNull ModulePath of(@NotNull String... names) { return new ModulePath(ImmutableSeq.from(names)); }

public boolean isInModule(@NotNull ModulePath other) {
var moduleName = other.module;
if (module.sizeLessThan(moduleName.size())) return false;
return module.sliceView(0, moduleName.size()).sameElements(moduleName);
}

public ModulePath dropLast(int n) {
return new ModulePath(module.dropLast(n));
}

public boolean sameElements(ModulePath other) {
return module.sameElements(other.module);
}

public @NotNull ModuleName.Qualified asName() {
return ModuleName.qualified(module);
}

public @NotNull ModulePath derive(@NotNull String modName) {
return new ModulePath(module.appended(modName));
}

public ModulePath dropLast(int n) { return new ModulePath(module.dropLast(n)); }
public boolean sameElements(ModulePath other) { return module.sameElements(other.module); }
public @NotNull ModuleName.Qualified asName() { return ModuleName.qualified(module); }
public @NotNull ModulePath derive(@NotNull String modName) { return new ModulePath(module.appended(modName)); }
public @NotNull ModulePath derive(@NotNull ModulePath modName) {
return new ModulePath(module.concat(modName.module));
}

@Override public String toString() { return QualifiedID.join(module); }
@Override public @NotNull String toString() { return QualifiedID.join(module); }
public boolean isEmpty() { return module.isEmpty(); }
public int size() { return module.size(); }
public @NotNull String last() { return module.getLast(); }
Expand Down
12 changes: 4 additions & 8 deletions tools/src/main/java/org/aya/util/error/SourcePos.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.util.error;

import java.util.Objects;

import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.LineColumn;
import com.intellij.openapi.util.text.StringUtil;
import kala.collection.SeqView;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
* Position in source code for error reporting only.
*
Expand Down Expand Up @@ -108,10 +108,6 @@ public boolean oneLinear() {
return startLine == endLine;
}

public @NotNull SourcePos sourcePosForSubExpr(@NotNull SeqView<SourcePos> params) {
return sourcePosForSubExpr(file, params);
}

public @NotNull SourcePos sourcePosForSubExpr(@NotNull SourceFile sourceFile, @NotNull SeqView<SourcePos> params) {
var restParamSourcePos = params.fold(SourcePos.NONE, (acc, it) -> {
if (acc == SourcePos.NONE) return it;
Expand All @@ -129,7 +125,7 @@ public boolean oneLinear() {
);
}

@Override public String toString() {
@Override public @NotNull String toString() {
return "(" + tokenStartIndex + "-" + tokenEndIndex + ") [" + lineColumnString() + ']';
}
public @NotNull String lineColumnString() {
Expand Down

0 comments on commit 06bc281

Please sign in to comment.