Skip to content

Commit

Permalink
optimize CalcCapForJoin(StrVec)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed May 24, 2024
1 parent 2f8d23c commit 5e57138
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/utils/StrVec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,11 @@ int Split(StrVec& v, const char* s, const char* separator, bool collapse) {

static int CalcCapForJoin(const StrVec& v, const char* joint) {
// it's ok to over-estimate
int len = v.Size();
size_t jointLen = str::Len(joint);
int cap = len * (int)jointLen;
for (int i = 0; i < len; i++) {
char* s = v.At(i);
cap += (int)str::Len(s);
int cap = 0;
int jointLen = str::Leni(joint);
for (auto it = v.begin(); it != v.end(); it++) {
auto s = it.Span();
cap += s.Size() + 1 + jointLen;
}
return cap + 32; // +32 arbitrary buffer
}
Expand Down Expand Up @@ -596,6 +595,10 @@ char* StrVec::iterator::operator*() const {
return page->At(idxInPage);
}

StrSpan StrVec::iterator::Span() const {
return page->AtSpan(idxInPage);
}

static void Next(StrVec::iterator& it, int n) {
// TODO: optimize for n > 1
for (int i = 0; i < n; i++) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/StrVec.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct StrVec {

iterator(const StrVec* v, int idx);
char* operator*() const;
StrSpan Span() const;
iterator& operator++(); // ++it
iterator& operator++(int); // it++
iterator& operator+(int); // it += n
Expand Down

0 comments on commit 5e57138

Please sign in to comment.