Skip to content

Commit c060b41

Browse files
authored
[data] minor macro optimizations (#1079)
### Problem I'm exploring ways to reduce compilation times and noticed a few optimization opportunities in macros. ### Solution ### Notes <!-- Add any important additional information as bullet points, such as: - Implementation details reviewers should know about - Open questions and concerns - Limitations -->
1 parent 6303ede commit c060b41

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

kyo-data/shared/src/main/scala/kyo/Frame.scala

+15-15
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,11 @@ object Frame:
107107
private def frameImpl(internal: Boolean)(using Quotes): Expr[String] =
108108
import quotes.reflect.*
109109

110-
val pos = quotes.reflect.Position.ofMacroExpansion
111-
val (startLine, startColumn) = (pos.startLine, pos.startColumn)
112-
val endLine = pos.endLine
110+
val pos = quotes.reflect.Position.ofMacroExpansion
111+
val fileName = pos.sourceFile.name
113112

114113
if !internal then
115114
findEnclosing { sym =>
116-
val fileName = pos.sourceFile.name
117115
sym.fullName.startsWith("kyo") && !allowKyoFileSuffixes.exists(fileName.endsWith)
118116
}.foreach { sym =>
119117
report.errorAndAbort(
@@ -129,19 +127,21 @@ object Frame:
129127
}
130128
end if
131129

132-
val fileContent =
133-
pos.sourceFile.content
134-
.getOrElse(report.errorAndAbort("Can't locate source code to generate frame."))
130+
val cls = findEnclosing(_.isClassDef).map(_.fullName).getOrElse("?")
131+
val method = if internal then "?" else findEnclosing(_.isDefDef).map(_.name).getOrElse("?")
135132

136-
val markedContent = fileContent.take(pos.end) + "📍" + fileContent.drop(pos.end)
137-
val lines = markedContent.linesIterator.toList
138-
val snippetLines = lines.slice(startLine - 1, endLine + 2).filter(_.exists(_ != ' '))
139-
val toDrop = snippetLines.map(_.takeWhile(_ == ' ').size).minOption.getOrElse(0)
140-
val snippet = if internal then "<internal>" else snippetLines.map(_.drop(toDrop)).mkString("\n")
141-
val cls = findEnclosing(_.isClassDef).map(render).getOrElse("?")
142-
val method = findEnclosing(_.isDefDef).map(render).getOrElse("?")
133+
val snippet =
134+
if internal then "<internal>"
135+
else
136+
val content = pos.sourceFile.content.getOrElse(
137+
report.errorAndAbort("Can't locate source code")
138+
)
139+
val marked = content.take(pos.end) + "📍" + content.drop(pos.end)
140+
val lines = marked.linesIterator.slice(pos.startLine - 1, pos.endLine + 2).filter(_.exists(_ != ' ')).toSeq
141+
val toDrop = lines.map(_.takeWhile(_ == ' ').length).min
142+
lines.map(_.drop(toDrop)).mkString("\n")
143143

144-
Expr(s"$version${pos.sourceFile.name}:${startLine + 1}:${startColumn + 1}|$cls|$method|$snippet")
144+
Expr(s"$version$fileName:${pos.startLine + 1}:${pos.startColumn + 1}|$cls|$method|$snippet")
145145
end frameImpl
146146

147147
private def render(using Quotes)(symbol: quotes.reflect.Symbol): String =

kyo-data/shared/src/main/scala/kyo/internal/SafeClassTagMacro.scala

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package kyo.internal
22

3+
import kyo.Chunk
34
import kyo.SafeClassTag
45
import kyo.SafeClassTag.*
56
import scala.quoted.*
@@ -36,17 +37,17 @@ private[kyo] object SafeClassTagMacro:
3637
def create(tpe: TypeRepr): Expr[SafeClassTag[Any]] =
3738
tpe match
3839
case OrType(_, _) =>
39-
def flatten(tpe: TypeRepr): Seq[TypeRepr] =
40+
def flatten(tpe: TypeRepr): Chunk[TypeRepr] =
4041
tpe match
41-
case OrType(a, b) => flatten(a) ++ flatten(b)
42-
case _ => Seq(tpe)
42+
case OrType(a, b) => flatten(a).concat(flatten(b))
43+
case _ => Chunk(tpe)
4344
val exprs = flatten(tpe).map(create)
4445
'{ Union(Set(${ Varargs(exprs) }*)) }
4546
case AndType(_, _) =>
46-
def flatten(tpe: TypeRepr): Seq[TypeRepr] =
47+
def flatten(tpe: TypeRepr): Chunk[TypeRepr] =
4748
tpe match
48-
case AndType(a, b) => flatten(a) ++ flatten(b)
49-
case _ => Seq(tpe)
49+
case AndType(a, b) => flatten(a).concat(flatten(b))
50+
case _ => Chunk(tpe)
5051
val exprs = flatten(tpe).map(create)
5152
'{ Intersection(Set(${ Varargs(exprs) }*)) }
5253
case _ => createSingle(tpe)

0 commit comments

Comments
 (0)