3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-06-06 06:03:23 +00:00

Speed up stringf / vstringf by 1.8x.

The main speedup is accomplished by avoiding a heap allocation in the common case where the final string length is less than 128. Inlining stringf & vstringf adds an additional improvement.
This commit is contained in:
Rasmus Munk Larsen 2023-10-02 11:00:00 -07:00 committed by Lofty
parent 11ffd7df40
commit 4968229efc
2 changed files with 64 additions and 45 deletions

View file

@ -74,6 +74,7 @@
#include <errno.h>
#include "libs/json11/json11.hpp"
#include "devtools/build/runtime/get_runfiles_dir.h"
YOSYS_NAMESPACE_BEGIN
@ -175,48 +176,6 @@ int ceil_log2(int x)
#endif
}
std::string stringf(const char *fmt, ...)
{
std::string string;
va_list ap;
va_start(ap, fmt);
string = vstringf(fmt, ap);
va_end(ap);
return string;
}
std::string vstringf(const char *fmt, va_list ap)
{
std::string string;
char *str = NULL;
#if defined(_WIN32 )|| defined(__CYGWIN__)
int sz = 64, rc;
while (1) {
va_list apc;
va_copy(apc, ap);
str = (char*)realloc(str, sz);
rc = vsnprintf(str, sz, fmt, apc);
va_end(apc);
if (rc >= 0 && rc < sz)
break;
sz *= 2;
}
#else
if (vasprintf(&str, fmt, ap) < 0)
str = NULL;
#endif
if (str != NULL) {
string = str;
free(str);
}
return string;
}
int readsome(std::istream &f, char *s, int n)
{
int rc = int(f.readsome(s, n));
@ -1025,7 +984,8 @@ void init_share_dirname()
return;
}
# ifdef YOSYS_DATDIR
proc_share_path = YOSYS_DATDIR "/";
proc_share_path = devtools_build::GetRunfilesDir() + "/";
proc_share_path += YOSYS_DATDIR "/";
if (check_file_exists(proc_share_path, true)) {
yosys_share_dirname = proc_share_path;
return;