Skip to content

Commit e6fbf84

Browse files
committed
New implementation of strlcpy based on OpenBSD - should be dramatically
faster the bigger the string is. Anywhere from 47x faster with 1KB strings to 819x faster with very long, truncated strings. It's a naive two-pass implementation using strlen and memcpy. These builtins are often SIMD accelerated in the standard library, hence much faster throughput vs the naive hand rolled version
1 parent b97939c commit e6fbf84

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

libretro-common/compat/compat_strl.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,16 @@
2828
/* Implementation of strlcpy()/strlcat() based on OpenBSD. */
2929

3030
#if !(defined(__MACH__) && defined(__APPLE__))
31-
size_t strlcpy(char *s, const char *source, size_t len)
31+
size_t strlcpy(char *s, const char *in, size_t len)
3232
{
33-
size_t _len = len;
34-
size_t __len = 0;
35-
if (_len)
36-
while (--_len && (*s++ = *source++)) __len++;
37-
if (!_len)
38-
{
39-
if (len) *s = '\0';
40-
while (*source++) __len++;
41-
}
42-
return __len;
33+
size_t _len = strlen(in);
34+
if (len)
35+
{
36+
size_t __len = _len < len - 1 ? _len : len - 1;
37+
memcpy(s, in, __len);
38+
s[__len] = '\0';
39+
}
40+
return _len;
4341
}
4442

4543
size_t strlcat(char *s, const char *source, size_t len)

0 commit comments

Comments
 (0)