From 91eee2123524e4720f9dee4bc7fde3aed9934401 Mon Sep 17 00:00:00 2001 From: apocelipes Date: Tue, 28 Nov 2023 12:07:26 +0900 Subject: [PATCH] optimize(Displayserver): performance up Replace stdio with the posix api, and drop fscanf. It shows a huge performance up in my local benchmarks. About 35~40% faster per modefile than the old one. Here is the highlights of benchmarks: ```text Running ./bench_linux_amd64 Run on (8 X 1800.01 MHz CPU s) CPU Caches: L1 Data 32 KiB (x4) L1 Instruction 32 KiB (x4) L2 Unified 256 KiB (x4) L3 Unified 6144 KiB (x1) Load Average: 0.12, 0.19, 0.16 ------------------------------------------------------- Benchmark Time CPU Iterations ------------------------------------------------------- BenchStdIO 1712 ns 1712 ns 341076 BenchPOSIXIO 998 ns 998 ns 655856 ``` updates #634 --- .../displayserver/linux/displayserver_linux.c | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/detection/displayserver/linux/displayserver_linux.c b/src/detection/displayserver/linux/displayserver_linux.c index 7f11675200..3dd9e0efed 100644 --- a/src/detection/displayserver/linux/displayserver_linux.c +++ b/src/detection/displayserver/linux/displayserver_linux.c @@ -4,6 +4,10 @@ #include "util/stringUtils.h" #include +#include + +// 2 * (uint32_t string max length is 10 bytes) + 'x' + NUL +#define MODE_FILE_CONTENT_LENGTH 22 static void parseDRM(FFDisplayServerResult* result) { @@ -27,8 +31,8 @@ static void parseDRM(FFDisplayServerResult* result) ffStrbufAppendS(&drmDir, entry->d_name); ffStrbufAppendS(&drmDir, "/modes"); - FILE* modeFile = fopen(drmDir.chars, "r"); - if(modeFile == NULL) + int FF_AUTO_CLOSE_FD modeFileFd = open(drmDir.chars, O_RDONLY); + if(modeFileFd < 0) { ffStrbufSubstrBefore(&drmDir, drmDirLength); continue; @@ -36,8 +40,17 @@ static void parseDRM(FFDisplayServerResult* result) uint32_t width = 0, height = 0; - int scanned = fscanf(modeFile, "%ux%u", &width, &height); - if(scanned == 2 && width > 0 && height > 0) + char buf[MODE_FILE_CONTENT_LENGTH] = {0x00}; + if (read(modeFileFd, buf, sizeof(char) * (MODE_FILE_CONTENT_LENGTH - 1)) > 0) { + char *sep = strchr(buf, 'x'); + if (sep != NULL) { + *sep = '\0'; + width = (uint32_t)atoi(buf); + height = (uint32_t)atoi(sep + 1); + } + } + + if(width > 0 && height > 0) { ffStrbufSubstrBefore(&drmDir, drmDirLength); ffStrbufAppendS(&drmDir, entry->d_name); @@ -71,7 +84,6 @@ static void parseDRM(FFDisplayServerResult* result) ); } - fclose(modeFile); ffStrbufSubstrBefore(&drmDir, drmDirLength); }