mirror of
https://github.com/MontFerret/ferret.git
synced 2025-08-15 20:02:56 +02:00
Refactor LoopCollectCompiler
: simplify aggregation function call handling, optimize register allocation, streamline loop source setup, and update function arguments for consistency.
This commit is contained in:
@@ -116,8 +116,14 @@ func (c *LoopCollectCompiler) compileLoop(spec *core.Collector) {
|
|||||||
doInit := spec.HasGrouping() || !spec.HasAggregation()
|
doInit := spec.HasGrouping() || !spec.HasAggregation()
|
||||||
|
|
||||||
if doInit {
|
if doInit {
|
||||||
// Move the collector to the next loop source
|
if loop.Allocate {
|
||||||
c.ctx.Emitter.EmitMove(loop.Src, spec.Destination())
|
// Move the collector to the next loop source
|
||||||
|
c.ctx.Emitter.EmitMove(loop.Src, spec.Destination())
|
||||||
|
} else {
|
||||||
|
// We do not control the source of the loop, so we just set it to the destination
|
||||||
|
loop.Src = spec.Destination()
|
||||||
|
}
|
||||||
|
|
||||||
loop.EmitInitialization(c.ctx.Registers, c.ctx.Emitter, c.ctx.Loops.Depth())
|
loop.EmitInitialization(c.ctx.Registers, c.ctx.Emitter, c.ctx.Loops.Depth())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -177,7 +177,6 @@ func (c *LoopCollectCompiler) finalizeGlobalAggregation(spec *core.Collector) {
|
|||||||
c.ctx.Registers.Free(prevLoop.Src)
|
c.ctx.Registers.Free(prevLoop.Src)
|
||||||
|
|
||||||
// Create a new loop with 1 iteration only to process the aggregation
|
// Create a new loop with 1 iteration only to process the aggregation
|
||||||
c.ctx.Symbols.EnterScope()
|
|
||||||
loop := c.ctx.Loops.NewLoop(core.ForInLoop, core.NormalLoop, prevLoop.Distinct)
|
loop := c.ctx.Loops.NewLoop(core.ForInLoop, core.NormalLoop, prevLoop.Distinct)
|
||||||
c.ctx.Loops.Push(loop)
|
c.ctx.Loops.Push(loop)
|
||||||
|
|
||||||
@@ -199,19 +198,16 @@ func (c *LoopCollectCompiler) finalizeGlobalAggregation(spec *core.Collector) {
|
|||||||
// Initialize the loop
|
// Initialize the loop
|
||||||
loop.EmitInitialization(c.ctx.Registers, c.ctx.Emitter, c.ctx.Loops.Depth())
|
loop.EmitInitialization(c.ctx.Registers, c.ctx.Emitter, c.ctx.Loops.Depth())
|
||||||
|
|
||||||
// Process the aggregation function calls using the values from the previous loop's collector
|
c.compileGlobalAggregationFuncCalls(spec)
|
||||||
c.compileGlobalAggregationFuncCalls(spec, prevLoop.Dst)
|
|
||||||
|
|
||||||
// Free the previous loop's destination register
|
|
||||||
c.ctx.Registers.Free(prevLoop.Dst)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// compileGlobalAggregationFuncCalls processes the aggregation function calls for the selectors.
|
// compileGlobalAggregationFuncCalls processes the aggregation function calls for the selectors.
|
||||||
// It loads the arguments from the aggregator, calls the aggregation functions,
|
// It loads the arguments from the aggregator, calls the aggregation functions,
|
||||||
// and assigns the results to local variables.
|
// and assigns the results to local variables.
|
||||||
// It also handles the case where there are no records in the aggregator by loading NONE values.
|
// It also handles the case where there are no records in the aggregator by loading NONE values.
|
||||||
func (c *LoopCollectCompiler) compileGlobalAggregationFuncCalls(spec *core.Collector, aggregator vm.Operand) {
|
func (c *LoopCollectCompiler) compileGlobalAggregationFuncCalls(spec *core.Collector) {
|
||||||
// Gets the number of records in the accumulator
|
// Gets the number of records in the accumulator
|
||||||
|
aggregator := spec.Destination()
|
||||||
cond := c.ctx.Registers.Allocate(core.Temp)
|
cond := c.ctx.Registers.Allocate(core.Temp)
|
||||||
c.ctx.Emitter.EmitAB(vm.OpLength, cond, aggregator)
|
c.ctx.Emitter.EmitAB(vm.OpLength, cond, aggregator)
|
||||||
zero := loadConstant(c.ctx, runtime.ZeroInt)
|
zero := loadConstant(c.ctx, runtime.ZeroInt)
|
||||||
|
Reference in New Issue
Block a user