[llvm] [ADT] Avoid generic dispatch in StringRef::split(char) (PR #214978)

Zhiyang Chen via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 8 09:18:40 PDT 2026


https://github.com/jeffchen006 updated https://github.com/llvm/llvm-project/pull/214978

>From b8a8129437fd8547ce5a03731961b971bde00a54 Mon Sep 17 00:00:00 2001
From: Zhiyang Chen <jeffchen006 at gmail.com>
Date: Sat, 8 Aug 2026 16:18:30 +0000
Subject: [PATCH] [ADT] Avoid generic dispatch in StringRef::split(char)

split(char) built a temporary one-byte StringRef and delegated to the
generic split(StringRef): an out-of-line call into libLLVMSupport that
walks the needle-dispatch checks before reaching memchr, and taking the
parameter's address spills the separator byte to the stack at every call
site.  Call find(Separator) directly instead; it inlines to a plain
memchr.  The overload was already restricted to a single-character
separator, so behavior is unchanged.

Saves a fixed ~2.5 ns per call: ~1.7x for the common near-front
separator (Triple.cpp component splits 36.7 -> 19.8 ns, CommandLine.cpp
Arg.split('=') 6.3 -> 3.6 ns), tapering to 1.09x for a fruitless 4 KiB
scan.  Both paths bottom out in memchr, so no input class regresses.
Code size is neutral or slightly smaller.
---
 llvm/include/llvm/ADT/StringRef.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 724c27da0ee95..3cc0ea80bebc3 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -734,7 +734,10 @@ class LLVM_GSL_POINTER StringRef {
   /// \param Separator The character to split on.
   /// \returns The split substrings.
   [[nodiscard]] std::pair<StringRef, StringRef> split(char Separator) const {
-    return split(StringRef(&Separator, 1));
+    size_t Idx = find(Separator);
+    if (Idx == npos)
+      return {*this, StringRef()};
+    return {slice(0, Idx), substr(Idx + 1)};
   }
 
   /// Split into two substrings around the first occurrence of a separator



More information about the llvm-commits mailing list