diff options
author | Tom Lane | 2014-11-28 18:37:25 +0000 |
---|---|---|
committer | Tom Lane | 2014-11-28 18:37:25 +0000 |
commit | f4e031c662a6b600b786c4849968a099c58fcce7 (patch) | |
tree | 6a082f889ff2ea5b64bb43c467760686e5f013b0 /src/backend/optimizer | |
parent | 96d66bcfc60d9bcb7db767f23d33abf4d8bc7021 (diff) |
Add bms_next_member(), and use it where appropriate.
This patch adds a way of iterating through the members of a bitmapset
nondestructively, unlike the old way with bms_first_member(). While
bms_next_member() is very slightly slower than bms_first_member()
(at least for typical-size bitmapsets), eliminating the need to palloc
and pfree a temporary copy of the target bitmapset is a significant win.
So this method should be preferred in all cases where a temporary copy
would be necessary.
Tom Lane, with suggestions from Dean Rasheed and David Rowley
Diffstat (limited to 'src/backend/optimizer')
-rw-r--r-- | src/backend/optimizer/path/allpaths.c | 6 | ||||
-rw-r--r-- | src/backend/optimizer/path/indxpath.c | 6 | ||||
-rw-r--r-- | src/backend/optimizer/util/joininfo.c | 12 | ||||
-rw-r--r-- | src/backend/optimizer/util/var.c | 6 |
4 files changed, 10 insertions, 20 deletions
diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 25f30676f02..449fdc34742 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -2272,19 +2272,17 @@ remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel) static void print_relids(Relids relids) { - Relids tmprelids; int x; bool first = true; - tmprelids = bms_copy(relids); - while ((x = bms_first_member(tmprelids)) >= 0) + x = -1; + while ((x = bms_next_member(relids, x)) >= 0) { if (!first) printf(" "); printf("%d", x); first = false; } - bms_free(tmprelids); } static void diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c index 1ee3b93b1e3..7aab644e456 100644 --- a/src/backend/optimizer/path/indxpath.c +++ b/src/backend/optimizer/path/indxpath.c @@ -1875,9 +1875,8 @@ get_loop_count(PlannerInfo *root, Relids outer_relids) { int relid; - /* Need a working copy since bms_first_member is destructive */ - outer_relids = bms_copy(outer_relids); - while ((relid = bms_first_member(outer_relids)) >= 0) + relid = -1; + while ((relid = bms_next_member(outer_relids, relid)) >= 0) { RelOptInfo *outer_rel; @@ -1900,7 +1899,6 @@ get_loop_count(PlannerInfo *root, Relids outer_relids) if (result == 1.0 || result > outer_rel->rows) result = outer_rel->rows; } - bms_free(outer_relids); } return result; } diff --git a/src/backend/optimizer/util/joininfo.c b/src/backend/optimizer/util/joininfo.c index 0418946d714..40af38d7ada 100644 --- a/src/backend/optimizer/util/joininfo.c +++ b/src/backend/optimizer/util/joininfo.c @@ -96,17 +96,15 @@ add_join_clause_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids) { - Relids tmprelids; int cur_relid; - tmprelids = bms_copy(join_relids); - while ((cur_relid = bms_first_member(tmprelids)) >= 0) + cur_relid = -1; + while ((cur_relid = bms_next_member(join_relids, cur_relid)) >= 0) { RelOptInfo *rel = find_base_rel(root, cur_relid); rel->joininfo = lappend(rel->joininfo, restrictinfo); } - bms_free(tmprelids); } /* @@ -125,11 +123,10 @@ remove_join_clause_from_rels(PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids) { - Relids tmprelids; int cur_relid; - tmprelids = bms_copy(join_relids); - while ((cur_relid = bms_first_member(tmprelids)) >= 0) + cur_relid = -1; + while ((cur_relid = bms_next_member(join_relids, cur_relid)) >= 0) { RelOptInfo *rel = find_base_rel(root, cur_relid); @@ -140,5 +137,4 @@ remove_join_clause_from_rels(PlannerInfo *root, Assert(list_member_ptr(rel->joininfo, restrictinfo)); rel->joininfo = list_delete_ptr(rel->joininfo, restrictinfo); } - bms_free(tmprelids); } diff --git a/src/backend/optimizer/util/var.c b/src/backend/optimizer/util/var.c index d4f46b8d461..a64a8d7d5ff 100644 --- a/src/backend/optimizer/util/var.c +++ b/src/backend/optimizer/util/var.c @@ -773,11 +773,10 @@ static Relids alias_relid_set(PlannerInfo *root, Relids relids) { Relids result = NULL; - Relids tmprelids; int rtindex; - tmprelids = bms_copy(relids); - while ((rtindex = bms_first_member(tmprelids)) >= 0) + rtindex = -1; + while ((rtindex = bms_next_member(relids, rtindex)) >= 0) { RangeTblEntry *rte = rt_fetch(rtindex, root->parse->rtable); @@ -786,6 +785,5 @@ alias_relid_set(PlannerInfo *root, Relids relids) else result = bms_add_member(result, rtindex); } - bms_free(tmprelids); return result; } |