Skip to content

Commit

Permalink
De-duplicate fmt and format functions and optimize
Browse files Browse the repository at this point in the history
Just use a single function, also only one buffer is required for
the operation, no need to copy.

Signed-off-by: Eric Curtin <[email protected]>
  • Loading branch information
ericcurtin committed Feb 3, 2025
1 parent d92cb67 commit 58ae4a3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 34 deletions.
10 changes: 6 additions & 4 deletions common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,16 @@ std::string string_format(const char * fmt, ...) {
va_list ap2;
va_start(ap, fmt);
va_copy(ap2, ap);
int size = vsnprintf(NULL, 0, fmt, ap);
const int size = vsnprintf(NULL, 0, fmt, ap);
GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
std::vector<char> buf(size + 1);
int size2 = vsnprintf(buf.data(), size + 1, fmt, ap2);
std::string buf;
buf.resize(size);
const int size2 = vsnprintf(const_cast<char *>(buf.data()), buf.size() + 1, fmt, ap2);
GGML_ASSERT(size2 == size);
va_end(ap2);
va_end(ap);
return std::string(buf.data(), size);

return buf;
}

std::string string_strip(const std::string & str) {
Expand Down
36 changes: 10 additions & 26 deletions examples/run/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,6 @@
}
#endif

GGML_ATTRIBUTE_FORMAT(1, 2)
static std::string fmt(const char * fmt, ...) {
va_list ap;
va_list ap2;
va_start(ap, fmt);
va_copy(ap2, ap);
const int size = vsnprintf(NULL, 0, fmt, ap);
GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
std::string buf;
buf.resize(size);
const int size2 = vsnprintf(const_cast<char *>(buf.data()), buf.size() + 1, fmt, ap2);
GGML_ASSERT(size2 == size);
va_end(ap2);
va_end(ap);

return buf;
}

GGML_ATTRIBUTE_FORMAT(1, 2)
static int printe(const char * fmt, ...) {
va_list args;
Expand Down Expand Up @@ -445,11 +427,11 @@ class HttpClient {
int secs = static_cast<int>(seconds) % 60;

if (hrs > 0) {
return fmt("%dh %02dm %02ds", hrs, mins, secs);
return format("%dh %02dm %02ds", hrs, mins, secs);
} else if (mins > 0) {
return fmt("%dm %02ds", mins, secs);
return format("%dm %02ds", mins, secs);
} else {
return fmt("%ds", secs);
return format("%ds", secs);
}
}

Expand All @@ -464,7 +446,7 @@ class HttpClient {
}
}

return fmt("%.2f %s", dbl_size, suffix[i]);
return format("%.2f %s", dbl_size, suffix[i]);
}

static int update_progress(void * ptr, curl_off_t total_to_download, curl_off_t now_downloaded, curl_off_t,
Expand Down Expand Up @@ -498,7 +480,9 @@ class HttpClient {
return (now_downloaded_plus_file_size * 100) / total_to_download;
}

static std::string generate_progress_prefix(curl_off_t percentage) { return fmt("%3ld%% |", static_cast<long int>(percentage)); }
static std::string generate_progress_prefix(curl_off_t percentage) {
return format("%3ld%% |", static_cast<long int>(percentage));
}

static double calculate_speed(curl_off_t now_downloaded, const std::chrono::steady_clock::time_point & start_time) {
const auto now = std::chrono::steady_clock::now();
Expand All @@ -509,9 +493,9 @@ class HttpClient {
static std::string generate_progress_suffix(curl_off_t now_downloaded_plus_file_size, curl_off_t total_to_download,
double speed, double estimated_time) {
const int width = 10;
return fmt("%*s/%*s%*s/s%*s", width, human_readable_size(now_downloaded_plus_file_size).c_str(), width,
human_readable_size(total_to_download).c_str(), width, human_readable_size(speed).c_str(), width,
human_readable_time(estimated_time).c_str());
return format("%*s/%*s%*s/s%*s", width, human_readable_size(now_downloaded_plus_file_size).c_str(), width,
human_readable_size(total_to_download).c_str(), width, human_readable_size(speed).c_str(), width,
human_readable_time(estimated_time).c_str());
}

static int calculate_progress_bar_width(const std::string & progress_prefix, const std::string & progress_suffix) {
Expand Down
10 changes: 6 additions & 4 deletions src/llama-impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,16 @@ std::string format(const char * fmt, ...) {
va_list ap2;
va_start(ap, fmt);
va_copy(ap2, ap);
int size = vsnprintf(NULL, 0, fmt, ap);
const int size = vsnprintf(NULL, 0, fmt, ap);
GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
std::vector<char> buf(size + 1);
int size2 = vsnprintf(buf.data(), size + 1, fmt, ap2);
std::string buf;
buf.resize(size);
const int size2 = vsnprintf(const_cast<char *>(buf.data()), buf.size() + 1, fmt, ap2);
GGML_ASSERT(size2 == size);
va_end(ap2);
va_end(ap);
return std::string(buf.data(), size);

return buf;
}

std::string llama_format_tensor_shape(const std::vector<int64_t> & ne) {
Expand Down

0 comments on commit 58ae4a3

Please sign in to comment.