[libc-commits] [libc] [libc][string] Optimize stpncpy() with strnlen() and inline_memcpy() (PR #246447)

Kuan-Wei Chiu via libc-commits libc-commits at lists.llvm.org
Thu Nov 26 08:46:39 PST 2026


https://github.com/visitorckw created https://github.com/llvm/llvm-project/pull/246447

The current stpncpy implementation copies characters using a byte by byte loop before padding trailing null bytes with inline_bzero. Replace the copy loop with internal::strnlen and inline_memcpy to leverage vectorized string and memory operations, matching strncpy.

In a microbenchmark copying a 512 byte string into a 1024 byte buffer, this reduces execution time by about 83%, or 6.1x faster.

>From f2a72b17b6010b9438242d1796ae98d9f76ed6c9 Mon Sep 17 00:00:00 2001
From: Kuan-Wei Chiu <visitorckw at gmail.com>
Date: Thu, 26 Nov 2026 16:43:52 +0000
Subject: [PATCH] [libc][string] Optimize stpncpy() with strnlen() and
 inline_memcpy()

The current stpncpy implementation copies characters using a byte by byte
loop before padding trailing null bytes with inline_bzero. Replace the
copy loop with internal::strnlen and inline_memcpy to leverage vectorized
string and memory operations, matching strncpy.

In a microbenchmark copying a 512 byte string into a 1024 byte buffer,
this reduces execution time by about 83%, or 6.1x faster.
---
 libc/src/string/CMakeLists.txt |  2 ++
 libc/src/string/stpncpy.cpp    | 18 ++++++++++++------
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 4427e59961039d..d97e90016d57a4 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -92,6 +92,8 @@ add_entrypoint_object(
     stpncpy.h
   DEPENDS
     .memory_utils.inline_bzero
+    .memory_utils.inline_memcpy
+    .string_utils
 )
 
 add_entrypoint_object(
diff --git a/libc/src/string/stpncpy.cpp b/libc/src/string/stpncpy.cpp
index 47bf4c6e86fba6..99ddeb4f7654fa 100644
--- a/libc/src/string/stpncpy.cpp
+++ b/libc/src/string/stpncpy.cpp
@@ -1,17 +1,24 @@
-//===-- Implementation of stpncpy -----------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of stpncpy.
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/string/stpncpy.h"
+
+#include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/null_check.h"
 #include "src/string/memory_utils/inline_bzero.h"
-
-#include "src/__support/common.h"
+#include "src/string/memory_utils/inline_memcpy.h"
+#include "src/string/string_utils.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
@@ -22,10 +29,9 @@ LLVM_LIBC_FUNCTION(char *, stpncpy,
     LIBC_CRASH_ON_NULLPTR(dest);
     LIBC_CRASH_ON_NULLPTR(src);
   }
-  size_t i;
+  size_t i = internal::strnlen(src, n);
   // Copy up until \0 is found.
-  for (i = 0; i < n && src[i] != '\0'; ++i)
-    dest[i] = src[i];
+  inline_memcpy(dest, src, i);
   // When n>strlen(src), n-strlen(src) \0 are appended.
   if (n > i)
     inline_bzero(dest + i, n - i);



More information about the libc-commits mailing list