Skip to content

Commit

Permalink
Optimize LimitedSet and LimitedMap (#10)
Browse files Browse the repository at this point in the history
* In `LimitedSet` and `LimitedMap` use new optimized code for `std::less` and make that the default.

* Update format

* Fix comment

* Implement separate benchmark for limited_set (tests limited_ordered).
* Add `LimitedOptionsFlag::kNoOptimizeIndexOf` (disallows unrolling, etc).
* The benchmark tests all versions.
* The normal set test is expanded to verify all versions.
* Further optimizations for `index_of`.
  * Extend unrolling to 32 cases (might need to be even 34).
  * Explain unlikely cases in compiler-hints.

Performance analysis on a x86_64 system shows better or same performance
for `LimitedSet` compared to `std::set`. This has to be further analyzed
and tweaked.

* Improve benhchmark

* In `LimitedSet` and `LimitedMap` use new optimized code for `std::less` and make that the default.

* Fix comment

* Implement separate benchmark for limited_set (tests limited_ordered).
* Add `LimitedOptionsFlag::kNoOptimizeIndexOf` (disallows unrolling, etc).
* The benchmark tests all versions.
* The normal set test is expanded to verify all versions.
* Further optimizations for `index_of`.
  * Extend unrolling to 32 cases (might need to be even 34).
  * Explain unlikely cases in compiler-hints.

Performance analysis on a x86_64 system shows better or same performance
for `LimitedSet` compared to `std::set`. This has to be further analyzed
and tweaked.

* Add missing private marker

* Mark benchmark as manual

* Test iterator difference

* A few more micro optimizations:
* Mark additional wrapper functions as force inline
* Provide additional branch guidance

* Fix after sync.

* Optimize according to performance measurements:
* By default do not optimize past loop-unroll.
* Change loop unroll to 24.

* Add missing new line

* Cleanup and move LimitedOrdered unrolling config into separate file.
That file should be configurable outside - maybe via a bazel flag,
maybe by generating the file, possibly a configuration.

* Added custom bazel flag `--//mbo/container:limited_ordered_max_unroll_capacity=N`.

* Add explanation to static_assert.
* Add bzl library `//mbo/container:limited_ordered_config_bzl`.
  • Loading branch information
helly25 authored Jan 24, 2024
1 parent 3949622 commit 746004e
Show file tree
Hide file tree
Showing 20 changed files with 682 additions and 188 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ build --define absl=1
build --cxxopt=-std=c++20 --host_cxxopt=-std=c++20 --cxxopt=-Werror
test --test_output=errors

build --copt=-fdiagnostics-color=always

# Custom --config=asan mode:
build:asan --copt -fsanitize=address,undefined
build:asan --linkopt -fsanitize=address,undefined
Expand Down
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ BreakInheritanceList: BeforeComma
DerivePointerAlignment: false
EmptyLineBeforeAccessModifier: Always
FixNamespaceComments: true
IndentPPDirectives: AfterHash
IndentRequiresClause: false
InsertBraces: true
InsertNewlineAtEOF: true
Expand All @@ -36,6 +37,7 @@ MaxEmptyLinesToKeep: 1
PackConstructorInitializers: NextLine
PenaltyReturnTypeOnItsOwnLine: 1000
PointerAlignment: Left
PPIndentWidth: 1
QualifierAlignment: Left
ReferenceAlignment: Left
RemoveSemicolon: true
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
/compile_commands.json
# Ignore the directory in which `clangd` stores its local index.
/.cache/
/.vscode/
/.vscode/
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.2.18

* Changed `LimitedSet` and `LimitedMap`:
* Changed to new optimized code for `std::less` and make that the default.
* Added custom bazel flag `--//mbo/container:limited_ordered_max_unroll_capacity=N`. This controls the maximum capacity `N` for which `index_of` will be unrolled (defaults to 16, range [4...32], see `mbo::container::container_internal::kUnrollMaxCapacityDefault`.

# 0.2.17

* Added template-type `RefWrap<T>`: similar to `std::reference_wrapper` but supports operators `->` and `*`.
Expand Down
45 changes: 43 additions & 2 deletions mbo/container/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,30 @@
# See the License for the specific language governing permissions and
# limitations under the License.

package(default_visibility = ["//visibility:private"])
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "int_flag")
load(":internal/limited_ordered_config.bzl", "limited_ordered_config_gen")

# Create custom bazel flag `--//mbo/container:limited_ordered_max_unroll_capacity`.
int_flag(
name = "limited_ordered_max_unroll_capacity",
build_setting_default = 16,
visibility = ["//visibility:private"],
)

bzl_library(
name = "limited_ordered_config_bzl",
srcs = [":internal/limited_ordered_config.bzl"],
visibility = ["//visibility:private"],
deps = ["@bazel_skylib//rules:common_settings"],
)

limited_ordered_config_gen(
name = "limited_ordered_config_gen",
visibility = ["//visibility:private"],
output = "internal/limited_ordered_config.h",
template = "internal/limited_ordered_config.h.in"
)

cc_library(
name = "any_scan_cc",
Expand Down Expand Up @@ -94,7 +117,10 @@ cc_library(

cc_library(
name = "limited_ordered_cc",
hdrs = ["internal/limited_ordered.h"],
hdrs = [
"internal/limited_ordered_config.h",
"internal/limited_ordered.h",
],
visibility = ["//visibility:private"],
deps = [
":limited_options_cc",
Expand Down Expand Up @@ -141,6 +167,21 @@ cc_test(
],
)

cc_binary(
name = "limited_set_benchmark",
visibility = ["//visibility:private"],
srcs = ["limited_set_benchmark.cc"],
deps = [
":limited_set_cc",
":limited_vector_cc",
"//mbo/testing:matchers_cc",
"@com_github_google_benchmark//:benchmark",
"@com_google_absl//absl/log:initialize",
"@com_google_absl//absl/strings",
],
tags = ["manual"],
)

cc_library(
name = "limited_vector_cc",
hdrs = ["limited_vector.h"],
Expand Down
Loading

0 comments on commit 746004e

Please sign in to comment.