compiler: use memcmp to compare strings#5149
Draft
niaow wants to merge 1 commit intotinygo-org:devfrom
Draft
Conversation
This replaces our runtime stringEqual and stringLess functions with calls to libc's memcmp. This has a few advantages: 1. Memory comparison functions are not duplicated between Go and C 2. LLVM can optimize the compare-to-empty case by itself, so OptimizeStringEqual is no longer needed 3. LLVM can rewrite small constant-length memcmp operations into direct loads + compares 4. Compares to constants are generally compiled into simpler IR The downside of this is that all of the comparison logic must be handled by the compiler frontend. For string equality checks, we only need to compare the lengths and memcmp if they are equal. String order checks are messier, since we need to: 1. Find the minimum length 2. Call memcmp with the minimum length 3. Compare the lengths 4. Merge the length comparison with the memory comparison
dgryski
approved these changes
Jan 5, 2026
Member
dgryski
left a comment
There was a problem hiding this comment.
With the exception of the signed/unsigned length question this is probably good to merge. And if it's broken we'll find out soon enough :D
| // Calculate the minimum of the two string lengths. | ||
| lhsLen := b.CreateExtractValue(lhs, 1, "strlt.lhs.len") | ||
| rhsLen := b.CreateExtractValue(rhs, 1, "strlt.rhs.len") | ||
| minFnName := "llvm.umin.i" + strconv.Itoa(b.uintptrType.IntTypeWidth()) |
Member
Author
There was a problem hiding this comment.
The top bit is never set so they are both, although AVR is slightly awkward since the length is actually uintptr.
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 replaces our runtime stringEqual and stringLess functions with calls to libc's memcmp. This has a few advantages:
The downside of this is that all of the comparison logic must be handled by the compiler frontend. For string equality checks, we only need to compare the lengths and memcmp if they are equal. String order checks are messier, since we need to: