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

Implemented optimised map directly with quoted matching #1

Merged
merged 1 commit into from Mar 26, 2020
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -23,33 +23,26 @@ object PipeExample {
// }

import scala.tasty._
case class Mapped(first: Pipe[Int], fn: Int => Int) extends Pipe[Int] {
case class Mapped[A, B](first: Pipe[A], fn: A => B) extends Pipe[B] {
def toList = first.toList.map(fn)
}

def mapImpl (first: Expr[Pipe[Int]], fn: Expr[Int => Int])(using qctx: QuoteContext): Expr[Pipe[Int]] = {
import qctx.tasty._
def go(trm : Term): Expr[Pipe[Int]] = {
import qctx.tasty._
trm match {
case Typed(t, _) =>
go(t)
case Inlined(_, _, t) =>
go(t)
case Apply(mapped, args@List(iFirst, iFn)) =>
// qctx.tasty.warning(s"debug4 ${mapped.showExtractors} ", mapped.pos)
// qctx.warning(s"debug5 ${args.map(_.showExtractors)} ${args.length}")
'{ Mapped(${iFirst.seal.cast[Pipe[Int]]}, ${iFn.seal.cast[Int => Int]} andThen $fn) }
case _ =>
//qctx.tasty.warning(s"debug6 ${trm.showExtractors}", trm.pos)
'{ Mapped($first, $fn) }
}

private def mapImpl[A: Type, B: Type](first: Expr[Pipe[A]], fn: Expr[A => B])(using qctx: QuoteContext): Expr[Pipe[B]] =
first match {
case '{ Mapped.apply[$a, A]($first, $lfn): Pipe[A] } =>
mapImpl(first, '{ (aa: $a) => ${ Expr.betaReduce(fn)(Expr.betaReduce(lfn)('{ aa })) } })
case c =>
qctx.warning(s"Got unsupported expression: ${c.show}", c)

This comment has been minimized.

Copy link
@Blaisorblade

Blaisorblade Mar 26, 2020

Owner

This shouldn't be a warning, should try? Some calls to map are done on lists and shouldn't be optimized...

This comment has been minimized.

Copy link
@Blaisorblade

Blaisorblade Mar 26, 2020

Owner

(I can fix this myself, if I'm not missing anything.)

'{ Mapped[A, B]($first, $fn) }
}
go(first.unseal)

private def logTree[A](expr: Expr[A])(using QuoteContext): Expr[A] = {
println(s"Optimised code: ${expr.show}")
expr
}

extension pipeOps {
inline def (inline first: => Pipe[Int]).map(inline fn: => Int => Int): Pipe[Int] =
${ mapImpl('{ first }, '{ fn }) }
inline def [A, B](inline first: Pipe[A]).map(inline fn: A => B): Pipe[B] =
${ logTree(mapImpl('{ first }, '{ fn })) }
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.