Show a patch.

Update a patch.

Update a patch.

GET /api/patches/1529026/
Content-Type: application/json
Vary: Accept

    "id": 1529026,
    "url": "",
    "web_url": "",
    "project": {
        "id": 47,
        "url": "",
        "name": "Open vSwitch",
        "link_name": "openvswitch",
        "list_id": "",
        "list_email": "",
        "web_url": "",
        "scm_url": "",
        "webscm_url": "",
        "list_archive_url": "",
        "list_archive_url_format": "",
        "commit_url_format": ""
    "msgid": "<>",
    "list_archive_url": null,
    "date": "2021-09-16T20:19:07",
    "name": "[ovs-dev] ovsdb-data: Optimize union of sets.",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "2ff6b99d1216a3f96af63f13cef71fb370e700d1",
    "submitter": {
        "id": 76798,
        "url": "",
        "name": "Ilya Maximets",
        "email": ""
    "delegate": null,
    "mbox": "",
    "series": [
            "id": 262694,
            "url": "",
            "web_url": "",
            "date": "2021-09-16T20:19:07",
            "name": "[ovs-dev] ovsdb-data: Optimize union of sets.",
            "version": 1,
            "mbox": ""
    "comments": "",
    "check": "success",
    "checks": "",
    "tags": {},
    "related": [],
    "headers": {
        "Return-Path": "<>",
        "X-Original-To": [
        "Delivered-To": [
        "Authentication-Results": ";\n spf=pass (sender SPF authorized)\n (client-ip=;;\n; receiver=<UNKNOWN>)",
        "Received": [
            "from ( [])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest\n SHA256)\n\t(No client certificate requested)\n\tby (Postfix) with ESMTPS id 4H9T2H1bF5z9sCD\n\tfor <>; Fri, 17 Sep 2021 06:19:18 +1000 (AEST)",
            "from localhost (localhost [])\n\tby (Postfix) with ESMTP id 3830480EE6;\n\tThu, 16 Sep 2021 20:19:17 +0000 (UTC)",
            "from ([])\n\tby localhost ( []) (amavisd-new, port 10024)\n\twith ESMTP id W61xvNnWn2Cm; Thu, 16 Sep 2021 20:19:16 +0000 (UTC)",
            "from ( [])\n\tby (Postfix) with ESMTPS id 2369F80E9D;\n\tThu, 16 Sep 2021 20:19:15 +0000 (UTC)",
            "from (localhost [])\n\tby (Postfix) with ESMTP id 075D9C000F;\n\tThu, 16 Sep 2021 20:19:15 +0000 (UTC)",
            "from ( [IPv6:2605:bc80:3010::138])\n by (Postfix) with ESMTP id 01EF5C000D\n for <>; Thu, 16 Sep 2021 20:19:14 +0000 (UTC)",
            "from localhost (localhost [])\n by (Postfix) with ESMTP id E2E8880E90\n for <>; Thu, 16 Sep 2021 20:19:13 +0000 (UTC)",
            "from ([])\n by localhost ( []) (amavisd-new, port 10024)\n with ESMTP id 9idNvHBdx9v1 for <>;\n Thu, 16 Sep 2021 20:19:13 +0000 (UTC)",
            "from (\n [])\n by (Postfix) with ESMTPS id 80B1480E2D\n for <>; Thu, 16 Sep 2021 20:19:12 +0000 (UTC)",
            "(Authenticated sender:\n by (Postfix) with ESMTPSA id 55D02200002;\n Thu, 16 Sep 2021 20:19:10 +0000 (UTC)"
        "X-Virus-Scanned": [
            "amavisd-new at",
            "amavisd-new at"
        "X-Greylist": "domain auto-whitelisted by SQLgrey-1.8.0",
        "From": "Ilya Maximets <>",
        "To": "",
        "Date": "Thu, 16 Sep 2021 22:19:07 +0200",
        "Message-Id": "<>",
        "X-Mailer": "git-send-email 2.31.1",
        "MIME-Version": "1.0",
        "Cc": "Ilya Maximets <>, Dumitru Ceara <>",
        "Subject": "[ovs-dev] [PATCH] ovsdb-data: Optimize union of sets.",
        "X-BeenThere": "",
        "X-Mailman-Version": "2.1.15",
        "Precedence": "list",
        "List-Id": "<>",
        "List-Unsubscribe": "<>,\n <>",
        "List-Archive": "<>",
        "List-Post": "<>",
        "List-Help": "<>",
        "List-Subscribe": "<>,\n <>",
        "Content-Type": "text/plain; charset=\"us-ascii\"",
        "Content-Transfer-Encoding": "7bit",
        "Errors-To": "",
        "Sender": "\"dev\" <>"
    "content": "Current algorithm of ovsdb_datum_union looks like this:\n\n  for-each atom in b:\n      if not bin_search(a, atom):\n          a[n++] = clone(atom)\n  quicksort(a)\n\nSo, the complexity looks like this:\n\n   Nb * log2(Na)   +    Nb     +   (Na + Nb) * log2(Na + Nb)\n   Comparisons        clones       Comparisons for quicksort\n   for search\n\novsdb_datum_union() is heavily used in database transactions while\nnew element is added to a set.  For example, if new logical switch\nport is added to a logical switch in OVN.  This is a very common\nuse case where CMS adds one new port to an existing switch that\nalready has, let's say, 100 ports.  For this case ovsdb-server will\nhave to perform:\n\n   1 * log2(100)  + 1 clone + 101 * log2(101)\n   Comparisons                Comparisons for\n   for search                   quicksort.\n       ~7           1            ~707\n   Roughly 714 comparisons of atoms and 1 clone.\n\nSince binary search can give us position, where new atom should go\n(it's the 'low' index after the search completion) for free, the\nlogic can be re-worked like this:\n\n  copied = 0\n  for-each atom in b:\n      desired_position = bin_search(a, atom)\n      push(result, a[ copied : desired_position - 1 ])\n      copied = desired_position\n      result[n++] = clone(atom)\n  swap(a, result)\n\nComplexity of this schema:\n\n   Nb * log2(Na)   +    Nb     +         Na\n   Comparisons        clones       memory copy on push\n   for search\n\n'swap' is just a swap of a few pointers.  'push' is not a 'clone',\nbut a simple memory copy of 'union ovsdb_atom'.\n\nIn general, this schema substitutes complexity of a quicksort\nwith complexity of a memory copy of Na atom structures, where we're\nnot even copying strings that these atoms are pointing to.\n\nComplexity in the example above goes down from 714 comparisons\nto 7 comparisons and memcpy of 100 * sizeof (union ovsdb_atom) bytes.\n\nGeneral complexity of a memory copy should always be lower than\ncomplexity of a quicksort, especially because these copies usually\nperformed in bulk, so this new schema should work faster for any input.\n\nAll in all, this change allows to execute several times more\ntransactions per second for transactions that adds new entries to sets.\n\nAlternatively, union can be implemented as a linear merge of two\nsorted arrays, but this will result in O(Na) comparisons, which\nis more than Nb * log2(Na) in common case, since Na is usually\nfar bigger than Nb.  Linear merge will also mean per-atom memory\ncopies instead of copying in bulk.\n\n'replace' functionality of ovsdb_datum_union() had no users, so it\njust removed.  But it can easily be added back if needed in the future.\n\nSigned-off-by: Ilya Maximets <>\n---\n lib/db-ctl-base.c | 10 +++---\n lib/ovsdb-data.c  | 84 ++++++++++++++++++++++++++++++++---------------\n lib/ovsdb-data.h  |  6 ++--\n lib/ovsdb-idl.c   |  8 ++---\n ovsdb/mutation.c  |  2 +-\n vswitchd/bridge.c |  9 +++--\n 6 files changed, 77 insertions(+), 42 deletions(-)",
    "diff": "diff --git a/lib/db-ctl-base.c b/lib/db-ctl-base.c\nindex 77cc76a9f..f69868702 100644\n--- a/lib/db-ctl-base.c\n+++ b/lib/db-ctl-base.c\n@@ -321,7 +321,7 @@ get_row_by_id(struct ctl_context *ctx,\n             const union ovsdb_atom key_atom\n                 = { .string = CONST_CAST(char *, id->key) };\n             unsigned int i = ovsdb_datum_find_key(datum, &key_atom,\n-                                                  OVSDB_TYPE_STRING);\n+                                                  OVSDB_TYPE_STRING, NULL);\n             name = i == UINT_MAX ? NULL : &datum->values[i];\n         }\n         if (!name) {\n@@ -820,7 +820,7 @@ check_condition(const struct ovsdb_idl_table_class *table,\n         }\n \n         idx = ovsdb_datum_find_key(have_datum,\n-                                   &want_key, column->type.key.type);\n+                                   &want_key, column->type.key.type, NULL);\n         if (idx == UINT_MAX && !is_set_operator(operator)) {\n             retval = false;\n         } else {\n@@ -993,7 +993,7 @@ cmd_get(struct ctl_context *ctx)\n             }\n \n             idx = ovsdb_datum_find_key(datum, &key,\n-                                       column->type.key.type);\n+                                       column->type.key.type, NULL);\n             if (idx == UINT_MAX) {\n                 if (must_exist) {\n                     ctl_error(\n@@ -1375,7 +1375,7 @@ set_column(const struct ovsdb_idl_table_class *table,\n         ovsdb_atom_destroy(&value, column->type.value.type);\n \n         ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),\n-                          &column->type, false);\n+                          &column->type);\n         ovsdb_idl_txn_verify(row, column);\n         ovsdb_idl_txn_write(row, column, &datum);\n     } else {\n@@ -1514,7 +1514,7 @@ cmd_add(struct ctl_context *ctx)\n             ovsdb_datum_destroy(&old, &column->type);\n             return;\n         }\n-        ovsdb_datum_union(&old, &add, type, false);\n+        ovsdb_datum_union(&old, &add, type);\n         ovsdb_datum_destroy(&add, type);\n     }\n     if (old.n > type->n_max) {\ndiff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c\nindex c145f5ad9..11bf95fed 100644\n--- a/lib/ovsdb-data.c\n+++ b/lib/ovsdb-data.c\n@@ -799,7 +799,8 @@ ovsdb_atom_check_constraints(const union ovsdb_atom *atom,\n                              const struct ovsdb_base_type *base)\n {\n     if (base->enum_\n-        && ovsdb_datum_find_key(base->enum_, atom, base->type) == UINT_MAX) {\n+        && ovsdb_datum_find_key(base->enum_, atom,\n+                                base->type, NULL) == UINT_MAX) {\n         struct ovsdb_error *error;\n         struct ds actual = DS_EMPTY_INITIALIZER;\n         struct ds valid = DS_EMPTY_INITIALIZER;\n@@ -1787,11 +1788,13 @@ ovsdb_datum_compare_3way(const struct ovsdb_datum *a,\n /* If 'key' is one of the keys in 'datum', returns its index within 'datum',\n  * otherwise UINT_MAX.  'key.type' must be the type of the atoms stored in the\n  * 'keys' array in 'datum'.\n+ * If 'key' is not found, index where it should have been returned in '*pos'.\n  */\n unsigned int\n ovsdb_datum_find_key(const struct ovsdb_datum *datum,\n                      const union ovsdb_atom *key,\n-                     enum ovsdb_atomic_type key_type)\n+                     enum ovsdb_atomic_type key_type,\n+                     unsigned int *pos)\n {\n     unsigned int low = 0;\n     unsigned int high = datum->n;\n@@ -1806,6 +1809,9 @@ ovsdb_datum_find_key(const struct ovsdb_datum *datum,\n             return idx;\n         }\n     }\n+    if (pos) {\n+        *pos = low;\n+    }\n     return UINT_MAX;\n }\n \n@@ -1821,7 +1827,7 @@ ovsdb_datum_find_key_value(const struct ovsdb_datum *datum,\n                            const union ovsdb_atom *value,\n                            enum ovsdb_atomic_type value_type)\n {\n-    unsigned int idx = ovsdb_datum_find_key(datum, key, key_type);\n+    unsigned int idx = ovsdb_datum_find_key(datum, key, key_type, NULL);\n     if (idx != UINT_MAX\n         && value_type != OVSDB_TYPE_VOID\n         && !ovsdb_atom_equals(&datum->values[idx], value, value_type)) {\n@@ -1948,38 +1954,64 @@ ovsdb_datum_add_unsafe(struct ovsdb_datum *datum,\n     }\n }\n \n+/* Adds 'n' atoms starting from index 'start_idx' from 'src' to the end of\n+ * 'dst'.  'dst' should have enough memory allocated to hold the additional\n+ * 'n' atoms.  Atoms are not cloned, i.e. 'dst' will reference the same data.\n+ * Caller also should take care of the result being sorted. */\n+static void\n+ovsdb_datum_push_unsafe(struct ovsdb_datum *dst,\n+                        const struct ovsdb_datum *src,\n+                        unsigned int start_idx, unsigned int n,\n+                        const struct ovsdb_type *type)\n+{\n+    memcpy(&dst->keys[dst->n], &src->keys[start_idx], n * sizeof src->keys[0]);\n+    if (type->value.type != OVSDB_TYPE_VOID) {\n+        memcpy(&dst->values[dst->n], &src->values[start_idx],\n+               n * sizeof src->values[0]);\n+    }\n+    dst->n += n;\n+}\n+\n void\n ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b,\n-                  const struct ovsdb_type *type, bool replace)\n+                  const struct ovsdb_type *type)\n {\n-    unsigned int n;\n-    size_t bi;\n+    struct ovsdb_datum result;\n+    unsigned int copied, pos;\n+    size_t ai, bi;\n \n-    n = a->n;\n-    for (bi = 0; bi < b->n; bi++) {\n-        unsigned int ai;\n+    ovsdb_datum_init_empty(&result);\n+    ovsdb_datum_reallocate(&result, type, a->n + b->n);\n \n-        ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key.type);\n-        if (ai == UINT_MAX) {\n-            if (n == a->n) {\n-                ovsdb_datum_reallocate(a, type, a->n + (b->n - bi));\n-            }\n-            ovsdb_atom_clone(&a->keys[n], &b->keys[bi], type->key.type);\n-            if (type->value.type != OVSDB_TYPE_VOID) {\n-                ovsdb_atom_clone(&a->values[n], &b->values[bi],\n-                                 type->value.type);\n-            }\n-            n++;\n-        } else if (replace && type->value.type != OVSDB_TYPE_VOID) {\n-            ovsdb_atom_destroy(&a->values[ai], type->value.type);\n-            ovsdb_atom_clone(&a->values[ai], &b->values[bi],\n+    copied = 0;\n+    for (bi = 0; bi < b->n; bi++) {\n+        ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key.type, &pos);\n+        if (ai != UINT_MAX) {\n+            /* Atom with the same key already exists. */\n+            continue;\n+        }\n+        if (pos > copied) {\n+            /* Need to copy some atoms from 'a' first. */\n+            ovsdb_datum_push_unsafe(&result, a, copied, pos - copied, type);\n+            copied = pos;\n+        }\n+        /* Inserting new atom from 'b'. */\n+        ovsdb_atom_clone(&result.keys[result.n], &b->keys[bi], type->key.type);\n+        if (type->value.type != OVSDB_TYPE_VOID) {\n+            ovsdb_atom_clone(&result.values[result.n], &b->values[bi],\n                              type->value.type);\n         }\n+        result.n++;\n     }\n-    if (n != a->n) {\n-        a->n = n;\n-        ovs_assert(!ovsdb_datum_sort(a, type->key.type));\n+    if (a->n > copied) {\n+        /* Copying remaining atoms. */\n+        ovsdb_datum_push_unsafe(&result, a, copied, a->n - copied, type);\n     }\n+    /* All atoms are copied now. */\n+    a->n = 0;\n+\n+    ovsdb_datum_swap(&result, a);\n+    ovsdb_datum_destroy(&result, type);\n }\n \n void\ndiff --git a/lib/ovsdb-data.h b/lib/ovsdb-data.h\nindex c5a80ee39..a580cd2bd 100644\n--- a/lib/ovsdb-data.h\n+++ b/lib/ovsdb-data.h\n@@ -211,7 +211,8 @@ bool ovsdb_datum_equals(const struct ovsdb_datum *,\n /* Search. */\n unsigned int ovsdb_datum_find_key(const struct ovsdb_datum *,\n                                   const union ovsdb_atom *key,\n-                                  enum ovsdb_atomic_type key_type);\n+                                  enum ovsdb_atomic_type key_type,\n+                                  unsigned int *pos);\n unsigned int ovsdb_datum_find_key_value(const struct ovsdb_datum *,\n                                         const union ovsdb_atom *key,\n                                         enum ovsdb_atomic_type key_type,\n@@ -227,8 +228,7 @@ bool ovsdb_datum_excludes_all(const struct ovsdb_datum *,\n                               const struct ovsdb_type *);\n void ovsdb_datum_union(struct ovsdb_datum *,\n                        const struct ovsdb_datum *,\n-                       const struct ovsdb_type *,\n-                       bool replace);\n+                       const struct ovsdb_type *);\n void ovsdb_datum_subtract(struct ovsdb_datum *a,\n                           const struct ovsdb_type *a_type,\n                           const struct ovsdb_datum *b,\ndiff --git a/lib/ovsdb-idl.c b/lib/ovsdb-idl.c\nindex d3caccec8..d9b0993f6 100644\n--- a/lib/ovsdb-idl.c\n+++ b/lib/ovsdb-idl.c\n@@ -2842,7 +2842,7 @@ ovsdb_idl_txn_extract_mutations(struct ovsdb_idl_row *row,\n                     new_datum = map_op_datum(map_op);\n                     pos = ovsdb_datum_find_key(old_datum,\n                                                &new_datum->keys[0],\n-                                               key_type);\n+                                               key_type, NULL);\n                     if (ovsdb_atom_equals(&new_datum->values[0],\n                                           &old_datum->values[pos],\n                                           value_type)) {\n@@ -2854,7 +2854,7 @@ ovsdb_idl_txn_extract_mutations(struct ovsdb_idl_row *row,\n                     unsigned int pos;\n                     pos = ovsdb_datum_find_key(old_datum,\n                                                &map_op_datum(map_op)->keys[0],\n-                                               key_type);\n+                                               key_type, NULL);\n                     if (pos == UINT_MAX) {\n                         /* No key to delete.  Move on to next update. */\n                         VLOG_WARN(\"Trying to delete a key that doesn't \"\n@@ -2953,7 +2953,7 @@ ovsdb_idl_txn_extract_mutations(struct ovsdb_idl_row *row,\n                     unsigned int pos;\n                     pos = ovsdb_datum_find_key(old_datum,\n                                                &set_op_datum(set_op)->keys[0],\n-                                               key_type);\n+                                               key_type, NULL);\n                     if (pos == UINT_MAX) {\n                         /* No key to delete.  Move on to next update. */\n                         VLOG_WARN(\"Trying to delete a key that doesn't \"\n@@ -4131,7 +4131,7 @@ ovsdb_idl_txn_write_partial_map(const struct ovsdb_idl_row *row_,\n     /* Find out if this is an insert or an update. */\n     key_type = column->type.key.type;\n     old_datum = ovsdb_idl_read(row, column);\n-    pos = ovsdb_datum_find_key(old_datum, &datum->keys[0], key_type);\n+    pos = ovsdb_datum_find_key(old_datum, &datum->keys[0], key_type, NULL);\n     op_type = pos == UINT_MAX ? MAP_OP_INSERT : MAP_OP_UPDATE;\n \n     ovsdb_idl_txn_add_map_op(row, column, datum, op_type);\ndiff --git a/ovsdb/mutation.c b/ovsdb/mutation.c\nindex 56edc5f00..03d1c3499 100644\n--- a/ovsdb/mutation.c\n+++ b/ovsdb/mutation.c\n@@ -383,7 +383,7 @@ ovsdb_mutation_set_execute(struct ovsdb_row *row,\n             break;\n \n         case OVSDB_M_INSERT:\n-            ovsdb_datum_union(dst, arg, dst_type, false);\n+            ovsdb_datum_union(dst, arg, dst_type);\n             error = ovsdb_mutation_check_count(dst, dst_type);\n             break;\n \ndiff --git a/vswitchd/bridge.c b/vswitchd/bridge.c\nindex cb7c5cb76..c7a7e55e5 100644\n--- a/vswitchd/bridge.c\n+++ b/vswitchd/bridge.c\n@@ -4229,7 +4229,8 @@ bridge_configure_aa(struct bridge *br)\n         union ovsdb_atom atom;\n \n         atom.integer = m->isid;\n-        if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_INTEGER) == UINT_MAX) {\n+        if (ovsdb_datum_find_key(mc, &atom,\n+                                 OVSDB_TYPE_INTEGER, NULL) == UINT_MAX) {\n             VLOG_INFO(\"Deleting isid=%\"PRIu32\", vlan=%\"PRIu16,\n                       m->isid, m->vlan);\n             bridge_aa_mapping_destroy(m);\n@@ -4826,7 +4827,8 @@ queue_ids_include(const struct ovsdb_datum *queues, int64_t target)\n     union ovsdb_atom atom;\n \n     atom.integer = target;\n-    return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;\n+    return ovsdb_datum_find_key(queues, &atom,\n+                                OVSDB_TYPE_INTEGER, NULL) != UINT_MAX;\n }\n \n static void\n@@ -5020,7 +5022,8 @@ bridge_configure_mirrors(struct bridge *br)\n         union ovsdb_atom atom;\n \n         atom.uuid = m->uuid;\n-        if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {\n+        if (ovsdb_datum_find_key(mc, &atom,\n+                                 OVSDB_TYPE_UUID, NULL) == UINT_MAX) {\n             mirror_destroy(m);\n         }\n     }\n",
    "prefixes": [