Changeset 7de07a0 for irc.c


Ignore:
Timestamp:
2018-07-12T08:42:45Z (6 years ago)
Author:
dequis <dx@…>
Parents:
b9d1fdc
git-author:
dequis <dx@…> (12-07-18 08:30:59)
git-committer:
dequis <dx@…> (12-07-18 08:42:45)
Message:

Use GString for irc->sendbuffer (simpler and faster)

The old code managed growing the buffer manually and called strlen()
very often. GString has a length field and the logic to grow the buffer
is none of our business. Probably smarter, too.

This takes a "blist all" of 14k users from 8.2 to 1.3 seconds.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • irc.c

    rb9d1fdc r7de07a0  
    6262        irc->iconv = (GIConv) - 1;
    6363        irc->oconv = (GIConv) - 1;
     64
     65        irc->sendbuffer = g_string_sized_new(IRC_MAX_LINE * 2);
    6466
    6567        if (global.conf->ping_interval > 0 && global.conf->ping_timeout > 0) {
     
    305307        }
    306308
    307         g_free(irc->sendbuffer);
     309        g_string_free(irc->sendbuffer, TRUE);
    308310        g_free(irc->readbuffer);
    309311        g_free(irc->password);
     
    595597
    596598                if (now) {
    597                         g_free(irc->sendbuffer);
    598                         irc->sendbuffer = g_strdup("\r\n");
     599                        g_string_assign(irc->sendbuffer, "\r\n");
    599600                }
    600601                irc_vawrite(temp->data, format, params);
     
    611612void irc_vawrite(irc_t *irc, char *format, va_list params)
    612613{
    613         int size;
    614614        char line[IRC_MAX_LINE + 1];
    615615
     
    638638        g_strlcat(line, "\r\n", IRC_MAX_LINE + 1);
    639639
    640         if (irc->sendbuffer != NULL) {
    641                 gsize line_len = strlen(line);
    642                 gsize buffer_len = strlen(irc->sendbuffer);
    643 
    644                 size = buffer_len + line_len;
    645                 irc->sendbuffer = g_renew(char, irc->sendbuffer, size + 1);
    646                 strcpy((irc->sendbuffer + buffer_len), line);
    647         } else {
    648                 irc->sendbuffer = g_strdup(line);
    649         }
     640        g_string_append(irc->sendbuffer, line);
    650641
    651642        if (irc->w_watch_source_id == 0) {
     
    669660{
    670661        ssize_t n;
    671         size_t len;
    672 
    673         if (irc->sendbuffer == NULL) {
     662        size_t len = irc->sendbuffer->len;
     663
     664        if (len == 0) {
    674665                return;
    675666        }
    676667
    677         len = strlen(irc->sendbuffer);
    678         if ((n = send(irc->fd, irc->sendbuffer, len, 0)) == len) {
    679                 g_free(irc->sendbuffer);
    680                 irc->sendbuffer = NULL;
     668        if ((n = send(irc->fd, irc->sendbuffer->str, len, 0)) == len) {
     669                g_string_truncate(irc->sendbuffer, 0);
    681670
    682671                b_event_remove(irc->w_watch_source_id);
    683672                irc->w_watch_source_id = 0;
    684673        } else if (n > 0) {
    685                 char *s = g_strdup(irc->sendbuffer + n);
    686                 g_free(irc->sendbuffer);
    687                 irc->sendbuffer = s;
     674                g_string_erase(irc->sendbuffer, 0, n);
    688675        }
    689676        /* Otherwise something went wrong and we don't currently care
     
    702689                b_event_remove(irc->w_watch_source_id);
    703690                irc->w_watch_source_id = 0;
    704                 g_free(irc->sendbuffer);
    705                 irc->sendbuffer = NULL;
     691                g_string_truncate(irc->sendbuffer, 0);
    706692        }
    707693
Note: See TracChangeset for help on using the changeset viewer.