diff options
author | David Robillard <d@drobilla.net> | 2018-10-28 14:15:28 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2019-04-13 19:15:32 +0200 |
commit | d8dcff59b820771d3607eab651a5b6f2ce07eef4 (patch) | |
tree | d8f5acc56e49b28ae8a5773d5f6e38f00a41aad4 | |
parent | 05fbb2be9950175a517fda17e0d65135b4eaeab6 (diff) | |
download | serd-d8dcff59b820771d3607eab651a5b6f2ce07eef4.tar.gz serd-d8dcff59b820771d3607eab651a5b6f2ce07eef4.tar.bz2 serd-d8dcff59b820771d3607eab651a5b6f2ce07eef4.zip |
Cache commonly used nodes in the world
-rw-r--r-- | src/world.c | 12 | ||||
-rw-r--r-- | src/world.h | 16 | ||||
-rw-r--r-- | src/writer.c | 18 |
3 files changed, 32 insertions, 14 deletions
diff --git a/src/world.c b/src/world.c index d43a2909..21bce2f3 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" #include <errno.h> #include <stdarg.h> @@ -83,8 +84,18 @@ SerdWorld* serd_world_new(void) { SerdWorld* world = (SerdWorld*)calloc(1, sizeof(SerdWorld)); + SerdNodes* nodes = serd_nodes_new(); + + world->rdf_first = serd_nodes_manage(nodes, serd_new_uri(NS_RDF "first")); + world->rdf_nil = serd_nodes_manage(nodes, serd_new_uri(NS_RDF "nil")); + world->rdf_rest = serd_nodes_manage(nodes, serd_new_uri(NS_RDF "rest")); + world->rdf_type = serd_nodes_manage(nodes, serd_new_uri(NS_RDF "type")); + world->xsd_boolean = serd_nodes_manage(nodes, serd_new_uri(NS_XSD "boolean")); + world->xsd_decimal = serd_nodes_manage(nodes, serd_new_uri(NS_XSD "decimal")); + world->xsd_integer = serd_nodes_manage(nodes, serd_new_uri(NS_XSD "integer")); world->blank_node = serd_new_blank("b0000000000"); + world->nodes = nodes; return world; } @@ -93,6 +104,7 @@ void serd_world_free(SerdWorld* world) { serd_node_free(world->blank_node); + serd_nodes_free(world->nodes); free(world); } diff --git a/src/world.h b/src/world.h index 201d038f..9ac13a95 100644 --- a/src/world.h +++ b/src/world.h @@ -23,10 +23,18 @@ #include <stdio.h> struct SerdWorldImpl { - SerdErrorSink error_sink; - void* error_handle; - uint32_t next_blank_id; - SerdNode* blank_node; + SerdNodes* nodes; + SerdErrorSink error_sink; + void* error_handle; + uint32_t next_blank_id; + 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; }; FILE* serd_world_fopen(SerdWorld* world, const char* path, const char* mode); diff --git a/src/writer.c b/src/writer.c index 48167cc7..13f439fd 100644 --- a/src/writer.c +++ b/src/writer.c @@ -16,7 +16,6 @@ #include "env.h" #include "node.h" -#include "serd_internal.h" #include "sink.h" #include "stack.h" #include "string_utils.h" @@ -441,13 +440,11 @@ write_literal(SerdWriter* writer, const char* node_str = serd_node_get_string(node); const char* type_uri = serd_node_get_string(datatype); 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; - } else if (!strncmp(type_uri, NS_XSD, sizeof(NS_XSD) - 1) && - !strcmp(type_uri + sizeof(NS_XSD) - 1, "decimal") && + } else 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 @@ -510,9 +507,10 @@ write_uri_node(SerdWriter* const writer, const char* node_str = serd_node_get_string(node); const bool has_scheme = serd_uri_string_has_scheme(node_str); if (field == SERD_PREDICATE && supports_abbrev(writer) - && !strcmp(node_str, NS_RDF "type")) { + && serd_node_equals(node, writer->world->rdf_type)) { return sink("a", 1, writer) == 1; - } else if (supports_abbrev(writer) && !strcmp(node_str, NS_RDF "nil")) { + } else if (supports_abbrev(writer) && + serd_node_equals(node, writer->world->rdf_nil)) { return sink("()", 2, writer) == 2; } else if (has_scheme && supports_abbrev(writer) && serd_env_qualify_in_place(writer->env, node, &prefix, &suffix) && @@ -678,11 +676,11 @@ write_list_obj(SerdWriter* writer, const SerdNode* predicate, const SerdNode* object) { - if (!strcmp(serd_node_get_string(object), NS_RDF "nil")) { + if (serd_node_equals(object, writer->world->rdf_nil)) { --writer->indent; write_sep(writer, SEP_LIST_END); return true; - } else if (!strcmp(serd_node_get_string(predicate), NS_RDF "first")) { + } else if (serd_node_equals(predicate, writer->world->rdf_first)) { write_sep(writer, SEP_LIST_SEP); write_node(writer, object, SERD_OBJECT, flags); } |