Document the (uint)i < (uint)length bounds-check idiom and apply it to four sites#2417
Merged
Merged
Conversation
…o four sites Add a CLAUDE.md subsection under "Performance Considerations" explaining the unsigned-cast bounds check trick: a single unsigned comparison handles both negative and over-bounds checks, RyuJIT lowers it to one cmp+jae, and the post-comparison range fact lets the JIT elide the bounds check on the subsequent indexer access. Includes when-to-use / when-not-to-use guidance and notes the 15+ existing call sites that already follow this pattern (DictionarySlim, StringDictionarySlim, ValueStringBuilder, Arguments, TypeConverter, JsNumber, JintIdentifierExpression, ...). Apply the same idiom to four spots that still used the slower `>= 0 && < Length` form: - StringInlHelper.cs: stringBuilder.Remove guard - TemporalHelpers.cs: bracket-scan loop in date/time string parsing - PlainYearMonthConstructor.cs: suffix index check - ZonedDateTimeConstructor.cs: bracket-scan loop in ZDT parsing These are not hot paths (string parsing, called at most a few times per parse), so the change is mostly for consistency with the rest of the codebase. Behavior is identical — both forms accept exactly [0, Length). 98567 test262 + 2861 Jint.Tests pass; 0 regressions. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
lahma
enabled auto-merge (squash)
April 26, 2026 02:58
This was referenced Apr 26, 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
Two small unrelated improvements bundled together:
CLAUDE.md — adds a
Performance Considerationssubsection documenting the unsigned-cast bounds-check idiom ((uint) i < (uint) length). The pattern was already used at 15+ sites in this repo (DictionarySlim,StringDictionarySlim,ValueStringBuilder,Arguments,TypeConverter,JsNumber,JintIdentifierExpression, ...) but wasn't explicitly documented as a project convention. The new doc explains how it works (singlecmp+jae, JIT can elide the subsequent indexer bounds check), when to apply it, when it adds noise (ordinaryforloops where the JIT already elides), and the caveats (works only when length fits inint).Four candidate sites still using the slower
index >= 0 && index < x.Lengthform, rewritten to(uint) index < (uint) x.Length:Jint/Native/String/StringInlHelper.cs:43—stringBuilder.RemoveguardJint/Native/Temporal/TemporalHelpers.cs:4429— bracket-scan loop while parsing temporal stringsJint/Native/Temporal/PlainYearMonth/PlainYearMonthConstructor.cs:566— suffix index checkJint/Native/Temporal/ZonedDateTime/ZonedDateTimeConstructor.cs:671— bracket-scan loop in ZDT parsingThese are not hot paths (string parsing called at most a few times per parse), so the change is for consistency with the rest of the codebase, not measurable perf. Behavior is identical: both forms accept exactly
[0, length).Validation
Jint.Tests(net10.0): 2861 passed, 0 failed.Jint.Tests.Test262(net10.0): 98567 passed, 506 skipped, 0 failed.🤖 Generated with Claude Code