[ovs-dev] [non-NORMAL learning 2/3] tag: Be more precise about choosing tags to add, in tag_set_add().

Ben Pfaff blp at nicira.com
Wed Aug 4 17:27:36 UTC 2010


There is no need to add "tag" values with only a single bit set, but until
now tag_set_add() would do so anyhow.  (That is not a bug, it is only a
possible performance loss.)  It is likely that no existing caller ever
tries to do this, but an upcoming commit will start creating 1-bit tags
and thus introduce the possibility, so it seems like a good idea to avoid
the problem before it starts.

It is also not necessary to add a "tag" if all of the bits in it are
already present in some member of the set, so add that optimization too.
---
 lib/tag.c |   28 +++++++++++++++++++++++++++-
 1 files changed, 27 insertions(+), 1 deletions(-)

diff --git a/lib/tag.c b/lib/tag.c
index 0430224..68f756a 100644
--- a/lib/tag.c
+++ b/lib/tag.c
@@ -61,11 +61,37 @@ tag_set_init(struct tag_set *set)
     memset(set, 0, sizeof *set);
 }
 
+static bool
+tag_is_worth_adding(const struct tag_set *set, tag_type tag)
+{
+    if (!(tag & (tag - 1))) {
+        /* Adding an empty tag or a single 1-bit would just add
+         * false-positives, so drop it. */
+        return false;
+    } else if ((set->total & tag) != tag) {
+        /* 'set' doesn't have all the bits in 'tag', so we need to add it. */
+        return true;
+    } else {
+        /* We can drop it if some member of 'set' already includes all of the
+         * 1-bits in 'tag'.  (tag_set_intersects() does a different test:
+         * whether some member of 'set' has at least two 1-bit in common with
+         * 'tag'.) */
+        int i;
+
+        for (i = 0; i < TAG_SET_SIZE; i++) {
+            if ((set->tags[i] & tag) == tag) {
+                return false;
+            }
+        }
+        return true;
+    }
+}
+
 /* Adds 'tag' to 'set'. */
 void
 tag_set_add(struct tag_set *set, tag_type tag)
 {
-    if (tag && (!tag_is_valid(tag) || !tag_set_intersects(set, tag))) {
+    if (tag_is_worth_adding(set, tag)) {
         /* XXX We could do better by finding the set member to which we would
          * add the fewest number of 1-bits.  This would reduce the amount of
          * ambiguity, since e.g. three 1-bits match 3 * 2 / 2 = 3 unique tags
-- 
1.7.1





More information about the dev mailing list