Refactor, add comments

This commit is contained in:
InversionSpaces 2023-10-06 16:02:09 +00:00
parent dcf6f04796
commit a1f61ba299
2 changed files with 77 additions and 45 deletions

View File

@ -248,12 +248,72 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
} }
} }
/**
* Unfold `stream[idx]`
*/
private def unfoldStreamGate[S: Mangler: Exports: Arrows](
streamName: String,
streamType: StreamType,
idx: ValueRaw
): State[S, (VarModel, Inline)] = for {
/**
* Inline idx
*/
idxInlined <- unfold(idx)
(idxVM, idxInline) = idxInlined
/**
* Inline size which is `idx + 1`
* TODO: Refactor to apply optimizations
*/
sizeName <- Mangler[S].findAndForbidName(s"${streamName}_size")
sizeVar = VarModel(sizeName, idxVM.`type`)
sizeInline = CallServiceModel(
"math",
funcName = "add",
args = List(idxVM, LiteralModel.number(1)),
result = sizeVar
).leaf
gateInlined <- StreamGateInliner(streamName, streamType, sizeVar)
(gateVM, gateInline) = gateInlined
/**
* Remove properties from idx
* as we need to use it in index
* TODO: Do not generate it
* if it is not needed,
* e.g. in `join`
*/
idxFlattened <- idxVM match {
case vr: VarModel => removeProperties(vr)
case _ => (idxVM, Inline.empty).pure[State[S, *]]
}
(idxFlat, idxFlatInline) = idxFlattened
/**
* Construct stream[idx]
*/
gate = gateVM.withProperty(
IntoIndexModel
.fromValueModel(idxFlat, streamType.element)
.getOrElse(
internalError(s"Unexpected: could not convert ($idxFlat) to IntoIndexModel")
)
)
} yield gate -> Inline(
idxInline.predo
.append(sizeInline) ++
gateInline.predo ++
idxFlatInline.predo,
mergeMode = SeqMode
)
private def unfoldRawWithProperties[S: Mangler: Exports: Arrows]( private def unfoldRawWithProperties[S: Mangler: Exports: Arrows](
raw: ValueRaw, raw: ValueRaw,
properties: Chain[PropertyRaw], properties: Chain[PropertyRaw],
propertiesAllowed: Boolean propertiesAllowed: Boolean
): State[S, (ValueModel, Inline)] = { ): State[S, (ValueModel, Inline)] = {
((raw, properties.uncons) match { ((raw, properties.uncons) match {
/**
* To inline
*/
case ( case (
vr @ VarRaw(_, st @ StreamType(_)), vr @ VarRaw(_, st @ StreamType(_)),
Some(IntoIndexRaw(idx, _), otherProperties) Some(IntoIndexRaw(idx, _), otherProperties)
@ -261,39 +321,11 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
unfold(vr).flatMap { unfold(vr).flatMap {
case (VarModel(nameVM, _, _), inl) => case (VarModel(nameVM, _, _), inl) =>
for { for {
idxInlined <- unfold(idx) gateInlined <- unfoldStreamGate(nameVM, st, idx)
(idxVM, idxInline) = idxInlined
sizeName <- Mangler[S].findAndForbidName(s"${nameVM}_size")
sizeVar = VarModel(sizeName, idxVM.`type`)
sizeInline = CallServiceModel(
"math",
funcName = "add",
args = List(idxVM, LiteralModel.number(1)),
result = sizeVar
).leaf
gateInlined <- StreamGateInliner(nameVM, st, sizeVar)
(gateVM, gateInline) = gateInlined (gateVM, gateInline) = gateInlined
idxFlattened <- idxVM match {
case vr: VarModel => removeProperties(vr)
case _ => (idxVM, Inline.empty).pure[State[S, *]]
}
(idxFlat, idxFlatInline) = idxFlattened
gate = gateVM.withProperty(
IntoIndexModel
.fromValueModel(idxFlat, st.element)
.getOrElse(
internalError(s"Unexpected: could not convert ($idxFlat) to IntoIndexModel")
)
)
propsInlined <- unfoldProperties( propsInlined <- unfoldProperties(
Inline( gateInline,
idxInline.predo gateVM,
.append(sizeInline) ++
gateInline.predo ++
idxFlatInline.predo,
mergeMode = SeqMode
),
gate,
otherProperties, otherProperties,
propertiesAllowed propertiesAllowed
) )

View File

@ -22,25 +22,25 @@ object StreamGateInliner extends Logging {
* To wait for size elements of a stream, * To wait for size elements of a stream,
* the following model is generated: * the following model is generated:
* (seq * (seq
* (seq * (seq
* (fold $stream s * (fold $stream s
* (seq
* (seq * (seq
* (ap s $stream_test) * (seq
* (canon <peer> $stream_test #stream_iter_canon) * (ap s $stream_test)
* ) * (canon <peer> $stream_test #stream_iter_canon)
* (xor * )
* (match #stream_iter_canon.length size * (xor
* (null) * (match #stream_iter_canon.length size
* (null)
* )
* (next s)
* ) * )
* (next s)
* ) * )
* (never)
* ) * )
* (never) * (canon <peer> $stream_test #stream_result_canon)
* ) * )
* (canon <peer> $stream_test #stream_result_canon) * (ap #stream_result_canon stream_gate)
* )
* (ap #stream_result_canon stream_gate)
* ) * )
*/ */
def joinStreamOnIndexModel( def joinStreamOnIndexModel(