anonymous2ch / libproxy

Automatically exported from code.google.com/p/libproxy
GNU Lesser General Public License v2.1
0 stars 0 forks source link

px_readline is insanely inefficient #85

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It reads lines using recursive calls and byte-by-byte reads.

        /* If our buffer is full, add more to the buffer. */
        if (bufsize <= strlen(buffer))
        {
                char *tmp = px_malloc0(1024 + strlen(buffer) + 1);
                strcpy(tmp, buffer);
                free(buffer);
                buffer = tmp;
                bufsize = strlen(buffer) + 1024;
        }

Can you at least not call strlen() thrice?

        strncat(buffer, &c, 1);

Quadratic (relative line length) run time.

Or maybe don't torture yourself and just use fgets()?

Original issue reported on code.google.com by vda.li...@googlemail.com on 23 Feb 2010 at 4:11

GoogleCodeExporter commented 9 years ago
Good point.  I must have been drinking that weekend. :)

More seriously, I looked through the code in trunk and found three 
implementations of
readline (the original one is gone).

#1 - utils/proxy.c
This one is fixed, it now uses fgets(). See r566.

#2 - libproxy/url.cpp
This one might be improved.  However, we cannot use fgets here since this is a 
socket
(sockets are not file descriptors in win32).  However, this is a simple enough
implementation that even though it sucks to use all the recv() calls (one byte 
at a
time), I might just leave it since performance will be io bound anyway.

#3 - libproxy/modules/config_gnome.cpp
This one could probably just be converted to fgets(), but I haven't done it yet.

Original comment by npmccallum@gmail.com on 23 Feb 2010 at 6:51

GoogleCodeExporter commented 9 years ago
r567 fixes #3.  #2 will remain unfixed unless you have any suggestions (patches 
welcome).

Original comment by npmccallum@gmail.com on 24 Feb 2010 at 3:32