2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-25 15:07:05 +00:00

ovsdb-parser: Use sset instead of svec for detecting unused members.

Should be slightly cheaper than sorting a list (O(n) vs. O(n lg n)).
This commit is contained in:
Ben Pfaff
2011-03-25 13:20:35 -07:00
parent 19993ef3ca
commit 53d046612d
2 changed files with 9 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2009 Nicira Networks /* Copyright (c) 2009, 2011 Nicira Networks
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@ ovsdb_parser_init(struct ovsdb_parser *parser, const struct json *json,
parser->name = xvasprintf(name, args); parser->name = xvasprintf(name, args);
va_end(args); va_end(args);
svec_init(&parser->used); sset_init(&parser->used);
parser->error = NULL; parser->error = NULL;
parser->json = (json && json->type == JSON_OBJECT ? json : NULL); parser->json = (json && json->type == JSON_OBJECT ? json : NULL);
@@ -85,7 +85,7 @@ ovsdb_parser_member(struct ovsdb_parser *parser, const char *name,
|| (types & OP_ID && value->type == JSON_STRING || (types & OP_ID && value->type == JSON_STRING
&& ovsdb_parser_is_id(value->u.string))) && ovsdb_parser_is_id(value->u.string)))
{ {
svec_add(&parser->used, name); sset_add(&parser->used, name);
return value; return value;
} else { } else {
ovsdb_parser_raise_error(parser, "Type mismatch for member '%s'.", ovsdb_parser_raise_error(parser, "Type mismatch for member '%s'.",
@@ -133,14 +133,12 @@ ovsdb_parser_finish(struct ovsdb_parser *parser)
const struct shash *object = json_object(parser->json); const struct shash *object = json_object(parser->json);
size_t n_unused; size_t n_unused;
/* XXX this method of detecting unused members can be made cheaper */ n_unused = shash_count(object) - sset_count(&parser->used);
svec_sort_unique(&parser->used);
n_unused = shash_count(object) - parser->used.n;
if (n_unused) { if (n_unused) {
struct shash_node *node; struct shash_node *node;
SHASH_FOR_EACH (node, object) { SHASH_FOR_EACH (node, object) {
if (!svec_contains(&parser->used, node->name)) { if (!sset_contains(&parser->used, node->name)) {
if (n_unused > 1) { if (n_unused > 1) {
ovsdb_parser_raise_error( ovsdb_parser_raise_error(
parser, parser,
@@ -160,7 +158,7 @@ ovsdb_parser_finish(struct ovsdb_parser *parser)
} }
free(parser->name); free(parser->name);
svec_destroy(&parser->used); sset_destroy(&parser->used);
return parser->error; return parser->error;
} }

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2009, 2010 Nicira Networks /* Copyright (c) 2009, 2010, 2011 Nicira Networks
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -19,12 +19,12 @@
#include <stdbool.h> #include <stdbool.h>
#include "compiler.h" #include "compiler.h"
#include "json.h" #include "json.h"
#include "svec.h" #include "sset.h"
#include "util.h" #include "util.h"
struct ovsdb_parser { struct ovsdb_parser {
char *name; /* Used only in error messages. */ char *name; /* Used only in error messages. */
struct svec used; /* Already-parsed names from 'object'. */ struct sset used; /* Already-parsed names from 'object'. */
const struct json *json; /* JSON object being parsed. */ const struct json *json; /* JSON object being parsed. */
struct ovsdb_error *error; /* Error signaled, if any. */ struct ovsdb_error *error; /* Error signaled, if any. */
}; };