]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
i18n: cache CTX
authorTimePath <andrew.hardaker1995@gmail.com>
Sat, 5 Dec 2015 02:28:43 +0000 (13:28 +1100)
committerTimePath <andrew.hardaker1995@gmail.com>
Sat, 5 Dec 2015 02:29:59 +0000 (13:29 +1100)
qcsrc/lib/_all.inc
qcsrc/lib/i18n.qh
qcsrc/lib/unsafe.qh [new file with mode: 0644]

index 9f7004c345ae8ca98c05a83434112b17fab2360e..a63b43e22a58c879e8c378e91a037bd7b13b0bf4 100644 (file)
@@ -67,5 +67,6 @@
 #include "string.qh"
 #include "struct.qh"
 #include "test.qc"
+#include "unsafe.qh"
 #include "urllib.qc"
 #include "vector.qh"
index 3773e16d1d2fdde2c904f6ac37812aab2d5a08a2..137e78204ac60416629468180861c51ea72e1ba2 100644 (file)
@@ -2,6 +2,7 @@
 #define I18N_H
 
 #include "log.qh"
+#include "unsafe.qh"
 
 // translation helpers
 string prvm_language;
@@ -20,11 +21,36 @@ string language_filename(string s)
        return s;
 }
 
+#ifndef CTX_CACHE
+       #define CTX_CACHE 1
+#endif
+
+#if CTX_CACHE
+       AL_declare(CTX_cache);
+       STATIC_INIT(CTX_cache)
+       {
+               AL_init(CTX_cache, 0, string_null, s);
+       }
+       SHUTDOWN(CTX_cache)
+       {
+               AL_delete(CTX_cache);
+       }
+#endif
+
 string CTX(string s)
 {
+#if CTX_CACHE
+               int i = strid(s);
+               string c = AL_gets(CTX_cache, i);
+               if (c) return c;
+#endif
        int p = strstrofs(s, "^", 0);
-       if (p < 0) return s;
-       return substring(s, p + 1, -1);
+       string ret = (p < 0) ? s : substring(s, p + 1, -1);
+#if CTX_CACHE
+        LOG_DEBUGF("CTX(\"%s\")\n", s);
+               AL_sets(CTX_cache, i, ret);
+#endif
+       return ret;
 }
 
 #define ZCTX(s) strzone(CTX(s))
diff --git a/qcsrc/lib/unsafe.qh b/qcsrc/lib/unsafe.qh
new file mode 100644 (file)
index 0000000..f8fa382
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef UNSAFE_H
+#define UNSAFE_H
+
+#define reinterpret_cast(T, it) _unsafe_cast_##T(0, it)
+#define X(T) T _unsafe_cast_##T(int dummy, ...) { return ...(0, T); }
+X(float)
+X(entity)
+X(string)
+typedef float(...) rawfunc;
+X(rawfunc)
+#undef X
+
+#define strid(s) etof(reinterpret_cast(entity, s))
+
+#endif