-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Normalize index in Vector.at in builtin method
The main culprit of a Vector slowdown (when compared to Array) was the normalization of the index when accessing the elements. Turns out that the Graal was very persistent on **not** inlining that particular fragment and that was degrading the results in benchmarks. Being unable to force it to do it (looks like a combination of thunk execution and another layer of indirection) we resorted to just moving the normalization to the builtin method. That makes Array and Vector perform roughly the same.
- Loading branch information
Showing
2 changed files
with
34 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...me/src/main/java/org/enso/interpreter/node/expression/builtin/immutable/AtVectorNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.enso.interpreter.node.expression.builtin.immutable; | ||
|
||
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.Context; | ||
import org.enso.interpreter.runtime.data.Vector; | ||
import org.enso.interpreter.runtime.error.PanicException; | ||
|
||
@BuiltinMethod( | ||
type = "Vector", | ||
name = "at_builtin", | ||
description = "Returns an element of Vector at the specified index.") | ||
public class AtVectorNode extends Node { | ||
private @Child InteropLibrary interop = InteropLibrary.getFactory().createDispatched(3); | ||
private @Child HostValueToEnsoNode toEnso = HostValueToEnsoNode.build(); | ||
|
||
Object execute(Vector self, long index) { | ||
try { | ||
long actualIndex = index < 0 ? index + self.length(interop) : index; | ||
return self.readArrayElement(actualIndex, interop, toEnso); | ||
} catch (InvalidArrayIndexException e) { | ||
Context ctx = Context.get(this); | ||
throw new PanicException( | ||
ctx.getBuiltins().error().makeInvalidArrayIndexError(self, index), this); | ||
} catch (UnsupportedMessageException e) { | ||
throw new PanicException(e.getMessage(), this); | ||
} | ||
} | ||
} |