java-topology/defects/prefect/pre-0002-steps-core-printed-messages-list.md

1.3 KiB

pre-0002: deployments/steps/core.py printed_messages list O(W²) warning deduplication

Severity: LOW CWE: CWE-407 (Algorithmic Complexity — linear membership test in hot loop) Target: Prefect (PrefectHQ/prefect) Files:

  • src/prefect/deployments/steps/core.py:191-202run_steps(): printed_messages list deduplication

Description

run_steps() deduplicates deprecation warnings using a plain list inside a for-loop:

printed_messages = []
for warning in w:                            # O(W) outer loop
    message = str(warning.message)
    if message not in printed_messages:      # O(W) linear scan — O(W²) total
        ...print...
        printed_messages.append(message)

With W warnings per step, deduplication cost is O(W²). In practice W is small (deployment step warnings are bounded by deprecation notices), but the fix is trivial: use a set for O(1) membership.

Complexity Before

message not in printed_messages (list): O(W) Total: O(W²)

Complexity After

message not in printed_set (set): O(1) average Total: O(W)

Patch

See patch/pre-0002-steps-core-printed-messages-list.patch

Reproduction

cd defects/prefect/unit && javac -d . PrefectTest.java && java -ea unit.PrefectTest