-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Provide consistent alignment to swap_ranges benchmark
#5043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
213b3a9
Provide consistent alignment to swap_ranges benchmark
AlexGuteniev 4280a91
proper cast
AlexGuteniev 70f8db6
Update benchmarks/inc/alloc.hpp
AlexGuteniev 94366fa
Update benchmarks/inc/alloc.hpp
AlexGuteniev 79f2678
Update benchmarks/src/swap_ranges.cpp
AlexGuteniev 9cb3346
Fixup after commit from web
AlexGuteniev b3e73ed
hide padder
AlexGuteniev 7cc37d4
not scary names
AlexGuteniev 3a76097
Rename to skewed_allocator.hpp.
StephanTLavavej 9ae7647
`bad_alloc` is provided by `<new>`, not `<stdexcept>`.
StephanTLavavej d922de8
Add newline.
StephanTLavavej c9e7e25
Use `inline constexpr`.
StephanTLavavej 388afba
Separate `static_assert`s.
StephanTLavavej f5e718b
`skew` => `realistic_skew`
StephanTLavavej daab7aa
Improve comment grammar.
StephanTLavavej c57d124
Add `const`.
StephanTLavavej 7fcdefc
Bugfix: Use aliases for allocators instead of derived structs.
StephanTLavavej e79e93c
Bugfix: `type` => `other`
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,60 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdlib> | ||
| #include <stdexcept> | ||
StephanTLavavej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| template <class T, size_t Alignment, size_t Skew> | ||
| struct skewed_allocator { | ||
| using value_type = T; | ||
|
|
||
AlexGuteniev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
AlexGuteniev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| T* allocate(size_t n) { | ||
StephanTLavavej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const auto p = static_cast<unsigned char*>(_aligned_malloc(n * sizeof(T) + Skew, Alignment)); | ||
| if (!p) { | ||
| throw std::bad_alloc{}; | ||
| } | ||
| return reinterpret_cast<T*>(p + Skew); | ||
| } | ||
|
|
||
| void deallocate(T* p, size_t) { | ||
| if (p) { | ||
| _aligned_free(reinterpret_cast<unsigned char*>(p) - Skew); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // The purpose is to provide consistent behavior for benchmarks. | ||
| // 64 seems to be reasonable alignment for practical perf uses, | ||
| // as it is both cache line size and maximum vector instruction size (on x64). | ||
| // However to provide even more consistency, aligning to page, | ||
| // to make sure the same number of page boundaries is crossed each time. | ||
| constexpr size_t page_size = 4096; | ||
StephanTLavavej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // A realistic skew relative to allocation granularity, when a variable is placed | ||
| // next to a pointer in a structure or on stack. Also corresponds to the default packing. | ||
| constexpr size_t skew = 8; | ||
StephanTLavavej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| template <class T> | ||
| struct aligned_allocator : skewed_allocator<T, page_size, 0> {}; | ||
|
|
||
| template <class T> | ||
| struct unaligned_allocator : skewed_allocator<T, page_size, skew> {}; | ||
CaseyCarter marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #pragma warning(push) | ||
| #pragma warning(disable : 4324) // structure was padded due to alignment specifier | ||
|
|
||
| template <class T> | ||
| struct alignas(page_size) aligner { | ||
| T value; | ||
| }; | ||
|
|
||
| template <class T> | ||
| struct alignas(page_size) unaligner { | ||
| char pad[skew]; | ||
| T value; | ||
| }; | ||
|
|
||
| #pragma warning(pop) | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.