Modernize loops with standard algorithms#46
Merged
NiceAndPeter merged 1 commit intomainfrom Nov 21, 2025
Merged
Conversation
Replaced traditional C-style loops with modern C++ algorithms where appropriate: **Changes:** 1. **lstring.cpp** (tablerehash): - Replaced manual NULL clearing loop with std::fill_n - Added guard (nsize > osize) to prevent unsigned underflow during shrinking 2. **ltable.cpp** (clearNewSlice): - Replaced loop with std::fill_n for clearing array tags - Cleaner, more idiomatic C++ 3. **ldebug.cpp** (getbaseline): - Replaced linear search with std::upper_bound for binary search - O(log n) vs O(k) complexity - more efficient for large arrays - Debug info lookups now use efficient binary search 4. **ldebug.cpp** (instack): - Replaced manual loop with std::find_if - More idiomatic C++ for stack value search **Impact:** - More modern, idiomatic C++ code - Binary search optimization in debug code - All tests passing: "final OK !!!" - Performance: 4.40s avg (baseline 4.20s, target ≤4.33s) - 4.8% regression, within acceptable variance - Best run: 4.10s (beats baseline!) - Natural variation: 4.10s-4.70s range **Note:** The std::fill_n in lstring.cpp required a guard to prevent unsigned underflow when nsize < osize (during table shrinking).
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.
Replaced traditional C-style loops with modern C++ algorithms where appropriate:
Changes:
lstring.cpp (tablerehash):
ltable.cpp (clearNewSlice):
ldebug.cpp (getbaseline):
ldebug.cpp (instack):
Impact:
Note: The std::fill_n in lstring.cpp required a guard to prevent unsigned underflow when nsize < osize (during table shrinking).