Edits for pre-compile regex(faster-compile)#5015
Edits for pre-compile regex(faster-compile)#5015theonlychant wants to merge 9 commits intogoogle:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Response from ADK Triaging Agent Hello @1proprogrammerchant, thank you for your contribution! It looks like the Contributor License Agreement (CLA) has not been signed. Before we can review this PR, please make sure you have signed the CLA. You can find more information and sign it at https://cla.developers.google.com/. Thanks! |
| return 'gemini-3.1' in mv and 'live' in mv | ||
|
|
||
| def _is_gemini_31_live(self) -> bool: | ||
| return self._isgemini_31_live() |
There was a problem hiding this comment.
is_gemini_31_live is defined twice and return statement has a typo
| input=types.LiveClientContent( | ||
| turns=[content], | ||
| turn_complete=True, | ||
| # Gemini 3.1 live models require in-session text to go through |
There was a problem hiding this comment.
This is not part of scope of this PR. Please create a separate PR for this.
There was a problem hiding this comment.
ok I will create a new PR thanks for the heads-up by the way I'm sorry for any mess ups this is actually my first time committing code to google(I am doing this for experience)
|
Hi @1proprogrammerchant , Thank you for your contribution! We appreciate you taking the time to submit this pull request. |
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
-- N/A
2. Or, if no issue exists, describe the change:
Problem:
Several hot-path functions in ADK recompile regex patterns on every invocation instead of compiling once. Additionally, multiple dict iteration patterns use
.keys()with separate value lookups instead of.items(), causing redundant hash lookups.models/registry.py-re.compile(regex).fullmatch(model)called inside theresolve()loop on every model resolutiontools/_gemini_schema_util.py- 4 separatere.sub()calls in_to_snake_case()each recompiling their patternmodels/gemma_llm.py-re.compile()for markdown code block parsing inside the function call extraction pathmemory/in_memory_memory_service.py-re.findall()recompiling on every call, plusset([...])creating an unnecessary intermediate listsessions/_session_util.py,sessions/in_memory_session_service.py,sessions/database_session_service.py-.keys()+dict[key]instead of.items()Solution:
_compiled_regex_cachedict in the model registry, populated at registration timeset([...])with set comprehension{...}to avoid intermediate list allocationfor key in dict.keys(): dict[key]withfor key, value in dict.items()for single-pass iterationThese are all drop-in replacements with identical behavior -- no API or behavioral changes.
Testing Plan
Unit Tests:
-- No new tests needed. All changes are purely internal optimizations that preserve identical behavior. Existing tests in
tests/unittests/models/,tests/unittests/sessions/, andtests/unittests/tools/cover the modified functions.Manual End-to-End (E2E) Tests:
-- Not applicable. These are micro-optimizations to existing internal code paths with no user-facing API changes.
Checklist
Additional context
Files changed (7):
src/google/adk/models/registry.py-- Pre-compile regex at registration time into_compiled_regex_cachesrc/google/adk/tools/_gemini_schema_util.py-- 4 module-level compiled regex constantssrc/google/adk/models/gemma_llm.py-- Module-level_MARKDOWN_CODE_BLOCK_REsrc/google/adk/memory/in_memory_memory_service.py-- Module-level_RE_WORDS+ set comprehensionsrc/google/adk/sessions/_session_util.py--.items()iterationsrc/google/adk/sessions/in_memory_session_service.py--.items()iterationsrc/google/adk/sessions/database_session_service.py--.items()iteration