aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-10-28 14:15:28 +0100
committerDavid Robillard <d@drobilla.net>2021-03-08 23:23:05 -0500
commit6aee38ce233b319c4ee3ac1f16f4c2ca621cb4b0 (patch)
tree566321615dbbdc73554339c73da67a4bc0c02638
parent800f2d0d4960061a1739a36d8b608a13896b321e (diff)
downloadserd-6aee38ce233b319c4ee3ac1f16f4c2ca621cb4b0.tar.gz
serd-6aee38ce233b319c4ee3ac1f16f4c2ca621cb4b0.tar.bz2
serd-6aee38ce233b319c4ee3ac1f16f4c2ca621cb4b0.zip
Cache commonly used nodes in the world
-rw-r--r--include/serd/serd.h11
-rw-r--r--src/world.c29
-rw-r--r--src/world.h16
-rw-r--r--src/writer.c17
4 files changed, 58 insertions, 15 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h
index 7c1ff057..7b279517 100644
--- a/include/serd/serd.h
+++ b/include/serd/serd.h
@@ -956,6 +956,17 @@ void
serd_world_free(SerdWorld* SERD_NULLABLE world);
/**
+ Return the nodes cache in `world`.
+
+ The returned cache is owned by the world and contains various nodes used
+ frequently by the implementation. For convenience, it may be used to store
+ additional nodes which will be freed when the world is freed.
+*/
+SERD_PURE_API
+SerdNodes* SERD_NONNULL
+serd_world_nodes(SerdWorld* SERD_NONNULL world);
+
+/**
Return a unique blank node.
The returned node is valid only until the next time serd_world_get_blank()
diff --git a/src/world.c b/src/world.c
index 1b66f025..354331f6 100644
--- a/src/world.c
+++ b/src/world.c
@@ -21,6 +21,7 @@
#include "cursor.h"
#include "node.h"
#include "serd_config.h"
+#include "serd_internal.h"
#if defined(USE_POSIX_FADVISE)
# include <fcntl.h>
@@ -86,8 +87,25 @@ SerdWorld*
serd_world_new(void)
{
SerdWorld* world = (SerdWorld*)calloc(1, sizeof(SerdWorld));
-
- world->blank_node = serd_new_blank(SERD_STATIC_STRING("b00000000000"));
+ SerdNodes* nodes = serd_nodes_new();
+
+ const SerdStringView rdf_first = SERD_STATIC_STRING(NS_RDF "first");
+ const SerdStringView rdf_nil = SERD_STATIC_STRING(NS_RDF "nil");
+ const SerdStringView rdf_rest = SERD_STATIC_STRING(NS_RDF "rest");
+ const SerdStringView rdf_type = SERD_STATIC_STRING(NS_RDF "type");
+ const SerdStringView xsd_boolean = SERD_STATIC_STRING(NS_XSD "boolean");
+ const SerdStringView xsd_decimal = SERD_STATIC_STRING(NS_XSD "decimal");
+ const SerdStringView xsd_integer = SERD_STATIC_STRING(NS_XSD "integer");
+
+ world->rdf_first = serd_nodes_manage(nodes, serd_new_uri(rdf_first));
+ world->rdf_nil = serd_nodes_manage(nodes, serd_new_uri(rdf_nil));
+ world->rdf_rest = serd_nodes_manage(nodes, serd_new_uri(rdf_rest));
+ world->rdf_type = serd_nodes_manage(nodes, serd_new_uri(rdf_type));
+ world->xsd_boolean = serd_nodes_manage(nodes, serd_new_uri(xsd_boolean));
+ world->xsd_decimal = serd_nodes_manage(nodes, serd_new_uri(xsd_decimal));
+ world->xsd_integer = serd_nodes_manage(nodes, serd_new_uri(xsd_integer));
+ world->blank_node = serd_new_blank(SERD_STATIC_STRING("b00000000000"));
+ world->nodes = nodes;
return world;
}
@@ -97,10 +115,17 @@ serd_world_free(SerdWorld* world)
{
if (world) {
serd_node_free(world->blank_node);
+ serd_nodes_free(world->nodes);
free(world);
}
}
+SerdNodes*
+serd_world_nodes(SerdWorld* world)
+{
+ return world->nodes;
+}
+
const SerdNode*
serd_world_get_blank(SerdWorld* world)
{
diff --git a/src/world.h b/src/world.h
index 99f0c92d..44e21166 100644
--- a/src/world.h
+++ b/src/world.h
@@ -23,10 +23,18 @@
#include <stdio.h>
struct SerdWorldImpl {
- SerdErrorFunc error_func;
- void* error_handle;
- uint32_t next_blank_id;
- SerdNode* blank_node;
+ SerdNodes* nodes;
+ SerdErrorFunc error_func;
+ void* error_handle;
+ SerdNode* blank_node;
+ const SerdNode* rdf_first;
+ const SerdNode* rdf_nil;
+ const SerdNode* rdf_rest;
+ const SerdNode* rdf_type;
+ const SerdNode* xsd_boolean;
+ const SerdNode* xsd_decimal;
+ const SerdNode* xsd_integer;
+ uint32_t next_blank_id;
};
/// Open a file configured for fast sequential reading
diff --git a/src/writer.c b/src/writer.c
index 0fabb7af..4f4a76df 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -502,15 +502,13 @@ write_literal(SerdWriter* writer,
const char* node_str = serd_node_string(node);
const char* type_uri = datatype ? serd_node_string(datatype) : NULL;
if (supports_abbrev(writer) && type_uri) {
- if (!strncmp(type_uri, NS_XSD, sizeof(NS_XSD) - 1) &&
- (!strcmp(type_uri + sizeof(NS_XSD) - 1, "boolean") ||
- !strcmp(type_uri + sizeof(NS_XSD) - 1, "integer"))) {
+ if (serd_node_equals(datatype, writer->world->xsd_boolean) ||
+ serd_node_equals(datatype, writer->world->xsd_integer)) {
sink(node_str, node->n_bytes, writer);
return true;
}
- if (!strncmp(type_uri, NS_XSD, sizeof(NS_XSD) - 1) &&
- !strcmp(type_uri + sizeof(NS_XSD) - 1, "decimal") &&
+ if (serd_node_equals(datatype, writer->world->xsd_decimal) &&
strchr(node_str, '.') && node_str[node->n_bytes - 1] != '.') {
/* xsd:decimal literals without trailing digits, e.g. "5.", can
not be written bare in Turtle. We could add a 0 which is
@@ -573,11 +571,12 @@ write_uri_node(SerdWriter* const writer,
const char* node_str = serd_node_string(node);
const bool has_scheme = serd_uri_string_has_scheme(node_str);
if (supports_abbrev(writer)) {
- if (field == SERD_PREDICATE && !strcmp(node_str, NS_RDF "type")) {
+ if (field == SERD_PREDICATE &&
+ serd_node_equals(node, writer->world->rdf_type)) {
return sink("a", 1, writer) == 1;
}
- if (!strcmp(node_str, NS_RDF "nil")) {
+ if (serd_node_equals(node, writer->world->rdf_type)) {
return sink("()", 2, writer) == 2;
}
@@ -760,13 +759,13 @@ write_list_obj(SerdWriter* writer,
const SerdNode* predicate,
const SerdNode* object)
{
- if (!strcmp(serd_node_string(object), NS_RDF "nil")) {
+ if (serd_node_equals(object, writer->world->rdf_nil)) {
--writer->indent;
write_sep(writer, SEP_LIST_END);
return true;
}
- if (!strcmp(serd_node_string(predicate), NS_RDF "first")) {
+ if (serd_node_equals(predicate, writer->world->rdf_first)) {
write_sep(writer, SEP_LIST_SEP);
write_node(writer, object, SERD_OBJECT, flags);
}