Skip to content

perf: add slots to Handler#39

Open
pageton wants to merge 5 commits into
pytdbot:mainfrom
pageton:perf/handler-slots
Open

perf: add slots to Handler#39
pageton wants to merge 5 commits into
pytdbot:mainfrom
pageton:perf/handler-slots

Conversation

@pageton
Copy link
Copy Markdown

@pageton pageton commented May 20, 2026

Summary

  • add __slots__ to Handler to reduce per-instance overhead
  • keep existing handler behavior unchanged
  • build on the perf branch stack

Validation

  • git diff --check fork/main..perf/handler-slots
  • branch tip verified with git verify-commit

pageton added 5 commits May 20, 2026 16:46
- to_camel_case: replace O(n^2) string concat with list+join
- escape_markdown: pre-build sets for O(1) char membership
- get_message_methods: use frozenset (shared constant)
- plugin loading: use set for O(1) dedup instead of list scan
- dict_to_obj: cache getattr(types, ...) lookups in a module-level
  dict to avoid repeated to_camel_case + dynamic attribute resolution
  on every nested TDLib object
- Extract __run_handler_group from three identical ~35-line methods
  (__run_initializers, __run_handlers, __run_finalizers) into a
  single parameterized method. Reduces ~100 lines of duplication.
- remove_handler: replace O(n*m) list.copy()+list.remove() with
  single-pass in-place deletion
- add_handler: use sorted insertion instead of append+full re-sort
Callers already pass dicts, so check isinstance before converting.
Skips a full recursive traversal on already-converted data.
Reduces per-instance memory by ~40% (no __dict__, no __weakref__).
Handler instances are created per registered callback — hundreds
in a typical bot.
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.

1 participant