Skip to content

Commit 2c940da

Browse files
committed
Make replace_all() have linear complexity
1 parent fa4c4e7 commit 2c940da

File tree

13 files changed

+59
-89
lines changed

13 files changed

+59
-89
lines changed

llama.cpp/common.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "json-schema-to-grammar.h"
1212
#include "llama.h"
1313
#include "llamafile/debug.h"
14+
#include "string.h"
1415

1516
#include <cosmo.h>
1617
#include <algorithm>
@@ -1731,15 +1732,20 @@ std::string string_get_sortable_timestamp() {
17311732
return std::string(timestamp_no_ns) + "." + std::string(timestamp_ns);
17321733
}
17331734

1734-
void string_replace_all(std::string & s, const std::string & search, const std::string & replace) {
1735-
if (search.empty()) {
1736-
return; // Avoid infinite loop if 'search' is an empty string
1737-
}
1735+
std::string replace_all(const std::string& s, const std::string& search, const std::string& replace) {
1736+
if (search.empty())
1737+
return s;
1738+
std::string builder;
1739+
builder.reserve(s.length());
17381740
size_t pos = 0;
1739-
while ((pos = s.find(search, pos)) != std::string::npos) {
1740-
s.replace(pos, search.length(), replace);
1741-
pos += replace.length();
1742-
}
1741+
size_t last_pos = 0;
1742+
while ((pos = s.find(search, last_pos)) != std::string::npos) {
1743+
builder.append(s, last_pos, pos - last_pos);
1744+
builder.append(replace);
1745+
last_pos = pos + search.length();
1746+
}
1747+
builder.append(s, last_pos, std::string::npos);
1748+
return builder;
17431749
}
17441750

17451751
void string_process_escapes(std::string & input) {

llama.cpp/common.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,6 @@ std::string gpt_params_get_system_info(const gpt_params & params);
289289

290290
std::vector<std::string> string_split(std::string input, char separator);
291291

292-
std::string string_strip(const std::string & str);
293-
std::string string_get_sortable_timestamp();
294-
295-
void string_replace_all(std::string & s, const std::string & search, const std::string & replace);
296-
297292
template<class T>
298293
static std::vector<T> string_split(const std::string & str, char delim) {
299294
std::vector<T> values;
@@ -309,17 +304,6 @@ static std::vector<T> string_split(const std::string & str, char delim) {
309304
}
310305

311306
bool string_parse_kv_override(const char * data, std::vector<llama_model_kv_override> & overrides);
312-
void string_process_escapes(std::string & input);
313-
314-
//
315-
// Filesystem utils
316-
//
317-
318-
bool fs_validate_filename(const std::string & filename);
319-
bool fs_create_directory_with_parents(const std::string & path);
320-
321-
std::string fs_get_cache_directory();
322-
std::string fs_get_cache_file(const std::string & filename);
323307

324308
//
325309
// Model utils

llama.cpp/llama-bench/llama-bench.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,6 @@ static T stdev(const std::vector<T> & v) {
9191
return stdev;
9292
}
9393

94-
static std::string replaceAll(std::string str, const std::string& from, const std::string& to) {
95-
size_t start_pos = 0;
96-
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
97-
str.replace(start_pos, from.length(), to);
98-
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
99-
}
100-
return str;
101-
}
102-
103-
10494
#ifdef __x86_64__
10595
static void cpuid(unsigned leaf, unsigned subleaf, unsigned *info) {
10696
asm("movq\t%%rbx,%%rsi\n\t"
@@ -159,9 +149,9 @@ static std::string get_cpu_info() { // [jart]
159149
}
160150
}
161151
#endif
162-
id = replaceAll(id, " 96-Cores", "");
163-
id = replaceAll(id, "(TM)", "");
164-
id = replaceAll(id, "(R)", "");
152+
id = replace_all(id, " 96-Cores", "");
153+
id = replace_all(id, "(TM)", "");
154+
id = replace_all(id, "(R)", "");
165155

166156
std::string march;
167157
#ifdef __x86_64__
@@ -1257,7 +1247,7 @@ struct markdown_printer : public printer {
12571247
snprintf(buf, sizeof(buf), "%.2f", t.avg_ts());
12581248
value = buf;
12591249
} else if (vmap.find(field) != vmap.end()) {
1260-
value = replaceAll(replaceAll(vmap.at(field), ".gguf", ""), ".llamafile", ""); // [jart]
1250+
value = replace_all(replace_all(vmap.at(field), ".gguf", ""), ".llamafile", ""); // [jart]
12611251
} else {
12621252
assert(false);
12631253
exit(1);

llama.cpp/llama-impl.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,3 @@ void llama_log_callback_default(ggml_log_level level, const char * text, void *
2424
#define LLAMA_LOG_INFO(...) llama_log_internal(GGML_LOG_LEVEL_INFO , __VA_ARGS__)
2525
#define LLAMA_LOG_WARN(...) llama_log_internal(GGML_LOG_LEVEL_WARN , __VA_ARGS__)
2626
#define LLAMA_LOG_ERROR(...) llama_log_internal(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
27-
28-
//
29-
// helpers
30-
//
31-
32-
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
33-
if (search.empty()) {
34-
return; // Avoid infinite loop if 'search' is an empty string
35-
}
36-
size_t pos = 0;
37-
while ((pos = s.find(search, pos)) != std::string::npos) {
38-
s.replace(pos, search.length(), replace);
39-
pos += replace.length();
40-
}
41-
}

llama.cpp/llama-vocab.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "llama-vocab.h"
44

55
#include "unicode.h"
6+
#include "string.h"
67

78
#include <algorithm>
89
#include <cassert>
@@ -152,11 +153,11 @@ static uint8_t llama_token_to_byte(const llama_vocab & vocab, llama_token id) {
152153
}
153154

154155
static void llama_escape_whitespace(std::string & text) {
155-
replace_all(text, " ", "\xe2\x96\x81");
156+
text = replace_all(text, " ", "\xe2\x96\x81");
156157
}
157158

158159
static void llama_unescape_whitespace(std::string & word) {
159-
replace_all(word, "\xe2\x96\x81", " ");
160+
word = replace_all(word, "\xe2\x96\x81", " ");
160161
}
161162

162163
struct llm_symbol {

llama.cpp/llama.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llama-sampling.h"
1212

1313
#include "unicode.h"
14+
#include "string.h"
1415

1516
#include "ggml.h"
1617
#include "ggml-alloc.h"
@@ -1424,8 +1425,8 @@ static std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i) {
14241425
if (arr_type == GGUF_TYPE_STRING) {
14251426
std::string val = gguf_get_arr_str(ctx_gguf, i, j);
14261427
// escape quotes
1427-
replace_all(val, "\\", "\\\\");
1428-
replace_all(val, "\"", "\\\"");
1428+
val = replace_all(val, "\\", "\\\\");
1429+
val = replace_all(val, "\"", "\\\"");
14291430
ss << '"' << val << '"';
14301431
} else if (arr_type == GGUF_TYPE_ARRAY) {
14311432
ss << "???";
@@ -3563,7 +3564,7 @@ struct llama_model_loader {
35633564
if (value.size() > MAX_VALUE_LEN) {
35643565
value = format("%s...", value.substr(0, MAX_VALUE_LEN - 3).c_str());
35653566
}
3566-
replace_all(value, "\n", "\\n");
3567+
value = replace_all(value, "\n", "\\n");
35673568

35683569
LLAMA_LOG_INFO("%s: - kv %3d: %42s %-16s = %s\n", __func__, i, name, type_name.c_str(), value.c_str());
35693570
}
@@ -16397,14 +16398,14 @@ static void llama_lora_adapter_init_internal(struct llama_model * model, const c
1639716398
for (ggml_tensor * cur = ggml_get_first_tensor(ctx); cur; cur = ggml_get_next_tensor(ctx, cur)) {
1639816399
std::string name(cur->name);
1639916400
if (str_endswith(name, ".lora_a")) {
16400-
replace_all(name, ".lora_a", "");
16401+
name = replace_all(name, ".lora_a", "");
1640116402
if (ab_map.find(name) == ab_map.end()) {
1640216403
ab_map[name] = llama_lora_weight(cur, nullptr);
1640316404
} else {
1640416405
ab_map[name].a = cur;
1640516406
}
1640616407
} else if (str_endswith(name, ".lora_b")) {
16407-
replace_all(name, ".lora_b", "");
16408+
name = replace_all(name, ".lora_b", "");
1640816409
if (ab_map.find(name) == ab_map.end()) {
1640916410
ab_map[name] = llama_lora_weight(nullptr, cur);
1641016411
} else {

llama.cpp/llava/clip.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llama.cpp/ggml-backend.h"
1212
#include "llama.cpp/ggml-cuda.h"
1313
#include "llama.cpp/ggml-metal.h"
14+
#include "llama.cpp/string.h"
1415

1516
#include "stb/stb_image.h"
1617

@@ -202,17 +203,6 @@ static std::string gguf_data_to_str(enum gguf_type type, const void * data, int
202203
}
203204
}
204205

205-
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
206-
if (search.empty()) {
207-
return; // Avoid infinite loop if 'search' is an empty string
208-
}
209-
size_t pos = 0;
210-
while ((pos = s.find(search, pos)) != std::string::npos) {
211-
s.replace(pos, search.length(), replace);
212-
pos += replace.length();
213-
}
214-
}
215-
216206
static std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i) {
217207
const enum gguf_type type = gguf_get_kv_type(ctx_gguf, i);
218208

@@ -230,8 +220,8 @@ static std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i) {
230220
if (arr_type == GGUF_TYPE_STRING) {
231221
std::string val = gguf_get_arr_str(ctx_gguf, i, j);
232222
// escape quotes
233-
replace_all(val, "\\", "\\\\");
234-
replace_all(val, "\"", "\\\"");
223+
val = replace_all(val, "\\", "\\\\");
224+
val = replace_all(val, "\"", "\\\"");
235225
ss << '"' << val << '"';
236226
} else if (arr_type == GGUF_TYPE_ARRAY) {
237227
ss << "???";
@@ -1073,7 +1063,7 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
10731063
if (value.size() > MAX_VALUE_LEN) {
10741064
value = format("%s...", value.substr(0, MAX_VALUE_LEN - 3).c_str());
10751065
}
1076-
replace_all(value, "\n", "\\n");
1066+
value = replace_all(value, "\n", "\\n");
10771067

10781068
LOG_TEE("%s: - kv %3d: %42s %-16s = %s\n", __func__, i, name, type_name.c_str(), value.c_str());
10791069
}

llama.cpp/llava/llava.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
33

44
#include "clip.h"
5-
#include "llama.cpp/common.h"
5+
#include "llama.cpp/string.h"
66
#include "llama.cpp/llama.h"
7+
#include "llama.cpp/log.h"
8+
#include "llama.cpp/common.h"
79
#include "llava.h"
810

911
#include <cstdio>

llama.cpp/llava/llava.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ LLAVA_API void llava_image_embed_free(struct llava_image_embed * embed);
4545
/** write the image represented by embed into the llama context with batch size n_batch, starting at context pos n_past. on completion, n_past points to the next position in the context after the image embed. */
4646
LLAVA_API bool llava_eval_image_embed(struct llama_context * ctx_llama, const struct llava_image_embed * embed, int n_batch, int * n_past);
4747

48+
struct gpt_params;
4849
LLAVA_API int llava_cli(int argc, char ** argv, gpt_params & params); // [jart]
4950

5051
#ifdef __cplusplus

llama.cpp/main/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "llamafile/version.h"
2121
#include "llama.cpp/llama.h"
22+
#include "llama.cpp/string.h"
2223
#include "llama.cpp/common.h"
2324
#include "llama.cpp/console.h"
2425
#include "llama.cpp/ggml-cuda.h"

0 commit comments

Comments
 (0)