refactor: improve time complexity and reduce memory allocations#5601
Open
chabinhwang wants to merge 1 commit intospring-projects:mainfrom
Open
refactor: improve time complexity and reduce memory allocations#5601chabinhwang wants to merge 1 commit intospring-projects:mainfrom
chabinhwang wants to merge 1 commit intospring-projects:mainfrom
Conversation
- DefaultToolCallingManager: pre-compute HashSet of existing tool names to reduce resolveToolDefinitions lookup from O(N*M) to O(N+M) - ModelOptionsUtils: change interfaceNames from List to Set for O(1) contains() instead of O(N), use Character.toUpperCase() to avoid unnecessary String allocation in toGetName() - Vector stores (16 files): replace documents.indexOf(document) O(N) lookup inside loops with index-based access O(1), eliminating O(N^2) overall complexity in doAdd methods Signed-off-by: chabinhwang <7chabin@gmail.com>
ericbottard
approved these changes
Mar 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
HashSetof existing tool names to reduceresolveToolDefinitionslookup from O(N×M) to O(N+M)interfaceNamesfromListtoSetfor O(1)contains()instead of O(N), useCharacter.toUpperCase()to avoid unnecessaryStringallocation intoGetName()documents.indexOf(document)O(N) lookup inside loops with index-based access O(1), eliminating O(N²) overall complexity indoAddmethodsDetails
DefaultToolCallingManager (
resolveToolDefinitions)Previously, for each tool name in
chatOptions.getToolNames(), the code streamed through alltoolCallbackswith.anyMatch()— resulting in O(N×M) complexity. Now aHashSet<String>of existing tool callback names is pre-computed once, reducing the check to O(1) per iteration.ModelOptionsUtils (
mergeBeans/toGetName)interfaceNameswas collected as aList<String>but used with.contains()which is O(N) per call. Changed toSet<String>for O(1) lookups.toGetName()usedname.substring(0, 1).toUpperCase()which creates an intermediateStringobject. Changed toCharacter.toUpperCase(name.charAt(0))to avoid unnecessary allocation.Vector Stores (16 files)
All vector store
doAddmethods useddocuments.indexOf(document)inside a for-each loop to correlate documents with their embeddings. SinceindexOfis O(N), this made the overall loop O(N²). Replaced with indexed for-loops usingembeddings.get(i)for O(1) access.Affected stores: Cassandra, Chroma, Couchbase, GemFire, MariaDB, Milvus, MongoDB Atlas, Neo4j, OpenSearch, Oracle, Pinecone, Qdrant, Redis, Typesense, Azure, CosmosDB