@@ -43,8 +43,10 @@ import pekko.util.OptionVal
43
43
.mandatoryAttribute[Attributes .NestedMaterializationCancellationPolicy ]
44
44
.propagateToNestedMaterialization
45
45
val matPromise = Promise [M ]()
46
- val logic = new GraphStageLogic (shape) with InHandler with OutHandler {
47
- val accumulated = collection.mutable.Buffer .empty[In ]
46
+ object logic extends GraphStageLogic (shape) with InHandler with OutHandler {
47
+ private var left = n
48
+ private var builder = Vector .newBuilder[In ]
49
+ builder.sizeHint(left)
48
50
49
51
private var subSource = OptionVal .none[SubSourceOutlet [In ]]
50
52
private var subSink = OptionVal .none[SubSinkInlet [Out ]]
@@ -65,11 +67,12 @@ import pekko.util.OptionVal
65
67
subSource match {
66
68
case OptionVal .Some (s) => s.push(grab(in))
67
69
case _ =>
68
- accumulated.append(grab(in))
69
- if (accumulated.size == n) {
70
+ builder += grab(in)
71
+ left -= 1
72
+ if (left == 0 ) {
70
73
materializeFlow()
71
74
} else {
72
- // gi' me some more!
75
+ // give me some more!
73
76
pull(in)
74
77
}
75
78
}
@@ -98,12 +101,12 @@ import pekko.util.OptionVal
98
101
// delegate to subSink
99
102
s.pull()
100
103
case _ =>
101
- if (accumulated.size < n ) pull(in)
102
- else if (accumulated.size == n ) {
104
+ if (left > 0 ) pull(in)
105
+ else if (left == 0 ) {
103
106
// corner case for n = 0, can be handled in FlowOps
104
107
materializeFlow()
105
108
} else {
106
- throw new IllegalStateException (s " Unexpected accumulated size: ${accumulated.size} (n: $n) " )
109
+ throw new IllegalStateException (s " Unexpected accumulated size, left : $left (n: $n) " )
107
110
}
108
111
}
109
112
}
@@ -114,7 +117,7 @@ import pekko.util.OptionVal
114
117
case _ =>
115
118
if (propagateToNestedMaterialization) {
116
119
downstreamCause = OptionVal .Some (cause)
117
- if (accumulated.size == n ) {
120
+ if (left == 0 ) {
118
121
// corner case for n = 0, can be handled in FlowOps
119
122
materializeFlow()
120
123
} else if (! hasBeenPulled(in)) { // if in was already closed, nested flow would have already been materialized
@@ -128,8 +131,8 @@ import pekko.util.OptionVal
128
131
129
132
def materializeFlow (): Unit =
130
133
try {
131
- val prefix = accumulated.toVector
132
- accumulated.clear()
134
+ val prefix = builder.result()
135
+ builder = null // free for GC
133
136
subSource = OptionVal .Some (new SubSourceOutlet [In ](" FlatMapPrefix.subSource" ))
134
137
val theSubSource = subSource.get
135
138
subSink = OptionVal .Some (new SubSinkInlet [Out ](" FlatMapPrefix.subSink" ))
0 commit comments