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

Encapsulating array-like data and operations into a single package #7544

Merged
merged 41 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
2201637
Moving all array-like data classes into the same package
JaroslavTulach Aug 10, 2023
b7efd8d
Unifying configuration of WithDebugCommand with annotation processors
JaroslavTulach Aug 10, 2023
22f9714
Hiding ArrayProxy as package private class
JaroslavTulach Aug 10, 2023
d4c1140
Let Meta*Test pass OK
JaroslavTulach Aug 11, 2023
91204f4
Enso objects shall extend EnsoObject
JaroslavTulach Aug 11, 2023
8974b3c
ArraySlice doesn't have to be public
JaroslavTulach Aug 11, 2023
58d661c
Less methods in Array
JaroslavTulach Aug 11, 2023
db95190
Replacing Array public methods with ArrayLikeLengthNode
JaroslavTulach Aug 11, 2023
e7c297e
ArrayLikeAtNode unifies access to index
JaroslavTulach Aug 11, 2023
8fc5995
Hiding all public methods of Vector
JaroslavTulach Aug 11, 2023
cc9df48
Fixing runtime/bench
JaroslavTulach Aug 11, 2023
0d64ede
Hiding Array.getItems() from public
JaroslavTulach Aug 11, 2023
5c7b9cd
Renaming to ArrayLikeCoerceToArrayNode
JaroslavTulach Aug 11, 2023
4d80810
Renaming to ArrayLikeCopyToArrayNode
JaroslavTulach Aug 11, 2023
f128f8b
Simplifying Flatten with the help of ArrayLikeAtNode and ArrayLikeLen…
JaroslavTulach Aug 11, 2023
660a938
Reformatting Java code
JaroslavTulach Aug 11, 2023
63941a6
Removing check for UnsupportedMessageException
JaroslavTulach Aug 11, 2023
359a608
Avoid querying length until really needed
JaroslavTulach Aug 12, 2023
7d9026f
Insert the newly created node before it is used
JaroslavTulach Aug 13, 2023
1dd97a8
Simplify various builtins and make Array package private
JaroslavTulach Aug 13, 2023
b5f5fdd
Undo b7efd8d
JaroslavTulach Aug 13, 2023
4c3e383
Making Vector package private class
JaroslavTulach Aug 13, 2023
d098ae5
WIP: Speeding up ArrayProxyBenchmarks.sumOverVectorBackedByDelegating…
JaroslavTulach Aug 13, 2023
56aabdf
Let's call it VectorLong so it ends next to Vector alphabetically
JaroslavTulach Aug 13, 2023
d68ab5b
Recognize also double[]
JaroslavTulach Aug 13, 2023
abe16a7
Refactoring into many specialized innerclasses
JaroslavTulach Aug 13, 2023
0433105
Specialize length and at from Double and Long vectors
JaroslavTulach Aug 13, 2023
6181b10
Create pure EnsoOnly vector from values that need no conversions
JaroslavTulach Aug 13, 2023
3c439ae
Place Warnings into Vector.Generic
JaroslavTulach Aug 14, 2023
685580a
Turning ArrayBuilder into a builtin
JaroslavTulach Aug 14, 2023
33b1cb6
Support for Vector_Builder.appendTo
JaroslavTulach Aug 15, 2023
ba8931b
Merging with #7525
JaroslavTulach Aug 15, 2023
fcad022
Documenting signatures of Array_Like_Helpers functions
JaroslavTulach Aug 15, 2023
2af6be8
Reformatted
JaroslavTulach Aug 15, 2023
232eb1c
Removing needless Javadoc
JaroslavTulach Aug 15, 2023
3c99eeb
It's a fallback specification
JaroslavTulach Aug 15, 2023
3ce8e17
Avoid shouldNotReachHere. Use panics from ArrayPanics.
JaroslavTulach Aug 15, 2023
969d8b2
One method less
JaroslavTulach Aug 15, 2023
9bfa61e
Removing useless documentation
JaroslavTulach Aug 15, 2023
24309fc
Using @Fallback to negate previous specializations
JaroslavTulach Aug 15, 2023
dca7e99
Throw IAE, convert asGuestValue, convert to Illegal_Argument
JaroslavTulach Aug 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import project.Errors.Unimplemented.Unimplemented
import project.Meta
import project.Nothing.Nothing
import project.Panic.Panic
import project.Internal.Array_Like_Helpers
from project.Data.Boolean import Boolean, False, True
from project.Data.Index_Sub_Range import Index_Sub_Range
from project.Data.Range.Extensions import all
Expand All @@ -42,7 +43,7 @@ type Array

[1, 2, 3].to_array.at -1 == 3
at : Integer -> Any ! Index_Out_Of_Bounds
at self index = @Builtin_Method "Array.at"
at self index = Array_Like_Helpers.at self index

## Returns the number of elements stored in this array.

Expand All @@ -51,7 +52,7 @@ type Array

[1, 2, 3, 4].to_array.length
length : Integer
length self = @Builtin_Method "Array.length"
length self = Array_Like_Helpers.length self

## Identity.
This method is implemented purely for completeness with the runtime's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import project.Data.Array.Array
import project.Data.Numbers.Integer
import project.Errors.Illegal_Argument.Illegal_Argument

import project.Internal.Array_Like_Helpers

## PRIVATE
A helper type used for creating an array from a length and a callback
providing its elements.
Expand All @@ -12,8 +14,10 @@ import project.Errors.Illegal_Argument.Illegal_Argument
type returned from polyglot calls to other languages. The most common
use-case for it is to pass it to `Vector.from_polyglot_array` to create a
vector backed by such custom storage.
@Builtin_Type
type Array_Proxy
## PRIVATE
Value ignore
Copy link
Member

Choose a reason for hiding this comment

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

What is the reason for this ignored field?

Copy link
Member Author

Choose a reason for hiding this comment

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

Removing the Value ignore constructor turns Array_Proxy into a singleton and fails:

[error] Test org.enso.interpreter.test.MetaIsATest.typesAreNotInstancesOfThemselves failed: java.lang.AssertionError: 
[error] Type Array_Proxy shall not be instance of itself expected:
[error]     at org.enso.interpreter.test.MetaIsATest.typesAreNotInstancesOfThemselves(MetaIsATest.java:238)

I decided to add a constructor to "silent" this tests.

The proper fix is to remove the type Array_Proxy altogether, I think. It is not a type anyway. It is just a place for the new and from_proxy_object methods. However I happily leave this for @jdunkerley, @radeusgd and for another pull request.


## PRIVATE
ADVANCED
Creates a new `Array_Proxy` from a length and a callback.
Expand All @@ -28,21 +32,12 @@ type Array_Proxy
Vector.from_polyglot_array (Array_Proxy.new 10000000 (i -> i))
new : Integer -> (Integer -> Any) -> Array
new length at = Illegal_Argument.handle_java_exception <|
Array_Proxy.new_builtin length at

## PRIVATE
The builtin cannot throw `Illegal_Argument` as that type cannot be a
builtin, because they do not support default arguments in constructors.
So a wrapper is used which will translate a Java exception into the Enso
one. This indirection may be removed once the following story is done:
https://www.pivotaltracker.com/n/projects/2539304/stories/183817602
new_builtin : Integer -> (Integer -> Any) -> Array
new_builtin length at = @Builtin_Method "Array_Proxy.new_builtin"
Array_Like_Helpers.new_array_proxy_builtin length at

## PRIVATE
ADVANCED
Creates a new `Array_Proxy` from an object providing `length` and `at`
methods.
from_proxy_object : Any -> Array
from_proxy_object proxy =
Array_Proxy.new proxy.length proxy.at
Array_Like_Helpers.new_array_proxy_builtin proxy.length proxy.at
10 changes: 6 additions & 4 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Vector.enso
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import project.Nothing.Nothing
import project.Panic.Panic
import project.Random
import project.Warning.Warning
import project.Internal.Array_Like_Helpers

from project.Data.Boolean import Boolean, False, True
from project.Data.Filter_Condition import unify_condition_or_predicate, unify_condition_predicate_or_element
from project.Data.Index_Sub_Range import drop_helper, Index_Sub_Range, take_helper
Expand Down Expand Up @@ -69,7 +71,7 @@ type Vector a

Vector.new my_vec.length (ix -> my_vec.at ix)
new : Integer -> (Integer -> Any) -> Vector Any
new length constructor = @Builtin_Method "Vector.new"
new length constructor = Array_Like_Helpers.vector_from_function length constructor

## PRIVATE
ADVANCED
Expand Down Expand Up @@ -161,7 +163,7 @@ type Vector a
## PRIVATE
ADVANCED
Copies content of a vector into an Array.
to_array self = @Builtin_Method "Vector.to_array"
to_array self = Array_Like_Helpers.vector_to_array self

## Returns the number of elements stored in this vector.

Expand All @@ -170,7 +172,7 @@ type Vector a

[1, 2, 3, 4].length
length : Integer
length self = @Builtin_Method "Vector.length"
length self = Array_Like_Helpers.length self

## Gets an element from the vector at a specified index (0-based).

Expand All @@ -189,7 +191,7 @@ type Vector a

[1, 2, 3].at -1 == 3
at : Integer -> Any ! Index_Out_Of_Bounds
at self index = @Builtin_Method "Vector.at"
at self index = Array_Like_Helpers.at self index

## Gets an element from the vector at a specified index (0-based).
If the index is invalid then `if_missing` is returned.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import project.Any.Any
import project.Data.Array.Array
import project.Data.Numbers.Integer

## PRIVATE
The builtin cannot throw `Illegal_Argument` as that type cannot be a
builtin, because they do not support default arguments in constructors.
So a wrapper is used which will translate a Java exception into the Enso
one. This indirection may be removed once the following story is done:
https://www.pivotaltracker.com/n/projects/2539304/stories/183817602
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved
new_array_proxy_builtin : Integer -> (Integer -> Any) -> Array
new_array_proxy_builtin length at = @Builtin_Method "Array_Like_Helpers.new_array_proxy_builtin"

length array_like = @Builtin_Method "Array_Like_Helpers.length"
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved
at array_like index = @Builtin_Method "Array_Like_Helpers.at"

vector_to_array array_like = @Builtin_Method "Array_Like_Helpers.vector_to_array"
vector_from_function = @Builtin_Method "Array_Like_Helpers.vector_from_function"
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* A primitive boxed array type to be used only in EPB.
*
* <p>{@link ReadOnlyArray} is essentially a stripped-down, read-only, version of {@link
* org.enso.interpreter.runtime.data.Array}, used for passing arguments. The latter cannot be used
* in EPB because EPB is a dependency of runtime.
* org.enso.interpreter.runtime.data.vector.Array}, used for passing arguments. The latter cannot be
* used in EPB because EPB is a dependency of runtime.
*/
@ExportLibrary(InteropLibrary.class)
public final class ReadOnlyArray implements TruffleObject {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package org.enso.interpreter.bench.benchmarks.semantic;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import org.enso.interpreter.test.TestBase;
import org.enso.polyglot.MethodNames.Module;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.io.IOAccess;
import org.openjdk.jmh.annotations.Benchmark;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.source.SourceSection;

import org.enso.interpreter.runtime.callable.function.Function;
import org.enso.interpreter.runtime.tag.IdentifiedTag;

import java.util.Arrays;
import java.util.UUID;

import org.enso.interpreter.node.ClosureRootNode;
import org.enso.interpreter.runtime.data.EnsoObject;
import org.enso.interpreter.runtime.tag.AvoidIdInstrumentationTag;

/**
Expand Down Expand Up @@ -56,7 +59,7 @@ public boolean isInstrumentable() {

/** A simple value class for function call information. */
@ExportLibrary(InteropLibrary.class)
public static final class FunctionCall implements TruffleObject {
public static final class FunctionCall implements EnsoObject {
private final Function function;
private final Object state;
private final @CompilerDirectives.CompilationFinal(dimensions = 1) Object[] arguments;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.enso.interpreter.node.callable;

import org.enso.interpreter.node.ExpressionNode;
import org.enso.interpreter.runtime.data.Array;
import org.enso.interpreter.runtime.data.Vector;
import org.enso.interpreter.runtime.data.vector.ArrayLikeHelpers;
import org.enso.interpreter.runtime.error.PanicSentinel;

import com.oracle.truffle.api.frame.VirtualFrame;
Expand Down Expand Up @@ -43,6 +42,6 @@ public Object executeGeneric(VirtualFrame frame) {
throw sentinel;
}
}
return Vector.fromArray(new Array(itemValues));
return ArrayLikeHelpers.asVectorWithCheckAt(itemValues);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.enso.interpreter.node.controlflow.caseexpr;

import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.Node;

import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.EnsoObject;

record BranchResult(boolean isMatched, Object result) implements TruffleObject {
record BranchResult(boolean isMatched, Object result) implements EnsoObject {
static BranchResult failure(Node node) {
return new BranchResult(false, EnsoContext.get(node).getBuiltins().nothing());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import org.enso.interpreter.node.expression.builtin.runtime.GetStackTraceNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.data.Array;
import org.enso.interpreter.runtime.data.EnsoObject;
import org.enso.interpreter.runtime.error.PanicException;

@BuiltinMethod(
type = "Panic",
name = "primitive_get_attached_stack_trace",
description = "Gets the stack trace attached to the throwable.")
public class GetAttachedStackTraceNode extends Node {
Array execute(@AcceptsError Object error) {
EnsoObject execute(@AcceptsError Object error) {
if (error instanceof Throwable) {
return GetStackTraceNode.stackTraceToArray((Throwable) error);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
package org.enso.interpreter.node.expression.builtin.immutable;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.interop.syntax.HostValueToEnsoNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.Vector;
import org.enso.interpreter.runtime.data.vector.ArrayLikeAtNode;
import org.enso.interpreter.runtime.data.vector.ArrayLikeLengthNode;
import org.enso.interpreter.runtime.error.DataflowError;
import org.enso.interpreter.runtime.error.WarningsLibrary;

@BuiltinMethod(
type = "Vector",
type = "Array_Like_Helpers",
name = "at",
description = "Returns an element of Vector at the specified index.")
Copy link
Contributor

Choose a reason for hiding this comment

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

probably needs an update as well

Copy link
Member Author

Choose a reason for hiding this comment

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

What kind of update would you suggest, @hubertp?

public class AtVectorNode extends Node {
private @Child InteropLibrary interop = InteropLibrary.getFactory().createDispatched(3);
private @Child WarningsLibrary warnings = WarningsLibrary.getFactory().createDispatched(3);
private @Child HostValueToEnsoNode convert = HostValueToEnsoNode.build();
private @Child ArrayLikeAtNode at = ArrayLikeAtNode.create();
private @Child ArrayLikeLengthNode length;

Object execute(Vector self, long index) {
Object execute(Object arrayLike, long index) {
try {
return readElement(self, index);
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreter();
throw new IllegalStateException(e);
long actualIndex = index < 0 ? index + len(arrayLike) : index;
return at.executeAt(arrayLike, actualIndex);
} catch (InvalidArrayIndexException e) {
var len = len(arrayLike);
var ctx = EnsoContext.get(this);
var payload = ctx.getBuiltins().error().makeIndexOutOfBounds(index, len);
return DataflowError.withoutTrace(payload, this);
}
}

private Object readElement(Vector self, long index) throws UnsupportedMessageException {
try {
long actualIndex = index < 0 ? index + self.length(interop) : index;
return self.readArrayElement(actualIndex, interop, warnings, convert);
} catch (InvalidArrayIndexException e) {
EnsoContext ctx = EnsoContext.get(this);
return DataflowError.withoutTrace(
ctx.getBuiltins().error().makeIndexOutOfBounds(index, self.length(interop)), this);
private long len(Object arrayLike) {
if (length == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
length = insert(ArrayLikeLengthNode.create());
}
return length.executeLength(arrayLike);
}
}
Loading
Loading