From 06ecd5a1ffef795ff42e1653fafad20684cf9e6e Mon Sep 17 00:00:00 2001 From: Carter Li Date: Wed, 25 Mar 2026 11:01:56 +0800 Subject: [PATCH] Smbios (Linux): improve performance of small file reading --- src/common/FFstrbuf.h | 1 + src/common/impl/smbiosHelper.c | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/common/FFstrbuf.h b/src/common/FFstrbuf.h index 59497b56e..6c194687f 100644 --- a/src/common/FFstrbuf.h +++ b/src/common/FFstrbuf.h @@ -230,6 +230,7 @@ static inline void ffStrbufInitMoveS(FFstrbuf* strbuf, char* heapStr) ffStrbufInitMoveNS(strbuf, (uint32_t) strlen(heapStr), heapStr); } +// Despite the name, this function resets strbuf to the initial/unallocated state static inline void ffStrbufDestroy(FFstrbuf* strbuf) { if(strbuf->allocated > 0) diff --git a/src/common/impl/smbiosHelper.c b/src/common/impl/smbiosHelper.c index 34e98a80f..a41638ccb 100644 --- a/src/common/impl/smbiosHelper.c +++ b/src/common/impl/smbiosHelper.c @@ -161,15 +161,27 @@ static bool parseSmbiosTable(const uint8_t* data, uint32_t length) #ifdef __linux__ bool ffGetSmbiosValue(const char* devicesPath, const char* classPath, FFstrbuf* buffer) { - if (ffReadFileBuffer(devicesPath, buffer)) + // /sys/class/dmi/id/* are all pseudo-files with very small content + // so reading the whole file at once is efficient + ffStrbufEnsureFixedLengthFree(buffer, 127); + + ssize_t len = ffReadFileData(devicesPath, buffer->allocated - 1, buffer->chars); + if (len > 0) { + assert(len < buffer->allocated); + buffer->chars[len] = '\0'; + buffer->length = (uint32_t) len; ffStrbufTrimRightSpace(buffer); if(ffIsSmbiosValueSet(buffer)) return true; } - if (ffReadFileBuffer(classPath, buffer)) + len = ffReadFileData(classPath, buffer->allocated - 1, buffer->chars); + if (len > 0) { + assert(len < buffer->allocated); + buffer->chars[len] = '\0'; + buffer->length = (uint32_t) len; ffStrbufTrimRightSpace(buffer); if(ffIsSmbiosValueSet(buffer)) return true;