How We Speed Up Quantitative Scripts by 2–3.3× Without Rewriting Strategy Logic

DolphinDB
2026-09-16

In quantitative computing, performance bottlenecks are not always where you expect them to be.

Tick-by-tick processing, factor calculation, cumulative statistics, and other strategy workloads can involve millions of iterations, conditional branches, and calls to user-defined functions. As strategy logic becomes more sophisticated, the overhead of executing these scripts can become significant—even when the underlying business logic itself is relatively simple.

This is why we introduced Dynamic Script Optimization in DolphinDB.

By dynamically optimizing the interpreter's execution path, DolphinDB can reduce the overhead associated with loops, branches, and function calls. In our benchmarks, selected workloads achieved 2–3.3× faster execution.

Why optimize script execution?

Traditional script execution can introduce overhead when repeatedly evaluating loops, conditional branches, variable scopes, and function calls. For a small script, this is negligible. But when the same logic runs hundreds of thousands or millions of times, these costs can become a meaningful part of total execution time.

Dynamic Script Optimization addresses this overhead by dynamically adjusting the interpreter's execution path based on how the script is executed.

One line to enable it

There is no need to rewrite existing scripts. Simply run:

enableDynamicScriptOptimization()

The optimization will apply to scripts submitted subsequently in the current session.

To disable it:

disableDynamicScriptOptimization()

This session-level design makes performance testing particularly straightforward. A developer can compare the same workload with and without optimization in separate sessions before deciding whether to enable it more broadly.

For production environments, administrators can also enable the optimization by default for new sessions through the server configuration:

enableDynamicScriptOptimization=true

After restarting the node, newly created sessions will inherit the setting.

A simple example

Consider a common pattern in quantitative research: calling several user-defined functions inside a loop. The strategy logic does not need to change; the optimization happens at execution time.

// Enable optimization for the current session
enableDynamicScriptOptimization()

def normalizePrice(x){
    return x * 1.01
}

def calcSignal(x){
    return x > 100.0
}

def calcScore(x){
    return x * x
}

n = 1000000
score = 0.0

for(i in 0..(n-1)){
    price = normalizePrice(double(i))
    signal = calcSignal(price)

    if(signal){
        score += calcScore(price)
    }
}

score

Where the gains show up

We benchmarked 11 representative scenarios on a three-node DolphinDB 3.00.6 cluster, using 1M iterations or 500K tick-level records. Each node ran a controller, datanode, computenode, and agent, with Intel Xeon Silver 4314 CPUs and 64 workers per node. Results were verified to be identical before and after optimization.

Some representative results:

ScenarioBeforeAfterSpeedup
Function call chain in a loop (1M iter)0.93s0.28s3.3×
Three function calls per iteration (1M iter)1.27s0.42s3.0×
Single UDF call per iteration (1M iter)0.81s0.35s2.3×
Lambda expression in a loop (1M iter)0.55s0.27s2.0×
VWAP cumulative calculation (500K records)0.27s0.18s1.5×
Tick-by-tick conditional factor calc (500K records)0.28s0.22s1.3×
1000×1000 matrix nested loop0.42s0.32s1.3×
Arithmetic-heavy for loop (1M iter)0.34s0.30s1.1×


The pattern is quite clear. The largest gains appear in workloads with frequent function calls, complex control flow, or repeated state updates. In several such scenarios, execution time was reduced by more than half.

By contrast, simple arithmetic-heavy loops see much smaller improvements. DolphinDB's existing interpreter is already efficient for these operations, leaving less optimization headroom.

So Dynamic Script Optimization is not something that needs to be enabled selectively for every single script. It is particularly valuable when the workload is function-call intensive, control-flow heavy, or executed at high frequency.

Why this matters

Performance in quantitative systems rarely comes from one dramatic fix. More often, it comes from eliminating thousands of small, repeated inefficiencies—a few microseconds here, multiplied across millions of ticks, signals, and factor calculations.

And rewriting a strategy just to make it faster is rarely a good trade-off. The debugging, validation, and maintenance costs can easily outweigh the performance gains.

That is where Dynamic Script Optimization fits in: it works underneath the strategy, rather than changing the strategy itself.


Available now in DolphinDB 2.00.18 and 3.00.5+. For full compatibility guide, benchmark scripts, and all 11 test scenarios, reach out to info@dolphindb.com.