From 5d8d5bfd73c97610626dccbe40e74e007b2dcc9f Mon Sep 17 00:00:00 2001 From: Stuart Ianna Date: Wed, 23 Aug 2023 14:49:22 +1000 Subject: [PATCH] libc/libfread: Use memcpy to copy read-ahead buffer to caller buffer. This change improves the performance when using larger CONFIG_STDIO_BUFFER_SIZE values as it allows architecture specific, or optimized memcpy algorithms to be utilized. Link: https://git.motec.com.au/id/I317dc4da266aed1ce0f1b5f9a609759e863b9005 --- libs/libc/stdio/lib_libfread.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/libs/libc/stdio/lib_libfread.c b/libs/libc/stdio/lib_libfread.c index 44d703cfadb..1633b3ad9c4 100644 --- a/libs/libc/stdio/lib_libfread.c +++ b/libs/libc/stdio/lib_libfread.c @@ -50,6 +50,7 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream) ssize_t bytes_read; size_t remaining = count; #ifndef CONFIG_STDIO_DISABLE_BUFFERING + size_t gulp_size; int ret; #endif @@ -121,12 +122,25 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream) { /* Is there readable data in the buffer? */ - while (remaining > 0 && stream->fs_bufpos < stream->fs_bufread) - { - /* Yes, copy a byte into the user buffer */ + gulp_size = stream->fs_bufread - stream->fs_bufpos; - *dest++ = *stream->fs_bufpos++; - remaining--; + /* Avoid empty buffers or read requests greater than the size + * buffer remaining + */ + + if (gulp_size > 0) + { + if (gulp_size > count) + { + /* Clip the gulp size to the requested byte count */ + + gulp_size = count; + } + + memcpy(dest, stream->fs_bufpos, gulp_size); + + remaining -= gulp_size; + stream->fs_bufpos += gulp_size; } /* The buffer is empty OR we have already supplied the number