Skip to content

Perf: _normalize_percent_encoding runs a regex on every signed path even when no % is present #261

Closed

Description

@TexasCoding

Summary

Every signed request pays a compiled-regex sub + lambda dispatch over the path even though the vast majority of Kalshi paths contain no % (no _seg()-encoded ticker fragments). On a 200 req/s account this is pure waste in a hot path.

Location

  • kalshi/auth.py:36-51 — _normalize_percent_encoding
  • kalshi/auth.py:255-256 — call site in sign_request

Evidence

def _normalize_percent_encoding(path: str) -> str:
    ...
    return re.sub(
        r"%([0-9a-fA-F]{2})",
        lambda m: "%" + m.group(1).upper(),
        path,
    )

# sign_request:
clean_path = _normalize_percent_encoding(clean_path)

Recommended fix

Short-circuit before the regex: if "%" not in path: return path.

Severity & category

medium / performance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance / hot-path concern

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions