[gjs] Add a way to store commonly used property names
- From: Giovanni Campagna <gcampagna src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs] Add a way to store commonly used property names
- Date: Tue, 23 Apr 2013 20:54:30 +0000 (UTC)
commit 4d378a62e081fb0ac81e9b567c52b01e83a5127a
Author: Giovanni Campagna <gcampagna src gnome org>
Date: Mon Apr 8 20:01:54 2013 +0200
Add a way to store commonly used property names
Accessing JS properties by name is expensive, because it forces
the JS API to convert them to UTF-8 and atomize them. We can
avoid that by interning property names that we know we need,
and keeping the jsid at hand.
https://bugzilla.gnome.org/show_bug.cgi?id=697592
Makefile.am | 2 +
gi/closure.c | 1 +
gi/function.c | 1 +
gi/object.c | 1 +
gi/value.c | 1 +
gjs/context.c | 7 ++-
gjs/jsapi-util.c | 15 -------
gjs/jsapi-util.h | 1 -
gjs/runtime.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
gjs/runtime.h | 51 +++++++++++++++++++++++++
10 files changed, 170 insertions(+), 18 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index ac6162e..9debd81 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -38,6 +38,7 @@ nobase_gjs_module_include_HEADERS = \
gjs/byteArray.h \
gjs/importer.h \
gjs/jsapi-util.h \
+ gjs/runtime.h \
gjs/type-module.h \
gjs/mem.h \
gjs/native.h \
@@ -117,6 +118,7 @@ libgjs_la_SOURCES = \
gjs/mem.c \
gjs/native.c \
gjs/profiler.c \
+ gjs/runtime.c \
gjs/stack.c \
gjs/type-module.c \
modules/modules.c \
diff --git a/gi/closure.c b/gi/closure.c
index 349c4f5..de6d7d6 100644
--- a/gi/closure.c
+++ b/gi/closure.c
@@ -31,6 +31,7 @@
#include "keep-alive.h"
#include <gjs/gjs-module.h>
#include <gjs/compat.h>
+#include <gjs/runtime.h>
typedef struct {
GClosure base;
diff --git a/gi/function.c b/gi/function.c
index 71d61cd..cd6e628 100644
--- a/gi/function.c
+++ b/gi/function.c
@@ -29,6 +29,7 @@
#include "boxed.h"
#include "union.h"
#include "gerror.h"
+#include <gjs/runtime.h>
#include <gjs/gjs-module.h>
#include <gjs/compat.h>
diff --git a/gi/object.c b/gi/object.c
index 585a3ba..ea8bfed 100644
--- a/gi/object.c
+++ b/gi/object.c
@@ -41,6 +41,7 @@
#include <gjs/gjs-module.h>
#include <gjs/compat.h>
#include <gjs/type-module.h>
+#include <gjs/runtime.h>
#include <util/log.h>
#include <girepository.h>
diff --git a/gi/value.c b/gi/value.c
index f61ca73..9a33398 100644
--- a/gi/value.c
+++ b/gi/value.c
@@ -37,6 +37,7 @@
#include "gerror.h"
#include <gjs/gjs-module.h>
#include <gjs/compat.h>
+#include <gjs/runtime.h>
#include <girepository.h>
diff --git a/gjs/context.c b/gjs/context.c
index 4ba8b3a..b3af309 100644
--- a/gjs/context.c
+++ b/gjs/context.c
@@ -30,6 +30,7 @@
#include "native.h"
#include "byteArray.h"
#include "compat.h"
+#include "runtime.h"
#include "gi.h"
#include "gi/object.h"
@@ -397,6 +398,8 @@ gjs_context_dispose(GObject *object)
}
if (js_context->runtime != NULL) {
+ gjs_runtime_deinit(js_context->runtime);
+
/* Cleans up data as well as destroying the runtime. */
JS_DestroyRuntime(js_context->runtime);
js_context->runtime = NULL;
@@ -572,7 +575,7 @@ gjs_context_constructor (GType type,
if (js_context->context == NULL)
gjs_fatal("Failed to create javascript context");
- JS_SetRuntimePrivate(js_context->runtime, js_context->context);
+ gjs_runtime_init_for_context(js_context->runtime, js_context->context);
JS_BeginRequest(js_context->context);
@@ -900,7 +903,7 @@ static void
gjs_on_context_gc (JSRuntime *rt,
JSGCStatus status)
{
- JSContext *context = JS_GetRuntimePrivate(rt);
+ JSContext *context = gjs_runtime_get_context(rt);
GjsContext *gjs_context = JS_GetContextPrivate(context);
switch (status) {
diff --git a/gjs/jsapi-util.c b/gjs/jsapi-util.c
index 860c859..35a9000 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -69,21 +69,6 @@ gjs_get_import_global(JSContext *context)
return JS_GetGlobalObject(context);
}
-/**
- * gjs_runtime_get_context:
- * @runtime: a #JSRuntime
- *
- * Gets the context associated with this runtime.
- *
- * Return value: the context, or %NULL if GJS hasn't been initialized
- * for the runtime or is being shut down.
- */
-JSContext *
-gjs_runtime_get_context(JSRuntime *runtime)
-{
- return (JSContext *) JS_GetRuntimePrivate (runtime);
-}
-
static void
finalize_stub(JSFreeOp *fop, JSObject *global)
{
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index aefd2bf..840958e 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -179,7 +179,6 @@ jsval gjs_##cname##_create_proto(JSContext *context, JSObject *module, const cha
gboolean gjs_init_context_standard (JSContext *context);
-JSContext* gjs_runtime_get_context (JSRuntime *runtime);
JSObject* gjs_get_import_global (JSContext *context);
gboolean gjs_object_has_property (JSContext *context,
JSObject *obj,
diff --git a/gjs/runtime.c b/gjs/runtime.c
new file mode 100644
index 0000000..ed69b49
--- /dev/null
+++ b/gjs/runtime.c
@@ -0,0 +1,108 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2013 Giovanni Campagna <scampa giovanni gmail com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <config.h>
+
+#include <util/log.h>
+#include <util/glib.h>
+#include <util/misc.h>
+
+#include "jsapi-util.h"
+#include "compat.h"
+#include "jsapi-private.h"
+#include "runtime.h"
+
+#include <string.h>
+#include <math.h>
+
+typedef struct {
+ JSContext *context;
+ jsid const_strings[GJS_STRING_LAST];
+} GjsRuntimeData;
+
+/* Keep this consistent with GjsConstString */
+static const char *const_strings[] = {
+ "constructor", "prototype", "length",
+ "imports", "__parentModule__", "__init__", "searchPath",
+ "__gjsKeepAlive",
+ "gi", "versions", "overrides",
+ "_init"
+};
+
+G_STATIC_ASSERT(G_N_ELEMENTS(const_strings) == GJS_STRING_LAST);
+
+static inline GjsRuntimeData *
+get_data(JSRuntime *runtime)
+{
+ return (GjsRuntimeData*) JS_GetRuntimePrivate(runtime);
+}
+
+/**
+ * gjs_runtime_get_context:
+ * @runtime: a #JSRuntime
+ *
+ * Gets the context associated with this runtime.
+ *
+ * Return value: the context, or %NULL if GJS hasn't been initialized
+ * for the runtime or is being shut down.
+ */
+JSContext *
+gjs_runtime_get_context(JSRuntime *runtime)
+{
+ return get_data(runtime)->context;
+}
+
+jsid
+gjs_runtime_get_const_string(JSRuntime *runtime,
+ GjsConstString name)
+{
+ /* Do not add prerequisite checks here, this is a very hot call! */
+
+ return get_data(runtime)->const_strings[name];
+}
+
+void
+gjs_runtime_init_for_context(JSRuntime *runtime,
+ JSContext *context)
+{
+ GjsRuntimeData *data;
+ int i;
+
+ data = g_new(GjsRuntimeData, 1);
+
+ data->context = context;
+ for (i = 0; i < GJS_STRING_LAST; i++) {
+ JSString *str;
+
+ str = JS_InternString(context, const_strings[i]);
+ data->const_strings[i] = INTERNED_STRING_TO_JSID(context, str);
+ }
+
+ JS_SetRuntimePrivate(runtime, data);
+}
+
+void
+gjs_runtime_deinit(JSRuntime *runtime)
+{
+ g_free(get_data(runtime));
+}
diff --git a/gjs/runtime.h b/gjs/runtime.h
new file mode 100644
index 0000000..59ae413
--- /dev/null
+++ b/gjs/runtime.h
@@ -0,0 +1,51 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2013 Giovanni Campagna <scampa giovanni gmail com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef __GJS_RUNTIME_H__
+#define __GJS_RUNTIME_H__
+
+typedef enum {
+ GJS_STRING_CONSTRUCTOR,
+ GJS_STRING_PROTOTYPE,
+ GJS_STRING_LENGTH,
+ GJS_STRING_IMPORTS,
+ GJS_STRING_PARENT_MODULE,
+ GJS_STRING_MODULE_INIT,
+ GJS_STRING_SEARCH_PATH,
+ GJS_STRING_KEEP_ALIVE_MARKER,
+ GJS_STRING_GI_MODULE,
+ GJS_STRING_GI_VERSIONS,
+ GJS_STRING_GI_OVERRIDES,
+ GJS_STRING_GOBJECT_INIT,
+ GJS_STRING_LAST
+} GjsConstString;
+
+void gjs_runtime_init_for_context (JSRuntime *runtime,
+ JSContext *context);
+void gjs_runtime_deinit (JSRuntime *runtime);
+
+JSContext* gjs_runtime_get_context (JSRuntime *runtime);
+jsid gjs_runtime_get_const_string (JSRuntime *runtime,
+ GjsConstString string);
+
+#endif /* __GJS_RUNTIME_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]