[libc-commits] [libc] [llvm] [libc][string] Optimize strncpy() with strnlen() and memory operations (PR #235762)

Kuan-Wei Chiu via libc-commits libc-commits at lists.llvm.org
Tue Nov 24 23:00:02 PST 2026


https://github.com/visitorckw updated https://github.com/llvm/llvm-project/pull/235762

>From 01102cab1347b8bcbb9f572ccda5cbc793795640 Mon Sep 17 00:00:00 2001
From: Kuan-Wei Chiu <visitorckw at gmail.com>
Date: Wed, 25 Nov 2026 06:53:36 +0000
Subject: [PATCH] [libc][string] Optimize strncpy() with strnlen() and memory
 operations

The current strncpy implementation copies characters and pads trailing
null bytes using byte by byte loops. Replace these loops with
internal::strnlen, inline_memcpy, and inline_bzero to leverage
word-level and vectorized memory operations.

In a microbenchmark copying a 512 byte string into a 1024 byte buffer,
this reduces execution time by about 43%, or 1.77x faster.
---
 libc/src/string/CMakeLists.txt                |  4 ++++
 libc/src/string/strncpy.cpp                   | 19 +++++++++++++------
 .../libc/src/string/BUILD.bazel               |  2 ++
 3 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index dad6ad21da15b6..4427e59961039d 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -288,6 +288,10 @@ add_entrypoint_object(
     strncpy.cpp
   HDRS
     strncpy.h
+  DEPENDS
+    .memory_utils.inline_bzero
+    .memory_utils.inline_memcpy
+    .string_utils
 )
 
 add_entrypoint_object(
diff --git a/libc/src/string/strncpy.cpp b/libc/src/string/strncpy.cpp
index e271009502f2a7..4fa1056a763e8f 100644
--- a/libc/src/string/strncpy.cpp
+++ b/libc/src/string/strncpy.cpp
@@ -1,16 +1,24 @@
-//===-- Implementation of strncpy -----------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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 strncpy.
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/string/strncpy.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/string/memory_utils/inline_memcpy.h"
+#include "src/string/string_utils.h"
 #include <stddef.h> // For size_t.
 
 namespace LIBC_NAMESPACE_DECL {
@@ -22,13 +30,12 @@ LLVM_LIBC_FUNCTION(char *, strncpy,
     LIBC_CRASH_ON_NULLPTR(dest);
     LIBC_CRASH_ON_NULLPTR(src);
   }
-  size_t i = 0;
+  size_t i = internal::strnlen(src, n);
   // Copy up until \0 is found.
-  for (; 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.
-  for (; i < n; ++i)
-    dest[i] = '\0';
+  if (n > i)
+    inline_bzero(dest + i, n - i);
   return dest;
 }
 
diff --git a/utils/bazel/llvm-project-overlay/libc/src/string/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/string/BUILD.bazel
index 951412e17290dc..86fe8aa02bee2a 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/string/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/string/BUILD.bazel
@@ -248,9 +248,11 @@ libc_function(
     srcs = ["strncpy.cpp"],
     hdrs = ["strncpy.h"],
     deps = [
+        ":utils",
         "//libc/src/__support:common",
         "//libc/src/__support/macros:config",
         "//libc/src/__support/macros:null_check",
+        "//libc/src/string/memory_utils",
     ],
 )
 



More information about the libc-commits mailing list