lynx-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

lynx-dev more memory optimizations (patch10)


From: Leonid Pauzner
Subject: lynx-dev more memory optimizations (patch10)
Date: Sun, 3 Nov 2002 21:13:10 +0300 (MSK)

Optimize memory usage:

* in SGML.c, element stack now use a pool of just 10 elements
  to avoid most of malloc/free calls.
* in HTParse(), use single alloca instead of three malloc/free pairs.



diff -u -p old/sgml.c ./sgml.c
--- old/sgml.c  Fri Oct 18 21:23:48 2002
+++ ./sgml.c    Sun Nov  3 15:32:44 2002
@@ -120,6 +120,26 @@ struct _HTElement {
        HTTag*          tag;    /* The tag at this level  */
 };

+/* storage for Element Stack */
+#define DEPTH 10
+static HTElement pool[DEPTH];
+static int depth = 0;
+
+HTElement* pool_get()
+{
+    depth++;
+    if (depth > DEPTH)
+       return (HTElement*) malloc(sizeof(HTElement));
+    return (pool + depth - 1);
+}
+void pool_free(HTElement* e)
+{
+    if (depth > DEPTH)
+       FREE(e);
+    depth--;
+    return;
+}
+
 typedef enum {
     S_text = 0
     ,S_attr
@@ -1034,7 +1054,7 @@ PRIVATE void do_close_stacked ARGS1(
        e,
        (char **)&context->include);
     context->element_stack = stacked->next;
-    FREE(stacked);
+    pool_free(stacked);
     context->no_lynx_specialcodes = context->element_stack ?
        (context->element_stack->tag->flags & Tgf_nolyspcl) : NO;
 }
@@ -1179,7 +1199,7 @@ PRIVATE void end_element ARGS2(
            context->element_stack->tag = ALT_TAGP_OF_TAGNUM(e);
        } else {
            context->element_stack = N->next;           /* Remove from stack */
-           FREE(N);
+           pool_free(N);
        }
        context->no_lynx_specialcodes = context->element_stack ?
            (context->element_stack->tag->flags & Tgf_nolyspcl) : NO;
@@ -1346,7 +1366,7 @@ PRIVATE void start_element ARGS1(
     if (status == HT_PARSER_OTHER_CONTENT)
        new_tag = ALT_TAGP(new_tag);    /* this is only returned for OBJECT */
     if (new_tag->contents != SGML_EMPTY) {             /* i.e., tag not empty 
*/
-       HTElement * N = (HTElement *)malloc(sizeof(HTElement));
+       HTElement * N = pool_get();
        if (N == NULL)
            outofmem(__FILE__, "start_element");
        N->next = context->element_stack;


--- old\htparse.c       Sun Oct 20 18:52:44 2002
+++ htparse.c  Wed Oct 30 18:14:58 2002
@@ -157,6 +157,14 @@ PRIVATE void scan ARGS2(
 } /*scan */


+#if !defined(alloca) || defined(LY_FIND_LEAKS)
+#define LYalloca(x)        malloc(x)
+#define LYalloca_free(x)   free(x)
+#else
+#define LYalloca(x)        alloca(x)
+#define LYalloca_free(x)   x = 0 /*avoid warning*/
+#endif
+
 /*     Parse a Name relative to another name.                  HTParse()
 **     --------------------------------------
 **
@@ -169,7 +177,7 @@ PRIVATE void scan ARGS2(
 **     wanted          A mask for the bits which are wanted.
 **
 ** On exit,
-**     returns         A pointer to a calloc'd string which MUST BE FREED
+**     returns         A pointer to a malloc'd string which MUST BE FREED
 */
 PUBLIC char * HTParse ARGS3(
        CONST char *,   aName,
@@ -178,7 +186,7 @@ PUBLIC char * HTParse ARGS3(
 {
     char * result = NULL;
     char * return_value = NULL;
-    int len;
+    int len, len1, len2;
     char * name = NULL;
     char * rel = NULL;
     char * p;
@@ -196,19 +204,25 @@ PUBLIC char * HTParse ARGS3(
            wanted &= ~(PARSE_STRICTPATH | PARSE_QUERY); /* ignore details */
     }
     /*
-    ** Allocate the output string.
+    ** Allocate the temporary string. Optimized.
     */
-    len = strlen(aName) + strlen(relatedName) + 10;
-    result = typecallocn(char, len);   /* Lots of space: more than enough */
+    len1 = strlen(aName) + 1;
+    len2 = strlen(relatedName) + 1;
+    len = len1 + len2 + 8;     /* Lots of space: more than enough */
+
+    result = (char*)LYalloca(len + len1 + len2);
     if (result == NULL) {
        outofmem(__FILE__, "HTParse");
     }
+    *result = '\0';
+    name = result + len;
+    rel = name + len1;

     /*
     ** Make working copies of the input strings to cut up.
     */
-    StrAllocCopy(name, aName);
-    StrAllocCopy(rel, relatedName);
+    memcpy(name, aName, len1);
+    memcpy(rel, relatedName, len2);

     /*
     ** Cut up the strings into URL fields.
@@ -451,11 +465,9 @@ PUBLIC char * HTParse ARGS3(
                             given.anchor : related.anchor);
        }
     CTRACE((tfp, "HTParse:      result:%s\n", result));
-    FREE(rel);
-    FREE(name);

     StrAllocCopy(return_value, result);
-    FREE(result);
+    LYalloca_free(result);

     return return_value;               /* exactly the right length */
 }
@@ -470,7 +482,7 @@ PUBLIC char * HTParseAnchor ARGS1(
     if (!strncasecomp(aName, "http://";, 7) ||
                !strncasecomp(aName, "file://", 7) ||
                !strncasecomp(aName, "https://";, 8)) { /* fast way */
-       char * p;
+       CONST char * p;
        for (p = aName; *p && *p != '#'; p++)
            ;
        if (*p++) {



; To UNSUBSCRIBE: Send "unsubscribe lynx-dev" to address@hidden

reply via email to

[Prev in Thread] Current Thread [Next in Thread]