Skip to content

perf(transform): copy events one level deep in filter_period_intersect - #203

Merged
ErikBjare merged 1 commit into
ActivityWatch:masterfrom
TimeToBuildBob:perf/filter-period-intersect-shallow-copy
Nov 15, 2026
Merged

ErikBjare merged 1 commit into
ActivityWatch:masterfrom
TimeToBuildBob:perf/filter-period-intersect-shallow-copy

Conversation

@TimeToBuildBob

Copy link
Copy Markdown
Contributor

_replace_event_period deep-copied every event that filter_period_intersect returns. With a year of data that deepcopy is the largest single cost of aw-webui's Activity query in the Python server: the query intersects all window events with not-AFK, and all web events with the browser window.

This change builds the result Event directly, with a one-level copy of data. That gives the same isolation aw_query's _copy_events gives transforms (#172): transforms only set top-level keys.

Measurement. I ran the aw-webui fullDesktopQuery-shaped year query in-process against a synthetic year: 321,781 window, 6,146 AFK and 53,756 web events, the same data as in ActivityWatch/activitywatch#1545. One Python 3.9 interpreter, with a fetch function shared by every checkout so that only aw-core code differs. Medians of 3 runs:

aw-core year query
v0.13.2 pin 6a58ec5 13.4s, 13.7s
v0.14.1 pin 5d34ffe 16.3–17.5s (15.3s on the same run as the next row)
5d34ffe + this change 12.9s

The ~3s regression between those two pins is spread over several correctness changes rather than one bug: _copy_events (#172) costs about 0.9s, JSON merge keys (#173) about 0.5s, WHATWG URL parsing (#173) about 0.3s, and flood normalization (#143/#145/#165) about 0.4s. This PR doesn't revert any of them. It removes a long-standing cost that is larger than all of them together.

Test: test_filter_period_intersect_copies_events pins that results are new events whose data and period can change without touching the input. The full suite passes locally (pytest tests/).

_replace_event_period deep-copied every intersected event. On a year of
synthetic data (322k window, 54k web events) that deepcopy is the largest
single cost of aw-webui's Activity query in the Python server.

Build the result Event directly with a one-level copy of data, the same
isolation aw_query's _copy_events gives transforms (they only set top-level
keys). Year query, same interpreter, in-process: 15.3s -> 12.9s.

Refs ActivityWatch/activitywatch#1545

Git-Session-Id: ccc0b6a5-3ef4-47ad-b308-b87ee9aef8e8
@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge.

Summary

_replace_event_period now builds an Event with a one-level copy of data instead of copying the whole event deeply.

  • Intersected events keep their clipped time with less copying.

Reviews (1) · Last reviewed commit: "perf(transform): copy events one level d..." · Reviewed by Greptile

@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

CI-green and mergeable — waiting only on a maintainer click.

This PR is ready to merge, but the bot has pull-only access to this repo and can't self-merge — surfacing it here so it isn't lost. The monitoring loop will stop re-flagging it now that this note is posted.

@TimeToBuildBob

TimeToBuildBob commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 AI code review

Replaces deepcopy(event) in _replace_event_period with construction of an Event using the existing ID, the replacement period, and dict(event.data). Adds test_filter_period_intersect_copies_events to check event identity, retained fields, and isolation when assigning top-level data and duration.

Safe to merge — no P0/P1 findings

Confidence 5/5

✅ No findings. The diff looks correct to me on this pass.

Files changed (2) — the diff as I read it
  • aw_transform/filter_period_intersect.py — Removes the deepcopy import and constructs replacement events with a shallow copy of data in _replace_event_period.
  • tests/test_transforms.py — Adds test_filter_period_intersect_copies_events, checking retained ID and data, clipped duration, and input preservation after result mutations.

Reviewed a0926cb979a6 · panel: codex:gpt-6.1-sol + claude-code:sonnet · subscription engine · 25s · about this reviewer

Maintainer commands

@TimeToBuildBob review (own line) — fresh review · @TimeToBuildBob fix — a worker acts on the findings. Once per comment; 👀 = received.

@ErikBjare
ErikBjare merged commit 26391c7 into ActivityWatch:master Nov 15, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants