From fe9734055d9f5879518369b5e3b9c960b6272995 Mon Sep 17 00:00:00 2001 From: HoshinoTented Date: Mon, 6 Jan 2025 18:00:40 +0800 Subject: [PATCH 1/2] fix: #1274 --- .../java/org/aya/compiler/free/Constants.java | 10 +++-- .../serializers/AbstractExprializer.java | 41 +++++++++++++++++-- .../serializers/PatternSerializer.java | 2 +- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/jit-compiler/src/main/java/org/aya/compiler/free/Constants.java b/jit-compiler/src/main/java/org/aya/compiler/free/Constants.java index 2528c921c2..45a6df8f25 100644 --- a/jit-compiler/src/main/java/org/aya/compiler/free/Constants.java +++ b/jit-compiler/src/main/java/org/aya/compiler/free/Constants.java @@ -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.compiler.free; @@ -39,6 +39,8 @@ private Constants() { } public static final @NotNull ClassDesc CD_MutableSeq = FreeUtil.fromClass(MutableSeq.class); public static final @NotNull ClassDesc CD_Thunk = FreeUtil.fromClass(Supplier.class); public static final @NotNull ClassDesc CD_Result = FreeUtil.fromClass(Result.class); + public static final @NotNull String NAME_OF = "of"; + public static final @NotNull String NAME_EMPTY = "empty"; // Term -> Term public static final @NotNull MethodRef CLOSURE = new MethodRef( @@ -63,10 +65,10 @@ private Constants() { } true ); - // ImmutableSeq from(Object[]) + /// @see ImmutableSeq#of(Object[]) public static final @NotNull MethodRef IMMSEQ = new MethodRef( CD_ImmutableSeq, - "from", + NAME_OF, CD_ImmutableSeq, ImmutableSeq.of(ConstantDescs.CD_Object.arrayType()), true ); @@ -110,7 +112,7 @@ private Constants() { } public static final @NotNull MethodRef IMMTREESEQ = new MethodRef( FreeUtil.fromClass(ImmutableTreeSeq.class), - "from", + NAME_OF, FreeUtil.fromClass(ImmutableTreeSeq.class), ImmutableSeq.of(ConstantDescs.CD_Object.arrayType()), false diff --git a/jit-compiler/src/main/java/org/aya/compiler/serializers/AbstractExprializer.java b/jit-compiler/src/main/java/org/aya/compiler/serializers/AbstractExprializer.java index 058a77cc05..de2448e7e3 100644 --- a/jit-compiler/src/main/java/org/aya/compiler/serializers/AbstractExprializer.java +++ b/jit-compiler/src/main/java/org/aya/compiler/serializers/AbstractExprializer.java @@ -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.compiler.serializers; @@ -10,6 +10,7 @@ import org.jetbrains.annotations.NotNull; import java.lang.constant.ClassDesc; +import java.lang.constant.ConstantDescs; public abstract class AbstractExprializer { protected final @NotNull FreeExprBuilder builder; @@ -39,14 +40,48 @@ public abstract class AbstractExprializer { return makeImmutableSeq(builder, Constants.IMMSEQ, typeName, terms); } + /// @param con the factory function used for constructing an {@link ImmutableSeq}, usually it is {@link ImmutableSeq#of}. + /// This function may re-resolve the factory function to fixed size parameters one + /// with the name of {@param con} + /// in case {@param terms} is very small. + /// @see ImmutableSeq#of() + /// @see ImmutableSeq#of(Object) + /// @see ImmutableSeq#of(Object, Object) + /// @see ImmutableSeq#of(Object, Object, Object) + /// @see ImmutableSeq#of(Object, Object, Object, Object) + /// @see ImmutableSeq#of(Object, Object, Object, Object, Object) + /// @see ImmutableSeq#of(Object[]) public static @NotNull FreeJavaExpr makeImmutableSeq( @NotNull FreeExprBuilder builder, @NotNull MethodRef con, @NotNull Class typeName, @NotNull ImmutableSeq terms ) { - var args = builder.mkArray(FreeUtil.fromClass(typeName), terms.size(), terms); - return builder.invoke(con, ImmutableSeq.of(args)); + ImmutableSeq args; + + if (terms.size() <= 5) { + String name = con.name(); + ImmutableSeq params; + + // re-resolve + if (terms.isEmpty()) { + name = Constants.NAME_EMPTY; + params = ImmutableSeq.empty(); + } else { + params = ImmutableSeq.fill(terms.size(), ConstantDescs.CD_Object); + } + + con = FreeJavaResolver.resolve( + con.owner(), name, + con.returnType(), params, + con.isInterface()); + + args = terms; + } else { + args = ImmutableSeq.of(builder.mkArray(FreeUtil.fromClass(typeName), terms.size(), terms)); + } + + return builder.invoke(con, args); } /** diff --git a/jit-compiler/src/main/java/org/aya/compiler/serializers/PatternSerializer.java b/jit-compiler/src/main/java/org/aya/compiler/serializers/PatternSerializer.java index 09be664012..359f598666 100644 --- a/jit-compiler/src/main/java/org/aya/compiler/serializers/PatternSerializer.java +++ b/jit-compiler/src/main/java/org/aya/compiler/serializers/PatternSerializer.java @@ -264,7 +264,7 @@ public PatternSerializer serialize(@NotNull FreeCodeBuilder builder, @NotNull Im assert i > 0; var realIdx = i - 1; unit.get(realIdx).onSucc.accept(this, mBuilder, bindSize.get(realIdx)); - }, builder1 -> builder1.unreachable()); + }, FreeCodeBuilder::unreachable); return this; } From 511c97f59fece4334802bc1b86eba3a9456903d6 Mon Sep 17 00:00:00 2001 From: HoshinoTented Date: Mon, 6 Jan 2025 18:02:14 +0800 Subject: [PATCH 2/2] misc: oops --- .../java/org/aya/compiler/serializers/AbstractExprializer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jit-compiler/src/main/java/org/aya/compiler/serializers/AbstractExprializer.java b/jit-compiler/src/main/java/org/aya/compiler/serializers/AbstractExprializer.java index de2448e7e3..00da58b6bd 100644 --- a/jit-compiler/src/main/java/org/aya/compiler/serializers/AbstractExprializer.java +++ b/jit-compiler/src/main/java/org/aya/compiler/serializers/AbstractExprializer.java @@ -44,7 +44,7 @@ public abstract class AbstractExprializer { /// This function may re-resolve the factory function to fixed size parameters one /// with the name of {@param con} /// in case {@param terms} is very small. - /// @see ImmutableSeq#of() + /// @see ImmutableSeq#empty() /// @see ImmutableSeq#of(Object) /// @see ImmutableSeq#of(Object, Object) /// @see ImmutableSeq#of(Object, Object, Object)