[libc-commits] [libc] 5b7b9ec - [libc][string] Optimize stpncpy() with strnlen() and inline_memcpy() (#246447)
via libc-commits
libc-commits at lists.llvm.org
Fri Nov 27 09:46:56 PST 2026
Author: Kuan-Wei Chiu
Date: 2026-11-27T17:46:51Z
New Revision: 5b7b9ec94d2f9c15e51033b03a4059cf80d3c605
URL: https://github.com/llvm/llvm-project/commit/5b7b9ec94d2f9c15e51033b03a4059cf80d3c605
DIFF: https://github.com/llvm/llvm-project/commit/5b7b9ec94d2f9c15e51033b03a4059cf80d3c605.diff
LOG: [libc][string] Optimize stpncpy() with strnlen() and inline_memcpy() (#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.
Added:
Modified:
libc/src/string/CMakeLists.txt
libc/src/string/stpncpy.cpp
Removed:
################################################################################
diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 4ccc5f5da9ed8d..1db6a62e1941fb 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -103,6 +103,11 @@ add_entrypoint_object(
stpncpy.h
DEPENDS
.memory_utils.inline_bzero
+ .memory_utils.inline_memcpy
+ .string_utils
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.__support.macros.null_check
)
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