Kaynağa Gözat

Only compile URI regexp once.

Stefano Cossu 4 yıl önce
ebeveyn
işleme
e610b7e864
5 değiştirilmiş dosya ile 35 ekleme ve 21 silme
  1. 3 0
      .gitmodules
  2. 8 0
      Makefile
  3. 1 0
      ext/rhashmap
  4. 2 2
      src/buffer.c
  5. 21 19
      src/term.c

+ 3 - 0
.gitmodules

@@ -13,3 +13,6 @@
 [submodule "ext/qlibc"]
 	path = ext/qlibc
 	url = https://github.com/wolkykim/qlibc.git
+[submodule "ext/rhashmap"]
+	path = ext/rhashmap
+	url = https://github.com/rmind/rhashmap.git

+ 8 - 0
Makefile

@@ -24,3 +24,11 @@ test:
 		-luuid \
 		ext/xxHash/xxhash.c src/*.c test.c \
 		-o bin/test
+
+profile:
+	gcc -g -Wall \
+		-std=c99 \
+		-Iinclude -Iext/xxHash -Iext/klib -Itest \
+		-luuid \
+		ext/xxHash/xxhash.c src/*.c test.c \
+		-o bin/profile

+ 1 - 0
ext/rhashmap

@@ -0,0 +1 @@
+Subproject commit ed2ee48da023945f99eb1f490dcef60d720f8bac

+ 2 - 2
src/buffer.c

@@ -20,7 +20,7 @@ LSUP_Buffer *LSUP_buffer_new(size_t size)
 
 int LSUP_buffer_init(LSUP_Buffer *buf, size_t size)
 {
-    TRACE("Buffer Size: %lu\n", size);
+    //TRACE("Buffer Size: %lu\n", size);
     CRITICAL(buf->addr = malloc(size * sizeof(char)));
     buf->size = size;
 
@@ -58,7 +58,7 @@ int LSUP_buffer_copy(LSUP_Buffer *dest, const LSUP_Buffer *src)
 
 void LSUP_buffer_done(LSUP_Buffer *buf){
     if (buf->addr != NULL) {
-        TRACE(STR, "Freeing buffer.");
+        //TRACE(STR, "Freeing buffer.");
         free(buf->addr);
     }
 }

+ 21 - 19
src/term.c

@@ -1,36 +1,38 @@
-//#define PY_SSIZE_T_CLEAN
-//#include <Python.h>
-
 #include "term.h"
 
 #define CHR         sizeof(char)
 #define NLEN(str)   (str) == NULL ? 0 : strlen((str))
 
+static regex_t ptn;
+static bool ptn_init = false;
+
+
+/**
+ * Free global regex struct. Register with atexit().
+ */
+void term_cleanup() { if (ptn_init) regfree(&ptn); }
+
+
 int
 LSUP_term_init(
         LSUP_Term *term, LSUP_term_type type,
-        char *data, char *datatype, char *lang) {
-
+        char *data, char *datatype, char *lang)
+{
     term->type = type;
     if (data == NULL) return -1;
 
     if (term->type == LSUP_TERM_URI) {
-        TRACE(STR, "Checking URI term.");
-        // TODO Move this to a code block that is only executed once.
-        regex_t ptn;
-        int status = regcomp(&ptn, URI_REGEX_STR, REG_EXTENDED|REG_NOSUB);
-        assert(status == 0);
-        //TRACE(STR, "Regex compiled.");
-
-        //TRACE("Checking data: %s", data);
-        status = regexec(&ptn, data, 0, NULL, 0);
-        regfree(&ptn);
-        if (status != 0) {
+        if (UNLIKELY(!ptn_init)) {
+            assert (regcomp(&ptn, URI_REGEX_STR, REG_EXTENDED) == 0);
+            ptn_init = true;
+            atexit(term_cleanup);
+        }
+
+        if (regexec(&ptn, data, 0, NULL, 0) != 0) {
             printf("Error matching URI pattern.\n");
 
-            return(-1);
+            return -1;
         }
-        TRACE(STR, "URI checked.");
     }
 
     term->data = malloc(strlen(data) + 1);
@@ -105,7 +107,7 @@ LSUP_term_serialize(const LSUP_Term *term, LSUP_Buffer *sterm)
         }
     }
 
-    TRACE("Serialized term size: %lu", size);
+    //TRACE("Serialized term size: %lu", size);
     LSUP_buffer_init(sterm, size);
 
     // Copy type.