Skip to content

Commit ef48ae2

Browse files
committed
Logging- / debugging-friendly refactoring of strategy code
1 parent 4d0cca9 commit ef48ae2

File tree

3 files changed

+100
-52
lines changed

3 files changed

+100
-52
lines changed

src/main/scala/streams/Strategies.scala

+27-12
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,18 @@ private[streams] trait Strategies
164164
false
165165
}
166166

167-
def isStreamSafe = {
168-
// Note: we count the number of closures / ops that have side effects, not the
169-
// number of side-effects themselves: we assume that within a closure the
170-
// side effects are still done in the same order, and likewise for preserved
171-
// sub-trees that they're still evaluated in the same order within the same
172-
// originating op. For instance with mkString(prefix, sep, suffix), prefix,
173-
// sep and suffix will still be evaluated in the same order after the rewrite.
174-
val unsafeClosureSideEffectCount =
175-
stream.closureSideEffectss.count(hasUnsafeEffect)
176-
def unsafePreservedTreesSideEffectsCount =
177-
stream.preservedSubTreesSideEffectss.count(hasUnsafeEffect)
167+
// Note: we count the number of closures / ops that have side effects, not the
168+
// number of side-effects themselves: we assume that within a closure the
169+
// side effects are still done in the same order, and likewise for preserved
170+
// sub-trees that they're still evaluated in the same order within the same
171+
// originating op. For instance with mkString(prefix, sep, suffix), prefix,
172+
// sep and suffix will still be evaluated in the same order after the rewrite.
173+
val unsafeClosureSideEffectCount =
174+
stream.closureSideEffectss.count(hasUnsafeEffect)
175+
def unsafePreservedTreesSideEffectsCount =
176+
stream.preservedSubTreesSideEffectss.count(hasUnsafeEffect)
178177

178+
def isStreamSafe = {
179179
unsafeClosureSideEffectCount <= 1 &&
180180
(unsafeClosureSideEffectCount + unsafePreservedTreesSideEffectsCount) <= 1 &&
181181
!couldSkipSideEffects
@@ -205,7 +205,22 @@ private[streams] trait Strategies
205205
}
206206
}
207207

208-
// println(s"tree = ${stream.tree}\n\tstream = ${stream.describe()}\n\tstrategy = $strategy\n\tlambdaCount = ${stream.lambdaCount}\n\tclosureSideEffectss = ${stream.closureSideEffectss}\n\tcouldSkipSideEffects = $couldSkipSideEffects\n\thasMoreThanOneLambdaWithUnsafeSideEffect = $hasMoreThanOneLambdaWithUnsafeSideEffect\n\tisWorthOptimizing = $worthOptimizing")
208+
// if (flags.debug) {
209+
// // info(stream.tree.pos,
210+
// println(s"""
211+
// tree = ${stream.tree}
212+
// stream = ${stream.describe()}
213+
// strategy = $strategy
214+
// lambdaCount = ${stream.lambdaCount}
215+
// closureSideEffectss = ${stream.closureSideEffectss}
216+
// couldSkipSideEffects = $couldSkipSideEffects
217+
// isWorthOptimizing = $worthOptimizing
218+
// isFaster = $isFaster
219+
// unsafeClosureSideEffectCount = $unsafeClosureSideEffectCount
220+
// unsafePreservedTreesSideEffectsCount = $unsafePreservedTreesSideEffectsCount
221+
// isStreamSafe = $isStreamSafe
222+
// """)//, force = true)
223+
// }
209224

210225
worthOptimizing
211226
}

src/main/scala/streams/StreamResults.scala

+21
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,30 @@ private[streams] trait StreamResults extends TuploidValues {
1313
afterBody: List[Tree] = Nil,
1414
ending: List[Tree] = Nil)
1515
{
16+
// val flatten: List[Tree] = {
17+
// val b = collection.mutable.ListBuffer[Tree]()
18+
// for (list <- List(prelude, beforeBody, body, afterBody);
19+
// item <- list) {
20+
// item match {
21+
// case Block(items, v) =>
22+
// b ++= items
23+
// // println("V = " + v + ": " + v.getClass)
24+
// b += v
25+
// case t =>
26+
// b += t
27+
// }
28+
// }
29+
// b ++= ending
30+
// b.result()
31+
// }
1632
def flatten: List[Tree] =
1733
prelude ++ beforeBody ++ body ++ afterBody ++ ending
1834

35+
// for ((n, list) <- Map("prelude" -> prelude, "beforeBody" -> beforeBody, "body" -> body, "afterBody" -> afterBody, "ending" -> ending);
36+
// Block(list, v) <- list) {
37+
// println(s"FOUND block item $v in $n")
38+
// }
39+
1940
def compose(typed: Tree => Tree) =
2041
typed(q"..$flatten")
2142

src/main/scala/streams/StreamTransforms.scala

+52-40
Original file line numberDiff line numberDiff line change
@@ -28,52 +28,64 @@ trait StreamTransforms
2828
fresh: String => String,
2929
currentOwner: Symbol,
3030
recur: Tree => Tree,
31-
typecheck: Tree => Tree): Option[Tree]
32-
= tree match {
33-
case tree @ SomeStream(stream) if !hasKnownLimitationOrBug(stream) =>
34-
if (isBlacklisted(tree.pos, currentOwner)) {
35-
verboseInfo(
36-
tree,
37-
Optimizations.messageHeader + s"Skipped stream ${stream.describe()}")
38-
39-
None
40-
} else if (isWorthOptimizing(stream, strategy)) {
41-
// println(s"stream = $stream")
42-
verboseInfo(
43-
tree,
44-
Optimizations.optimizedStreamMessage(stream.describe(), strategy))
45-
46-
try {
47-
val result: Tree = stream
48-
.emitStream(
49-
n => TermName(fresh(n)),
50-
recur,
51-
currentOwner = currentOwner,
52-
typed = typecheck)
53-
.compose(typecheck)
54-
55-
if (flags.debug) {
31+
typecheck: Tree => Tree)
32+
: Option[Tree] = {
33+
// println(tree)
34+
tree match {
35+
case tree @ SomeStream(stream) =>
36+
if (hasKnownLimitationOrBug(stream)) {
37+
if (flags.veryVerbose) {
5638
verboseInfo(
5739
tree,
58-
Optimizations.messageHeader + s"Result for ${stream.describe()} (owner: ${currentOwner.fullName}):\n$result")
40+
Optimizations.messageHeader + s"Stream ${stream.describe()} has known limitations or bugs with strategy $strategy")
5941
}
60-
Some(result)
42+
None
43+
} else {
44+
if (isBlacklisted(tree.pos, currentOwner)) {
45+
verboseInfo(
46+
tree,
47+
Optimizations.messageHeader + s"Skipped stream ${stream.describe()}")
6148

62-
} catch {
63-
case ex: Throwable =>
64-
logException(tree.pos, ex)
6549
None
50+
} else if (isWorthOptimizing(stream, strategy)) {
51+
// println(s"stream = ${stream.describe()}\n\t${stream.tree}")
52+
verboseInfo(
53+
tree,
54+
Optimizations.optimizedStreamMessage(stream.describe(), strategy))
55+
56+
try {
57+
val result: Tree = stream
58+
.emitStream(
59+
n => TermName(fresh(n)),
60+
recur,
61+
currentOwner = currentOwner,
62+
typed = typecheck)
63+
.compose(typecheck)
64+
65+
if (flags.debug) {
66+
verboseInfo(
67+
tree,
68+
Optimizations.messageHeader + s"Result for ${stream.describe()} (owner: ${currentOwner.fullName}):\n$result")
69+
}
70+
Some(result)
71+
72+
} catch {
73+
case ex: Throwable =>
74+
logException(tree.pos, ex)
75+
None
76+
}
77+
} else {
78+
if (flags.veryVerbose && !stream.isDummy && !flags.quietWarnings) {
79+
verboseInfo(
80+
tree,
81+
Optimizations.messageHeader + s"Stream ${stream.describe()} is not worth optimizing with strategy $strategy")
82+
}
83+
None
84+
}
6685
}
67-
} else {
68-
if (flags.veryVerbose && !stream.isDummy && !flags.quietWarnings) {
69-
verboseInfo(
70-
tree,
71-
Optimizations.messageHeader + s"Stream ${stream.describe()} is not worth optimizing with strategy $strategy")
72-
}
73-
None
74-
}
7586

76-
case _ =>
77-
None
87+
case _ =>
88+
None
89+
}
7890
}
7991
}

0 commit comments

Comments
 (0)