Skip to content

Fix #72: Reduce time complexity of strlen checks#141

Open
raza-khan0108 wants to merge 1 commit into
ritwik12:masterfrom
raza-khan0108:fix/issue-72-time-complexity
Open

Fix #72: Reduce time complexity of strlen checks#141
raza-khan0108 wants to merge 1 commit into
ritwik12:masterfrom
raza-khan0108:fix/issue-72-time-complexity

Conversation

@raza-khan0108

Copy link
Copy Markdown

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

  1. Optimized Loop Evaluation in src/analysis.c

    • Before: The loop condition evaluated strlen(example) on every iteration (for (...; iter_char < strlen(example); ...)). Because strlen executes in $O(n)$ time, placing it in an $n$-iteration loop escalated the complexity to $O(n^2)$.
    • After: Pre-calculated the length into a variable example_len before the loop. The complexity is now strictly $O(n)$.
  2. Cached Redundant Traversals in src/requests.c

    • Before: strlen(str) was called three distinct times in immediate succession to verify and strip newline characters at the end of the input.
    • After: Extracted strlen(str) into the existing len variable to execute only once. This reduces unnecessary string scans by two-thirds for this operation.
  3. Replaced $O(n)$ Emptiness Checks with $O(1)$ Array Indexing in src/word_list.c

    • Before: Inside the deep 3-level loop structure aggregating words from the classifier, strlen() was used simply to check if a word was non-empty.
    • After: Swapped the length check with a highly optimized constant-time index check: classifier[...][0] != '\0'. This bypasses traversing words altogether while safely verifying they aren't empty.

Testing/Verification

  • Traced local variable scoping (e.g. len, example_len) to ensure variables properly persist as expected due to the #include design architecture operating within main.c's main do-while loop.
  • Analyzed C pointer indexing over 3-dimensional classifier arrays to ensure standard C-compliance to avoid segfaults.
  • Avoided regression by retaining all the original logical pathways.

Copilot AI review requested due to automatic review settings June 22, 2026 09:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in src/analysis.c instead of recomputing it per loop iteration.
  • Cache strlen(str) once in src/requests.c when stripping the trailing newline.
  • Replace strlen(...) > 0-style emptiness checks with an O(1) first-character check in src/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.

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.

2 participants