Fix #72: Reduce time complexity of strlen checks#141
Open
raza-khan0108 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes hot-path string handling in the assistant’s text processing pipeline to avoid repeated strlen() traversals (addressing Issue #72), reducing unnecessary CPU work during continuous input handling.
Changes:
- Cache
strlen(example)once insrc/analysis.cinstead of recomputing it per loop iteration. - Cache
strlen(str)once insrc/requests.cwhen stripping the trailing newline. - Replace
strlen(...) > 0-style emptiness checks with anO(1)first-character check insrc/word_list.c.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/analysis.c | Precomputes example_len to prevent repeated strlen(example) calls in the character-normalization loop. |
| src/requests.c | Computes len = strlen(str) once before newline trimming instead of calling strlen multiple times. |
| src/word_list.c | Uses classifier[...] [0] != '\0' instead of strlen(...) for non-empty checks during word list construction. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
This PR addresses Issue #72, focusing on reducing unnecessary time complexity penalties (primarily$O(n^2)$ behavior and redundant computations) during text analysis and processing.
By avoiding repetitive string traversals, these changes optimize CPU utilization and significantly decrease string parsing overhead during continuous input handling.
Changes Included
Optimized Loop Evaluation in
src/analysis.cstrlen(example)on every iteration (for (...; iter_char < strlen(example); ...)). Becausestrlenexecutes inexample_lenbefore the loop. The complexity is now strictlyCached Redundant Traversals in
src/requests.cstrlen(str)was called three distinct times in immediate succession to verify and strip newline characters at the end of the input.strlen(str)into the existinglenvariable to execute only once. This reduces unnecessary string scans by two-thirds for this operation.Replaced$O(n)$ Emptiness Checks with $O(1)$ Array Indexing in
src/word_list.cstrlen()was used simply to check if a word was non-empty.classifier[...][0] != '\0'. This bypasses traversing words altogether while safely verifying they aren't empty.Testing/Verification
len,example_len) to ensure variables properly persist as expected due to the#includedesign architecture operating withinmain.c's maindo-whileloop.