[vlc-commits] Optimize vlc_memstream_puts() with constant string
Rémi Denis-Courmont
git at videolan.org
Sat Feb 25 14:59:20 CET 2017
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Feb 25 15:55:39 2017 +0200| [0579e0223a5bf2cfcdda7b858ff0dbfb2834413d] | committer: Rémi Denis-Courmont
Optimize vlc_memstream_puts() with constant string
If the string given to vlc_memstream_puts() is constant, or at least
its length is constant, use vlc_memstream_write() instead and let GCC
compute the length at build-time.
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=0579e0223a5bf2cfcdda7b858ff0dbfb2834413d
---
include/vlc_memstream.h | 11 +++++++++++
src/text/memstream.c | 4 ++--
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/include/vlc_memstream.h b/include/vlc_memstream.h
index 6333765..0ed78a7 100644
--- a/include/vlc_memstream.h
+++ b/include/vlc_memstream.h
@@ -62,4 +62,15 @@ VLC_API
int vlc_memstream_printf(struct vlc_memstream *s, const char *fmt,
...) VLC_FORMAT(2,3);
+# ifdef __GNUC__
+static inline int vlc_memstream_puts_len(struct vlc_memstream *ms,
+ const char *str, size_t len)
+{
+ return (vlc_memstream_write(ms, str, len) == len) ? (int)len : EOF;
+}
+# define vlc_memstream_puts(ms,s) \
+ (__builtin_constant_p(__builtin_strlen(s)) ? \
+ vlc_memstream_puts_len(ms,s,__builtin_strlen(s)) : \
+ vlc_memstream_puts(ms,s))
+# endif
#endif /* VLC_MEMSTREAM_H */
diff --git a/src/text/memstream.c b/src/text/memstream.c
index b1551ef..8e96309 100644
--- a/src/text/memstream.c
+++ b/src/text/memstream.c
@@ -82,7 +82,7 @@ int vlc_memstream_putc(struct vlc_memstream *ms, int c)
return fputc(c, ms->stream);
}
-int vlc_memstream_puts(struct vlc_memstream *ms, const char *str)
+int (vlc_memstream_puts)(struct vlc_memstream *ms, const char *str)
{
if (unlikely(ms->stream == NULL))
return EOF;
@@ -145,7 +145,7 @@ int vlc_memstream_putc(struct vlc_memstream *ms, int c)
return (vlc_memstream_write(ms, &(unsigned char){ c }, 1u) == 1) ? c : EOF;
}
-int vlc_memstream_puts(struct vlc_memstream *ms, const char *str)
+int (vlc_memstream_puts)(struct vlc_memstream *ms, const char *str)
{
size_t len = strlen(str);
return (vlc_memstream_write(ms, str, len) == len) ? 0 : EOF;
More information about the vlc-commits
mailing list