Skip to content

Read the argument count off the code object in weak() - #195

Merged
Korijn merged 1 commit into
masterfrom
claude/micro-opt-weak-argcount
Jul 10, 2026
Merged

Read the argument count off the code object in weak()#195
Korijn merged 1 commit into
masterfrom
claude/micro-opt-weak-argcount

Conversation

@Korijn

@Korijn Korijn commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

What

weak() built an inspect.Signature just to count a bound method's parameters. That costs ~7µs and runs on every Watcher/watch() creation that has a bound-method fn or callback. For a plain function whose parameters are all regular positional ones, the same count can be read straight off the code object (__code__.co_argcount), ~100x cheaper.

The fast path only applies when it's guaranteed to agree with inspect.signature: no __wrapped__ (decorated methods), no *args/**kwargs (co_flags), no keyword-only parameters. Everything else falls back to inspect.signature, preserving the previous behavior exactly.

Tests

Existing tests already cover 1/2/3-argument bound-method callbacks and the too-many-arguments error. New tests in tests/test_references.py cover the fallback path:

  • a functools.wraps-decorated bound-method callback (argument count resolved through the wrapper),
  • a *args bound-method callback (counts as a single new argument, as inspect.signature reports it).

Measurements

watch() with a bound-method fn and a bound-method callback: 32.9µs → 12.1µs per creation (20k reps, this machine).

Verification

  • pytest tests: 152 passed (2 new tests)
  • ruff check and ty check: clean

🤖 Generated with Claude Code

https://claude.ai/code/session_01UyALuqgF1ZZ3Lj88FzwGVc


Generated by Claude Code

weak() built an inspect.Signature just to count a bound method's
parameters, which costs ~7us and runs on every Watcher/watch()
creation with a bound-method fn or callback. For a plain function
whose parameters are all regular positional ones the same count can
be read straight off the code object, ~100x cheaper. Anything the
code object can't describe the same way (wrapped/decorated functions,
*args/**kwargs, keyword-only parameters) still falls back to
inspect.signature, preserving the previous behavior; the new tests
cover a decorated bound-method callback and a *args callback.

Measured: watch() with a bound-method fn and callback went from
32.9us to 12.1us.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UyALuqgF1ZZ3Lj88FzwGVc
@Korijn
Korijn merged commit 3799b1b into master Jul 10, 2026
11 checks passed
@Korijn
Korijn deleted the claude/micro-opt-weak-argcount branch July 10, 2026 13:32
berendkleinhaneveld added a commit that referenced this pull request Jul 10, 2026
First stable release. Since v0.19.0, observ has had a broad performance
overhaul, gained full type-hint coverage and a new trigger_ref API, and
picked up a documentation site.

New features
- trigger_ref(): force-notify the watchers of a proxy, mirroring Vue's
  triggerRef (#188, closes #123).
- Fully typed: modern type hints throughout, a py.typed marker, and ty
  type-checking enforced in CI (#185, #184, closes #114).

Performance
- Reworked deep-watch traversal: plain leaf values are filtered out of the
  traverse stack at push time and raw targets are traversed directly.
  Traversing large flat/shallow structures is up to ~85% faster (#190).
- Lower per-operation trap overhead and proxies constructed with positional
  flags: reactive reads/writes and proxy creation are ~35-50% faster
  (#192, #193).
- proxy_db lifetimes are managed by reference counting instead of a gc hook,
  eliminating reference cycles and making cleanup deterministic (#177).
- Scheduler and dependency-bookkeeping micro-optimizations: in-place bisect
  insertion (#191), single-read/write flush counting (#196), a set
  difference in cleanup_deps (#194), and reading the arg count off the code
  object in weak() (#195).

Correctness
- Guarantees, with tests, that observ creates no reference cycles (#189).

Documentation
- New MkDocs site published to GitHub Pages (#176), with an Internals
  section (#179) and a rewritten README (#180).

Tooling
- Modernized CI workflows (#182) and a more robust benchmark guard
  (#181, #187).
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