Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize csp.stats calculations for expanding intervals #247

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions cpp/csp/cppnodes/statsimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2061,11 +2061,6 @@ DECLARE_CPPNODE( _generic_tick_window_updates )
s_pending_recalc = true;
}

if( csp.ticked( recalc ) )
{
s_pending_recalc = true;
}

if( csp.ticked( sampler ) )
{
// Handle removals if needed
Expand Down Expand Up @@ -2096,8 +2091,10 @@ DECLARE_CPPNODE( _generic_tick_window_updates )

// Handle removals
if( !s_removals.empty() && !s_pending_recalc )
{
std::swap( removals_.reserveSpace(), s_removals );
s_removals.clear();
s_removals.clear();
}

if( s_pending_recalc && !s_value_buffer.empty() )
{
Expand Down Expand Up @@ -2186,9 +2183,12 @@ DECLARE_CPPNODE( _generic_time_window_updates )

STATE_VAR( bool, s_first{true} );
STATE_VAR( bool, s_expanding{false} );

STATE_VAR( VariableSizeWindowBuffer<T>, s_value_buffer{} );
STATE_VAR( VariableSizeWindowBuffer<DateTime>, s_time_buffer{} );

STATE_VAR( std::vector<T>, s_additions{} ); // only used for expanding window optimization

TS_NAMED_OUTPUT_RENAMED( std::vector<T>, additions, additions_ );
TS_NAMED_OUTPUT_RENAMED( std::vector<T>, removals, removals_ );

Expand All @@ -2213,9 +2213,14 @@ DECLARE_CPPNODE( _generic_time_window_updates )
{
if( csp.ticked( reset ) )
{
s_value_buffer.clear();
s_time_buffer.clear();
s_pending_removals = 0;
if( s_expanding )
s_additions.clear();
else
{
s_value_buffer.clear();
s_time_buffer.clear();
s_pending_removals = 0;
}
}

if( csp.ticked( recalc ) )
Expand All @@ -2229,21 +2234,40 @@ DECLARE_CPPNODE( _generic_time_window_updates )
if( csp.ticked( x ) )
{
node -> validateShape();
s_value_buffer.push( x );
if( s_expanding )
s_additions.push_back( x );
else
s_value_buffer.push( x );
}
else
{
node -> checkValid();
s_value_buffer.push( node -> createNan() );
if( s_expanding )
s_additions.push_back( node -> createNan() );
else
s_value_buffer.push( node -> createNan() );
}
s_time_buffer.push( now() );
if( !s_expanding )
s_time_buffer.push( now() );
}

if( csp.ticked( trigger ) || ( s_first && csp.ticked( x ) && csp.ticked( sampler ) ) )
{
s_first = false;

DateTime threshold = ( s_expanding ? DateTime::MIN_VALUE() : now() - interval );
if( s_expanding )
{
// fast track expanding window calculations
// we just need to swap in all additions with no checks
if( !s_additions.empty() )
{
std::swap( additions_.reserveSpace(), s_additions );
s_additions.clear(); // keep allocated memory
}
return;
}

DateTime threshold = now() - interval;
// Handle removals
int64_t count = csp.count( sampler );
std::vector<T>* removals = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions csp/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ def _setup(x, interval, min_window, trigger, sampler, reset, weights=None, ignor
)

if interval is None:
if recalc is not None:
AdamGlustein marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError("The recalc parameter cannot be used with an expanding window (it is redundant)")
interval = timedelta()

if min_window is None:
Expand Down
Loading