Yind
Yind

Reputation: 335

C++ fastest way to concatenate strings (Most optimal way)

I'm making an application that has to deliver very big reports and I encounter that the slowest part of the process is when it has to create the final JSON string, so I tried different things to speed it up and see which was the fastest

I started with += and + operation over a string. Then I change all the + with += and it improved a little. And then I tried something more bold which is using a char * buffer and manage the concatenation myself, even made my own INT andFLOAT to string functions. And it improved the time by a 30% but the process still takes about 15 segs for big reports and I would like to reduce that time to the minimum so I will paste my code here and see if there is any suggestion in how improved the performance.

char * itoa2(UINT num, char * str)
{
    if (num == 0)
    {
        *str++ = '0';
        return str;
    }

    // Process individual digits
    UCHAR nsiz = (UCHAR)log10(num) + 1;
    str += nsiz;
    while (num != 0)
    {
        *str-- = (num % 10) + '0';
        num = num/10;
    }
    return str + nsiz + 1;
}

char * ftoa2(float num, char * str) {
    int intg = (UINT)num;
    int dec = (UINT)((num - intg) * 1000);



    // Process individual digits
    UCHAR nsiz = (UCHAR)log10(intg) + 1;
    str += nsiz - 1;
    if (dec > 0)
    {
        nsiz += (UCHAR)log10(dec) + 2;
        str += nsiz - 1;
        while (dec != 0) {
            *str-- = (dec % 10) + '0';
            dec = dec / 10;
        }
        *str-- = '.';
    }

    if (intg == 0)
    {
        *str-- = '0';
    } else {
        while (intg != 0) {
            *str-- = (intg % 10) + '0';
            intg = intg / 10;
        }
    }


    return str + nsiz + 1;
}

void Formatter::jsonFormat2() {

    char * result = new char[1024 * 1024 * 1024]; //TODO: Use small buffers instead of this

    char * pos;

    string tmp;

    pos = result;

    (*pos++) = '[';

    const char * key;
    int keyPos;
    const Reporter::Entry * entry;
    set<Field *, Field::cmpOrder> * grouped = params->getGrouped();

    bool byDay, byMonth, byYear;
    time_t ts = 0;
    struct tm * lt;

    char type;

    FieldData::fieldVariant fkey(0, nullptr);
    FieldData::fieldVariant * fvar;

    byDay = params->getByDay();
    byMonth = params->getByMonth();
    byYear = params->getByYear();


    for (auto& it : *report) {
        key = it.first;
        entry = it.second;
        keyPos = 0;

        (*pos++) = '{';

        if (byDay || byMonth || byYear) {
            ts = *(time_t *)key;
            keyPos += sizeof(time_t);
            lt = gmtime(&ts);
        }

        if (byDay) {
            memcpy(pos, "\"day\":\"", 7);
            pos+=7;
            pos = itoa2(lt->tm_year + 1900, pos);
            (*pos++) = '-';
            if (lt->tm_mon+1 < 10) (*pos++) = '0';
            pos = itoa2(lt->tm_mon+1, pos);
            (*pos++) = '-';
            if (lt->tm_mday < 10) (*pos++) = '0';
            pos = itoa2(lt->tm_mday, pos);
            (*pos++) = '\"'; (*pos++) = ',';

            memcpy(pos, "\"wday\":\"", 8);
            pos+=8;

            memcpy(pos, &weekDayC[lt->tm_wday * 3], 3);
            pos+=3;

            (*pos++) = '\"'; (*pos++) = ',';
        }
        if (byMonth) {
            memcpy(pos, "\"month\":\"", 9);
            pos+=9;
            pos = itoa2(lt->tm_year + 1900, pos);
            (*pos++) = '-';
            if (lt->tm_mon+1 < 10) (*pos++) = '0';
            pos = itoa2(lt->tm_mon, pos);
            (*pos++) = '\"'; (*pos++) = ',';
        }
        if (byYear) {
            memcpy(pos, "\"year\":\"", 8);
            pos+=9;
            pos = itoa2(lt->tm_year + 1900, pos);
            (*pos++) = '\"'; (*pos++) = ',';
        }


        for (auto& field : *grouped) {
            (*pos++) = '\"';

            tmp = field->getName();
            memcpy(pos, tmp.c_str(), tmp.length());
            pos+=tmp.length();
            memcpy(pos, "\":\"", 3);
            pos+=3;

            type = field->getType();
            if (type == INT8) {
                pos = itoa2(*(key + keyPos), pos);
            } else if (type == INT16) {
                pos = itoa2(*(USHORT *)(key + keyPos), pos);
            } else if (type == INT32) {
                pos = itoa2(*(UINT *)(key + keyPos), pos);
            } else if (type == INT64) {
                pos = itoa2(*(ULONG *)(key + keyPos), pos);
            } else if (type == INT128) {
                pos = itoa2(*(UXLONG *)(key + keyPos), pos);
            } else if (type == CHAR2) {
                memcpy(pos, key + keyPos, 2);
                pos+=2;
            }

            (*pos++) = '\"'; (*pos++) = ',';


            for (auto& fieldData : *field->getFieldData()) {
                fkey.type = field->getType();

                if (fkey.type == CHAR2) {
                    fkey.data = new string((char *) (key + keyPos), 2);
                } else
                    fkey.data = (void *)key + keyPos;

                fvar = fieldData->find(&fkey);

                (*pos++) = '\"';

                tmp = fieldData->colName;
                memcpy(pos, tmp.c_str(), tmp.length());
                pos+=tmp.length();
                memcpy(pos, "\":\"", 3);
                pos+=3;
                if (fvar != nullptr) tmp = toString(fvar); else tmp = "null";
                memcpy(pos, tmp.c_str(), tmp.length());
                pos+=tmp.length();
                memcpy(pos, "\",", 2);
                pos+=2;

            }
            keyPos += field->getBytes();
        }

        memcpy(pos, "\"imps\":\"", 8);
        pos+=8;
        pos = itoa2(entry->imps, pos);
        (*pos++) = '\"'; (*pos++) = ',';

        memcpy(pos, "\"impsn\":\"", 9);
        pos+=9;
        pos = itoa2(entry->impsn, pos);
        (*pos++) = '\"'; (*pos++) = ',';

        memcpy(pos, "\"impsu\":\"", 9);
        pos+=9;
        pos = itoa2(entry->impsu, pos);
        (*pos++) = '\"'; (*pos++) = ',';

        memcpy(pos, "\"clicks\":\"", 10);
        pos+=10;
        pos = itoa2(entry->clicks, pos);
        (*pos++) = '\"'; (*pos++) = ',';

        memcpy(pos, "\"clicksu\":\"", 11);
        pos+=11;
        pos = itoa2(entry->clicksu, pos);
        (*pos++) = '\"'; (*pos++) = ',';

        memcpy(pos, "\"revenue\":\"", 11);
        pos+=11;
        pos = ftoa2(entry->revenue, pos);
        (*pos++) = '\"'; (*pos++) = ',';

        memcpy(pos, "\"revenue_2\":\"", 13);
        pos+=13;
        pos = ftoa2(entry->revenue_2, pos);
        (*pos++) = '\"'; (*pos++) = ',';

        memcpy(pos, "\"revenue_3\":\"", 13);
        pos+=13;
        pos = ftoa2(entry->revenue_3, pos);
        (*pos++) = '\"'; (*pos++) = ',';

        memcpy(pos, "\"convs\":\"", 9);
        pos+=9;
        pos = itoa2(entry->convs, pos);
        (*pos++) = '\"'; (*pos++) = ',';

        memcpy(pos, "\"convs_2\":\"", 11);
        pos+=11;
        pos = itoa2(entry->convs_2, pos);
        (*pos++) = '\"'; (*pos++) = ',';

        memcpy(pos, "\"convs_3\":\"", 11);
        pos+=11;
        pos = itoa2(entry->convs_3, pos);
        (*pos++) = '\"'; (*pos++) = ',';

        (*pos++) = '}'; (*pos++) = ',';
        pos+=3;
    }
    pos--;

    (*pos++) = ']';
    (*pos++) = '\0';

    if (params->getVerbose()) cout << string(result, pos - result);
}

Upvotes: 1

Views: 1908

Answers (1)

arayq2
arayq2

Reputation: 2554

In the same method, Formatter::jsonFormat2(), you have

char * result = new char[1024 * 1024 * 1024];

at the top, and

if (params->getVerbose()) cout << string(result, pos - result);

at the bottom.

Which means, you not only have a 1GB memory leak every time you call this method, you also do a lot of work for nothing if getVerbose() happens to return false (which you could have checked right at the beginning).

Further, if you are going to write everything to std::cout (or to any stream for that matter), consider writing the various fragments directly, using the stream insertion syntax (the << operator).

Upvotes: 1

Related Questions