-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
199 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
project(msft_proxy_benchmarks) | ||
|
||
include(FetchContent) | ||
# The policy uses the download time for timestamp, instead of the timestamp in the archive. This | ||
# allows for proper rebuilds when a projects URL changes. | ||
if(POLICY CMP0135) | ||
cmake_policy(SET CMP0135 NEW) | ||
endif() | ||
|
||
FetchContent_Declare( | ||
benchmark | ||
URL https://github.com/google/benchmark/archive/refs/tags/v1.9.0.tar.gz | ||
URL_HASH SHA256=35a77f46cc782b16fac8d3b107fbfbb37dcd645f7c28eee19f3b8e0758b48994 | ||
) | ||
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Disable tests for google benchmark") | ||
set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "Disable google benchmark unit tests") | ||
FetchContent_MakeAvailable(benchmark) | ||
|
||
add_executable(msft_proxy_benchmarks | ||
proxy_small_object_invocation_benchmark_context.cpp | ||
proxy_small_object_invocation_benchmark.cpp | ||
) | ||
target_include_directories(msft_proxy_benchmarks PRIVATE .) | ||
target_link_libraries(msft_proxy_benchmarks PRIVATE msft_proxy benchmark::benchmark benchmark::benchmark_main) | ||
|
||
if (MSVC) | ||
target_compile_options(msft_proxy_benchmarks PRIVATE /W4) | ||
else() | ||
target_compile_options(msft_proxy_benchmarks PRIVATE -Wall -Wextra -Wpedantic) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#include <benchmark/benchmark.h> | ||
|
||
#include "proxy_small_object_invocation_benchmark_context.h" | ||
|
||
void BM_SmallObjectInvocationViaProxy(benchmark::State& state) { | ||
for (auto _ : state) { | ||
for (auto& p : ProxyTestData) { | ||
int result = p->Fun(); | ||
benchmark::DoNotOptimize(result); | ||
} | ||
} | ||
} | ||
|
||
void BM_SmallObjectInvocationViaVirtualFunction(benchmark::State& state) { | ||
for (auto _ : state) { | ||
for (auto& p : VirtualFunctionTestData) { | ||
int result = p->Fun(); | ||
benchmark::DoNotOptimize(result); | ||
} | ||
} | ||
} | ||
|
||
BENCHMARK(BM_SmallObjectInvocationViaProxy); | ||
BENCHMARK(BM_SmallObjectInvocationViaVirtualFunction); |
68 changes: 68 additions & 0 deletions
68
benchmarks/proxy_small_object_invocation_benchmark_context.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#include "proxy_small_object_invocation_benchmark_context.h" | ||
|
||
namespace { | ||
|
||
constexpr std::size_t TestDataSize = 1500000; | ||
constexpr std::size_t TypeSeriesCount = 3; | ||
|
||
template <std::size_t TypeSeries> | ||
class NonIntrusiveImpl { | ||
public: | ||
explicit NonIntrusiveImpl(int seed) noexcept : seed_(seed) {} | ||
NonIntrusiveImpl(const NonIntrusiveImpl&) noexcept = default; | ||
int Fun() const noexcept { return seed_ ^ (TypeSeries + 1u); } | ||
|
||
private: | ||
int seed_; | ||
}; | ||
|
||
template <std::size_t TypeSeries> | ||
class IntrusiveImpl : public TestBase { | ||
public: | ||
explicit IntrusiveImpl(int seed) noexcept : seed_(seed) {} | ||
IntrusiveImpl(const IntrusiveImpl&) noexcept = default; | ||
int Fun() const noexcept override { return seed_ ^ (TypeSeries + 1u); } | ||
|
||
private: | ||
int seed_; | ||
}; | ||
|
||
template <std::size_t FromTypeSeries> | ||
void FillProxyTestData(std::vector<pro::proxy<TestFacade>>& data) { | ||
if constexpr (FromTypeSeries < TypeSeriesCount) { | ||
for (std::size_t i = FromTypeSeries; i < data.size(); i += TypeSeriesCount) { | ||
data[i] = pro::make_proxy<TestFacade, NonIntrusiveImpl<FromTypeSeries>>(static_cast<int>(i)); | ||
} | ||
FillProxyTestData<FromTypeSeries + 1u>(data); | ||
} | ||
} | ||
|
||
std::vector<pro::proxy<TestFacade>> GenerateProxyTestData() { | ||
std::vector<pro::proxy<TestFacade>> result(TestDataSize); | ||
FillProxyTestData<0u>(result); | ||
return result; | ||
} | ||
|
||
template <std::size_t FromTypeSeries> | ||
void FillVirtualFunctionTestData(std::vector<std::unique_ptr<TestBase>>& data) { | ||
if constexpr (FromTypeSeries < TypeSeriesCount) { | ||
for (std::size_t i = FromTypeSeries; i < data.size(); i += TypeSeriesCount) { | ||
data[i].reset(new IntrusiveImpl<FromTypeSeries>(static_cast<int>(i))); | ||
} | ||
FillVirtualFunctionTestData<FromTypeSeries + 1u>(data); | ||
} | ||
} | ||
|
||
std::vector<std::unique_ptr<TestBase>> GenerateVirtualFunctionTestData() { | ||
std::vector<std::unique_ptr<TestBase>> result(TestDataSize); | ||
FillVirtualFunctionTestData<0u>(result); | ||
return result; | ||
} | ||
|
||
} // namespace | ||
|
||
const std::vector<pro::proxy<TestFacade>> ProxyTestData = GenerateProxyTestData(); | ||
const std::vector<std::unique_ptr<TestBase>> VirtualFunctionTestData = GenerateVirtualFunctionTestData(); |
21 changes: 21 additions & 0 deletions
21
benchmarks/proxy_small_object_invocation_benchmark_context.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
#include "proxy.h" | ||
|
||
PRO_DEF_MEM_DISPATCH(MemFun, Fun); | ||
|
||
struct TestFacade : pro::facade_builder | ||
::add_convention<MemFun, int() const> | ||
::build{}; | ||
|
||
struct TestBase { | ||
virtual int Fun() const = 0; | ||
virtual ~TestBase() = default; | ||
}; | ||
|
||
extern const std::vector<pro::proxy<TestFacade>> ProxyTestData; | ||
extern const std::vector<std::unique_ptr<TestBase>> VirtualFunctionTestData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters