Skip to content

Commit 3644973

Browse files
committed
refactor: micro-optimizations in find_rectangles
1 parent 4b4ae70 commit 3644973

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

execution_engine/task/process/rectangle_cython.pyx

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ def find_rectangles(all_intervals: list[list[AnyInterval]],
381381
else: # at the same time, but different tracks => any order is fine
382382
return 1
383383
events.sort(key = cmp_to_key(compare_events))
384+
event_count = len(events)
384385
385386
# The result will be a list of intervals produced by
386387
# interval_constructor.
@@ -395,15 +396,6 @@ def find_rectangles(all_intervals: list[list[AnyInterval]],
395396
result.append(interval)
396397
previous_end = end
397398
398-
def no_gap_between_points_in_time(end_time, start_time):
399-
# Since points in time for intervals are quantized to whole
400-
# seconds and intervals are closed (inclusive) for both start
401-
# and end points, two adjacent intervals like
402-
# [START_TIME1, 10:59:59] [11:00:00, END_TIME2]
403-
# have no gap between them and can be considered a single
404-
# continuous interval [START_TIME1, END_TIME2].
405-
return (end_time == start_time) or (end_time == start_time - 1)
406-
407399
def is_same_result(active_intervals1, active_intervals2):
408400
# When we have to decide whether to extend a result interval
409401
# or start a new one, we compare the state for the existing
@@ -414,28 +406,34 @@ def find_rectangles(all_intervals: list[list[AnyInterval]],
414406
== interval_constructor(0,0,active_intervals2))
415407
416408
active_intervals = [None] * track_count
417-
def process_event_for_point_in_time(index, point_time):
418-
nonlocal active_intervals
409+
def process_events_for_point_in_time(index, point_time):
419410
high_time = point_time
420411
any_open = False
421-
for i in range(index, len(events)):
412+
for i in range(index, event_count):
422413
time, open_, interval, track = events[i]
423-
if no_gap_between_points_in_time(point_time, time):
424-
high_time = max(high_time, time)
414+
# Since points in time for intervals are quantized to whole
415+
# seconds and intervals are closed (inclusive) for both start
416+
# and end points, two adjacent intervals like
417+
# [START_TIME1, 10:59:59] [11:00:00, END_TIME2]
418+
# have no gap between them and can be considered a single
419+
# continuous interval [START_TIME1, END_TIME2].
420+
if (point_time == time) or (point_time == time - 1):
421+
if time > high_time:
422+
high_time = time
425423
any_open |= open_
426424
else:
427425
return i, time, active_intervals.copy(), high_time if any_open else high_time + 1
428426
active_intervals[track] = interval if open_ else None
429427
return None, None, None, None
430428
431-
if not len(events) == 0:
429+
if not event_count == 0:
432430
# Step through event "clusters" with a common point in time and
433431
# emit result intervals with unchanged interval "payload".
434432
index, time = 0, events[0][0]
435433
interval_start_time = time
436-
index, time, interval_start_state, high_time = process_event_for_point_in_time(index, time)
434+
index, time, interval_start_state, high_time = process_events_for_point_in_time(index, time)
437435
while index:
438-
new_index, new_time, maybe_end_state, high_time = process_event_for_point_in_time(index, time)
436+
new_index, new_time, maybe_end_state, high_time = process_events_for_point_in_time(index, time)
439437
# Diagram for this program point:
440438
# |___potential_result_interval___|| |
441439
# index new_index

execution_engine/task/process/rectangle_python.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ def compare_events(event1, event2):
375375
else: # at the same time, but different tracks => any order is fine
376376
return 1
377377
events.sort(key = cmp_to_key(compare_events))
378+
event_count = len(events)
378379

379380
# The result will be a list of intervals produced by
380381
# interval_constructor.
@@ -389,15 +390,6 @@ def add_segment(start, end, original_intervals):
389390
result.append(interval)
390391
previous_end = end
391392

392-
def no_gap_between_points_in_time(end_time, start_time):
393-
# Since points in time for intervals are quantized to whole
394-
# seconds and intervals are closed (inclusive) for both start
395-
# and end points, two adjacent intervals like
396-
# [START_TIME1, 10:59:59] [11:00:00, END_TIME2]
397-
# have no gap between them and can be considered a single
398-
# continuous interval [START_TIME1, END_TIME2].
399-
return (end_time == start_time) or (end_time == start_time - 1)
400-
401393
def is_same_result(active_intervals1, active_intervals2):
402394
# When we have to decide whether to extend a result interval
403395
# or start a new one, we compare the state for the existing
@@ -408,28 +400,34 @@ def is_same_result(active_intervals1, active_intervals2):
408400
== interval_constructor(0,0,active_intervals2))
409401

410402
active_intervals = [None] * track_count
411-
def process_event_for_point_in_time(index, point_time):
412-
nonlocal active_intervals
403+
def process_events_for_point_in_time(index, point_time):
413404
high_time = point_time
414405
any_open = False
415-
for i in range(index, len(events)):
406+
for i in range(index, event_count):
416407
time, open_, interval, track = events[i]
417-
if no_gap_between_points_in_time(point_time, time):
418-
high_time = max(high_time, time)
408+
# Since points in time for intervals are quantized to whole
409+
# seconds and intervals are closed (inclusive) for both start
410+
# and end points, two adjacent intervals like
411+
# [START_TIME1, 10:59:59] [11:00:00, END_TIME2]
412+
# have no gap between them and can be considered a single
413+
# continuous interval [START_TIME1, END_TIME2].
414+
if (point_time == time) or (point_time == time - 1):
415+
if time > high_time:
416+
high_time = time
419417
any_open |= open_
420418
else:
421419
return i, time, active_intervals.copy(), high_time if any_open else high_time + 1
422420
active_intervals[track] = interval if open_ else None
423421
return None, None, None, None
424422

425-
if not len(events) == 0:
423+
if not event_count == 0:
426424
# Step through event "clusters" with a common point in time and
427425
# emit result intervals with unchanged interval "payload".
428426
index, time = 0, events[0][0]
429427
interval_start_time = time
430-
index, time, interval_start_state, high_time = process_event_for_point_in_time(index, time)
428+
index, time, interval_start_state, high_time = process_events_for_point_in_time(index, time)
431429
while index:
432-
new_index, new_time, maybe_end_state, high_time = process_event_for_point_in_time(index, time)
430+
new_index, new_time, maybe_end_state, high_time = process_events_for_point_in_time(index, time)
433431
# Diagram for this program point:
434432
# |___potential_result_interval___|| |
435433
# index new_index

0 commit comments

Comments
 (0)