Skip to content

Commit

Permalink
Add empty base optimization to allocators and containers
Browse files Browse the repository at this point in the history
  • Loading branch information
KredeGC committed May 23, 2023
1 parent fd6238a commit 4c5e768
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 14 deletions.
3 changes: 2 additions & 1 deletion include/ktl/allocators/cascading.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../utility/aligned_malloc.h"
#include "../utility/alignment.h"
#include "../utility/assert.h"
#include "../utility/empty_base.h"
#include "../utility/meta.h"
#include "cascading_fwd.h"
#include "type_allocator.h"
Expand Down Expand Up @@ -35,7 +36,7 @@ namespace ktl
private:
struct node
{
Alloc Allocator;
KTL_EMPTY_BASE Alloc Allocator;
size_type Allocations = 0;
node* Next = nullptr;
};
Expand Down
5 changes: 3 additions & 2 deletions include/ktl/allocators/fallback.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "../utility/empty_base.h"
#include "../utility/meta.h"
#include "fallback_fwd.h"
#include "type_allocator.h"
Expand Down Expand Up @@ -188,7 +189,7 @@ namespace ktl
#pragma endregion

private:
P m_Primary;
F m_Fallback;
KTL_EMPTY_BASE P m_Primary;
KTL_EMPTY_BASE F m_Fallback;
};
}
3 changes: 2 additions & 1 deletion include/ktl/allocators/freelist.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../utility/assert.h"
#include "../utility/empty_base.h"
#include "../utility/meta.h"
#include "freelist_fwd.h"
#include "type_allocator.h"
Expand Down Expand Up @@ -224,7 +225,7 @@ namespace ktl
}

private:
Alloc m_Alloc;
KTL_EMPTY_BASE Alloc m_Alloc;
link* m_Free;
};
}
3 changes: 2 additions & 1 deletion include/ktl/allocators/overflow.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../utility/assert.h"
#include "../utility/empty_base.h"
#include "../utility/meta.h"
#include "overflow_fwd.h"
#include "type_allocator.h"
Expand Down Expand Up @@ -261,7 +262,7 @@ namespace ktl

private:
Stream& m_Stream;
Alloc m_Alloc;
KTL_EMPTY_BASE Alloc m_Alloc;
int64_t m_Allocs;
int64_t m_Constructs;
};
Expand Down
5 changes: 3 additions & 2 deletions include/ktl/allocators/segragator.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "../utility/empty_base.h"
#include "../utility/meta.h"
#include "segragator_fwd.h"
#include "type_allocator.h"
Expand Down Expand Up @@ -182,7 +183,7 @@ namespace ktl
#pragma endregion

private:
P m_Primary;
F m_Fallback;
KTL_EMPTY_BASE P m_Primary;
KTL_EMPTY_BASE F m_Fallback;
};
}
3 changes: 2 additions & 1 deletion include/ktl/allocators/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "../utility/aligned_malloc.h"
#include "../utility/alignment.h"
#include "../utility/empty_base.h"
#include "../utility/meta.h"
#include "shared_fwd.h"

Expand All @@ -18,7 +19,7 @@ namespace ktl

struct block
{
Alloc Allocator;
KTL_EMPTY_BASE Alloc Allocator;
detail::get_size_type_t<Alloc> UseCount;

template<typename... Args,
Expand Down
3 changes: 2 additions & 1 deletion include/ktl/allocators/threaded.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "../utility/aligned_malloc.h"
#include "../utility/alignment.h"
#include "../utility/empty_base.h"
#include "../utility/meta.h"
#include "threaded_fwd.h"

Expand All @@ -20,7 +21,7 @@ namespace ktl

struct block
{
Alloc Allocator;
KTL_EMPTY_BASE Alloc Allocator;
std::atomic<detail::get_size_type_t<Alloc>> UseCount;
std::mutex Lock;

Expand Down
3 changes: 2 additions & 1 deletion include/ktl/allocators/type_allocator.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "../utility/empty_base.h"
#include "../utility/meta.h"

#include "type_allocator_fwd.h"
Expand Down Expand Up @@ -158,7 +159,7 @@ namespace ktl
}

private:
Alloc m_Alloc;
KTL_EMPTY_BASE Alloc m_Alloc;
};

template<typename T, typename U, typename Alloc>
Expand Down
5 changes: 3 additions & 2 deletions include/ktl/containers/binary_heap.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../utility/assert.h"
#include "../utility/empty_base.h"
#include "binary_heap_fwd.h"

#include <cstddef>
Expand Down Expand Up @@ -458,8 +459,8 @@ namespace ktl
}

private:
Alloc m_Alloc;
Comp m_Comp;
KTL_EMPTY_BASE Alloc m_Alloc;
KTL_EMPTY_BASE Comp m_Comp;

size_t m_Size;
size_t m_Capacity;
Expand Down
3 changes: 2 additions & 1 deletion include/ktl/containers/trivial_array.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../utility/assert.h"
#include "../utility/empty_base.h"
#include "trivial_array_fwd.h"

#include <cstring>
Expand Down Expand Up @@ -302,7 +303,7 @@ namespace ktl
}

private:
Alloc m_Alloc;
KTL_EMPTY_BASE Alloc m_Alloc;
T* m_Begin;
T* m_End;
};
Expand Down
3 changes: 2 additions & 1 deletion include/ktl/containers/trivial_vector.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../utility/assert.h"
#include "../utility/empty_base.h"
#include "trivial_vector_fwd.h"

#include <cstring>
Expand Down Expand Up @@ -458,7 +459,7 @@ namespace ktl
}

private:
Alloc m_Alloc;
KTL_EMPTY_BASE Alloc m_Alloc;
T* m_Begin;
T* m_End;
T* m_EndMax;
Expand Down
7 changes: 7 additions & 0 deletions include/ktl/utility/empty_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#if defined(__has_cpp_attribute) && __has_cpp_attribute(no_unique_address)
#define KTL_EMPTY_BASE [[no_unique_address]]
#else // __has_cpp_attribute(no_unique_address)
#define KTL_EMPTY_BASE
#endif // __has_cpp_attribute(no_unique_address)

0 comments on commit 4c5e768

Please sign in to comment.