Skip to content

perf: stop loading scikit-learn at agent start-up - #551

Open
ahmad-ajmal wants to merge 1 commit into
V1.4.6from
fix/drop-sklearn-startup
Open

ahmad-ajmal wants to merge 1 commit into
V1.4.6from
fix/drop-sklearn-startup

Conversation

@ahmad-ajmal

Copy link
Copy Markdown
Collaborator

Why

After a reboot, macOS checks every native library again the first time it loads. One launch after a reboot took 42 s, and 22 s of that was imports stalled behind about 230 of those checks.

154 of the 190 native libraries the agent loads at start-up come from one line in agent_core/core/impl/event_stream/event_stream.py:

from sklearn.feature_extraction.text import TfidfVectorizer

That import pulls in scikit-learn, scipy and pandas. It is used only by _extract_keywords, which picks 5 keywords from a single message to label a long output that gets saved to a file.

What changes

  • _extract_keywords is now plain Python: the same tokenizer, scikit-learn's English stop words, single words and word pairs, ranked by count. With one document and default settings, TF-IDF ranks terms by count, so the keywords are the same. I compared old and new on 3,014 texts, from whole repository files and random slices of them to Unicode and empty input. All 3,014 matched.
  • event_stream/_stop_words.py is scikit-learn's 318-word list, copied with its BSD-3 licence notice.
  • scikit-learn is removed from requirements.txt and from the launch dependency check in app/runtime_preflight.py. If it stayed in the check, launches would fail with a missing-dependency error.
  • The five lock files are regenerated. They drop scikit-learn, scipy, joblib, threadpoolctl and cloudpickle. Only joblib needed cloudpickle.
  • tests/test_event_stream_keywords.py covers:
    • keywords matching the scikit-learn version on fixed cases;
    • the stop-words list;
    • a long-output pointer still carrying keywords;
    • importing the event stream not loading scikit-learn.

Results (macOS, Apple silicon)

Before After
Native libraries loaded at agent start-up 190 36
Agent import 3.1 s 1.75 s
Launch dependency check 1.23 s 0.56 s
Launch until ready, same boot (median of 4) 6.07 s 5.12 s

The launch after a reboot is where most of the gain should be, but I haven't measured it with this change yet.

Testing

  • The new tests pass, and the full suite gives the same results as V1.4.6.
  • Not yet done: a full install from a build with this change, and a launch after a reboot.

event_stream imported sklearn's TfidfVectorizer only to pick 5 keywords from one message. That import loaded scikit-learn, scipy and pandas: 154 of the 190 native libraries the agent loads at start-up, each re-checked by macOS after every reboot. Replace it with plain Python that gives the same keywords (checked on 3,014 texts), drop scikit-learn from requirements and the launch check, and regenerate the locks (removes scikit-learn, scipy, joblib, threadpoolctl, cloudpickle).
@ahmad-ajmal
ahmad-ajmal marked this pull request as ready for review December 3, 2026 14:54
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