Skip to content

Performance overhead when importing modules using "code" instead of "globalCode" #427

@KasparMetsa

Description

@KasparMetsa

Used Claude to find a problem with syntax like this:

functions:
  # Import inside code: (runs on every message - inefficient but works)
  log_action_count:
    type: forEach
    code: |
      from processor_functions import log_user_action_count
      log_user_action_count(key, value, log)

The Problem

When using code: for imports, the import statement executes on every message:

# Generated code when using code: for import
def log_action_count(key, value):
    from processor_functions import log_user_action_count  # RUNS EVERY CALL
    log_user_action_count(key, value, log)

When using globalCode:, the import executes once at startup:

# Generated code when using globalCode: for import
from processor_functions import log_user_action_count  # RUNS ONCE

def log_action_count(key, value):
    log_user_action_count(key, value, log)

Here is full description, proof and possible solutions by Claude

PR_422_IMPORT_OVERHEAD_ANALYSIS.md

Here is benchmark by Clade to prove the overhead:

benchmark_import.py

When I ran it, I got this output:

benchmark.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions