Skip to content

Instantly share code, notes, and snippets.

@chrisseaton
Created August 11, 2017 10:15
Show Gist options
  • Save chrisseaton/f090c14400a46503fcc39d6a43ea195a to your computer and use it in GitHub Desktop.
Save chrisseaton/f090c14400a46503fcc39d6a43ea195a to your computer and use it in GitHub Desktop.
@Specialisation(guards = "f.arity < args.length")
curry(Function f, Object[] args,
@Cached("createCallNode()") CallNode callNode) {
callNode.curry(f, args);
}
@Specialisation(guards = "f.arity == args.length")
exact(Function f, Object[] args,
@Cached("createCallNode()") CallNode callNode) {
callNode.call(f, args);
}
@ExplodeLoop
@Specialisation(guards = {"f.arity > args.length", "f.arity == cachedArity" "args.length == cachedArgsLength"})
over(Function f, Object[] args,
@Cached("f.arity") int cachedArity,
@Cached("args.length") int cachedArgsLength,
@Cached("createCallNode()") CallNode callNode) {
Function f = callNode.call(f, Arrays.copyOfRange(args, 0, cachedArity));
// Can explode becuase cachedArity and cachedArgsLength are now constant
for (int n = cachedArity; n < cachedArgsLength - 1) {
f = callNode.call(f, args[n]);
}
return callNode.call(f, args[cachedArgsLength - 1]);
}
@Specialisation(guards = {"f.arity > args.length"}, contains="over")
overFallback(Function f, Object[] args,
@Cached("createCallNode()") CallNode callNode) {
Function f = callNode.call(f, Arrays.copyOfRange(args, 0, cachedArity));
// Can't explode - these will need to be indirect calls which can't be inlined
for (int n = cachedArity; n < cachedArgsLength - 1) {
f = callNode.call(f, args[n]);
}
return callNode.call(f, args[cachedArgsLength - 1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment