Skip to content

Commit debcc3c

Browse files
committed
Logo (Image): improve performance when image is cached
1 parent 3d7d02b commit debcc3c

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/logo/image/image.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <sys/syslimits.h>
1111
#elif _WIN32
1212
#include <windows.h>
13+
#elif __linux__
14+
#include <sys/sendfile.h>
1315
#endif
1416

1517
// https://github.com/kostya/benchmarks/blob/master/base64/test-nolib.c#L145
@@ -758,10 +760,32 @@ static bool printCachedPixel(FFLogoRequestData* requestData)
758760
printf("\e[%uC", (unsigned) options->paddingLeft);
759761
fflush(stdout);
760762

761-
char buffer[32768];
762-
ssize_t readBytes;
763-
while((readBytes = ffReadFDData(FFUnixFD2NativeFD(fd), sizeof(buffer), buffer)) > 0)
764-
ffWriteFDData(FFUnixFD2NativeFD(STDOUT_FILENO), (size_t) readBytes, buffer);
763+
bool sent = false;
764+
#ifdef __linux__
765+
struct stat st;
766+
if (fstat(fd, &st) >= 0)
767+
{
768+
while (st.st_size > 0)
769+
{
770+
ssize_t bytes = sendfile(STDOUT_FILENO, fd, NULL, (size_t) st.st_size);
771+
if (bytes > 0)
772+
{
773+
sent = true;
774+
st.st_size -= bytes;
775+
}
776+
else
777+
break;
778+
}
779+
}
780+
#endif
781+
782+
if (!sent)
783+
{
784+
char buffer[32768];
785+
ssize_t readBytes;
786+
while((readBytes = ffReadFDData(FFUnixFD2NativeFD(fd), sizeof(buffer), buffer)) > 0)
787+
ffWriteFDData(FFUnixFD2NativeFD(STDOUT_FILENO), (size_t) readBytes, buffer);
788+
}
765789

766790
instance.state.logoWidth = requestData->logoCharacterWidth + options->paddingLeft + options->paddingRight;
767791
instance.state.logoHeight = requestData->logoCharacterHeight + options->paddingTop;

0 commit comments

Comments
 (0)