{"id":16678042,"url":"https://github.com/johnynek/inliner","last_synced_at":"2025-03-17T00:32:19.391Z","repository":{"id":66746754,"uuid":"47578409","full_name":"johnynek/inliner","owner":"johnynek","description":"scala macros to inline idiomatic scala for maximum performance","archived":false,"fork":false,"pushed_at":"2015-12-09T21:09:25.000Z","size":44,"stargazers_count":104,"open_issues_count":2,"forks_count":3,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-10-13T13:28:14.889Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/johnynek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-07T20:50:09.000Z","updated_at":"2022-08-24T07:00:38.000Z","dependencies_parsed_at":"2023-03-11T00:19:55.274Z","dependency_job_id":null,"html_url":"https://github.com/johnynek/inliner","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnynek%2Finliner","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnynek%2Finliner/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnynek%2Finliner/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnynek%2Finliner/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/johnynek","download_url":"https://codeload.github.com/johnynek/inliner/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221669335,"owners_count":16860855,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T13:28:13.181Z","updated_at":"2024-10-27T11:35:23.103Z","avatar_url":"https://github.com/johnynek.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Inliner\n[![Build\nStatus](https://travis-ci.org/johnynek/inliner.svg)](https://travis-ci.org/johnynek/inliner)\n\nInliner is a collection of scala macros to inline and optimize idiomatic scala into while loops or nested if/else statements. The purpose is to allow idiomatic scala without having to give up performance.\n\n## How to use?\nAdd `val inliner = ProjectRef(uri(\"git://github.com/johnynek/inliner.git\"), \"core\")` to your sbt project, then add `.dependsOn(inliner)`\nto any project where you want to use the code. Then do:\n```scala\nimport com.github.johnynek.inliner.{InlineArray, InlineCollection, InlineOption, InlineRange, InlineTry}\n// import the .inline enrichment:\nimport InlineArray._\nimport InlineCollection._\nimport InlineOption._\nimport InlineTry._\n```\nGenerally you import methods from an object and replace calls like `x.method` with `x.inline.method`. You can\nalso use the non-method syntax: `foldLeft(List(1, 2, 3), \"\")(_ + _)`\n\n### Collections\nThere are `.inline` versions of the following TraversableOnce methods: `find`, `forall`, `foldLeft`, `foreach`, `reduceOption`.\n```scala\nimport com.github.johnynek.inliner.InlineCollection._\nobject Test {\n  println(List(0, 1, 2, 4, 8, 16).inline.foldLeft(0)(_ + _))\n}\n```\nExpands the foldLeft into:\n```scala\n  val it$macro$1 = immutable.this.List.apply[Int](0, 1, 2, 4, 8, 16).toIterator;\n  var x$1 = 0;\n  while$1(){\n    if (it$macro$1.hasNext)\n      {\n        {\n          val x$2 = it$macro$1.next;\n          x$1 = x$1.+(x$2)\n        };\n        while$1()\n      }\n    else\n      ()\n  };\n  x$1\n```\n### scala.util.Try\nNormally, creating a Try means a call-by-name parameter, which requires an allocation and a method call. With a macro, we can directly inline into a try/catch block:\n```scala\nimport com.github.johnynek.inliner.InlineTry._\ndef halfEven(x: Int): Int = { require(x % 2 == 0, \"not even: \" + x); x/2 }\ninlineTry {\n  val x = halfEven(42)\n  val y = halfEven(43)\n  x * y\n}\n```\nwhich, at the REPL, expands to:\n```scala\ntry {\n  new _root_.scala.util.Success[Int]({\n    val x = $line4.$read.$iw.$iw.$iw.$iw.halfEven(42);\n    val y = $line4.$read.$iw.$iw.$iw.$iw.halfEven(43);\n    x.*(y)\n  })\n} catch {\n  case _root_.scala.util.control.NonFatal((e @ _)) =\u003e new scala.util.Failure[Nothing](e)\n}\n```\nSimilarly, for expressions can be expanded into nested if/else:\n```scala\n for {\n   a \u003c- inlineTry { assert(o1 \u003e 0); o1 }.inline\n   b \u003c- o2(a).inline\n   c \u003c- o3(b).inline\n } yield c\n```\nexpands to:\n```scala\n{\n  val opt$macro$13 = (try {\n    new scala.util.Success[Int]({\n      scala.this.Predef.assert(o1.\u003e(0));\n      o1\n    })\n  } catch {\n    case _root_.scala.util.control.NonFatal((e @ _)) =\u003e new scala.util.Failure[Nothing](e)\n  }: scala.util.Try[Int]);\n  if (opt$macro$13.isSuccess)\n    {\n      val a = opt$macro$13.get;\n      try {\n        ({\n          val opt$macro$12 = o2.apply(a);\n          if (opt$macro$12.isSuccess)\n            {\n              val b = opt$macro$12.get;\n              try {\n                (o3.apply(b): scala.util.Try[Long])\n              } catch {\n                case _root_.scala.util.control.NonFatal((e @ _)) =\u003e new scala.util.Failure[Nothing](e)\n              }\n            }\n          else\n            opt$macro$12.asInstanceOf[scala.util.Try[Long]]\n        }: scala.util.Try[Long])\n      } catch {\n        case _root_.scala.util.control.NonFatal((e @ _)) =\u003e new scala.util.Failure[Nothing](e)\n      }\n    }\n  else\n    opt$macro$13.asInstanceOf[_root_.scala.util.Try[Long]]\n}\n\n```\nThis gives you inlined versions of: `filter`, `flatMap`, `flatten`, `fold`, `foreach`, `getOrElse`, `map`, `orElse`.\n\n### Option\nSimilar to Try, you can inline for loops using `.inline` on methods `filter`, `flatMap`, `flatten`, `fold`, `foreach`, `getOrElse`, `map`, `orElse`.\n\n# When should I use Inliner?\nMacros are not as reliable as they could be. You should probably only use this library for inner loops that have been profiled. Optimizing without profiling is usually not profitable. Once you find a method that needs maximum optimization, Inliner may allow you to keep idiomatic code with minor modifications to get maximum performance.\n\n# Future Work\nCheck the issues, but generally support for more classes (such as Either) or constructs (such as PartialFunction literals) would be useful. Also, optimizing some of the trees would be really interesting. Once we have a full tree we can see that some of the branches will never be taken in large for-expressions. Also, we could port this approach to a whitebox macro such as `def inline(x: Any): Any` which could do whole expression optimization along the lines we have here without manually calling .inline. This would have the benefit of being able to optimize things like:\n```scala\nmyList\n  .map { x =\u003e (x, 1) }\n  .reduceOption { case (la, lb), (ra, rb) =\u003e (la + ra, lb + lb) }\n```\nto an expression like:\n```scala\nval it = myList.iterator\nif (it.hasNext) {\n  val head = it.next\n  var result1 = head\n  var result2 = 1\n  while(it.hasNext) {\n    val item = it.next\n    val item1 = item\n    val item2 = 1\n    result1 = result1 + item1\n    result2 = result2 + item2\n  }\n  Some((result1, result2))\n} else None\n```\n\n# Authors\nBest to check the commit history, but this was started by [Oscar Boykin](https://twitter.com/posco).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnynek%2Finliner","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohnynek%2Finliner","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnynek%2Finliner/lists"}