Skip to content

Commit f835921

Browse files
committed
Jsonifier Release v0.9.8
-Improved the Vector classes' implementation. -Improved the collectEscapedCharacters function. -Implemented the fallback logic.
1 parent 790d763 commit f835921

35 files changed

+1734
-2146
lines changed

.github/workflows/GCC_12-Ubuntu.yml renamed to .github/workflows/CLANG_18-Ubuntu.yml

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build-and-Test-GCC-Ubuntu
1+
name: Build-and-Test-CLANG-Ubuntu
22

33
on:
44
push:
@@ -13,20 +13,24 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
gcc: [12]
16+
clang: [18]
1717
build_type: [Debug, Release]
1818
std: [20]
19-
env:
20-
CC: gcc-${{matrix.gcc}}
21-
CXX: g++-${{matrix.gcc}}
2219

2320
steps:
2421
- uses: actions/checkout@v3
22+
23+
- name: Install the latest Clang compiler.
24+
working-directory: Tests
25+
run: |
26+
wget https://apt.llvm.org/llvm.sh
27+
chmod +x llvm.sh
28+
sudo ./llvm.sh 18
2529
2630
- name: Configure CMake
2731
working-directory: Tests
2832
run: |
29-
cmake -S . -B ./Build -DCMAKE_BUILD_TYPE=${{matrix.build_type}} -DGITHUB_BRANCH_TYPE=${{github.ref}}
33+
cmake -S . -B ./Build -DCMAKE_BUILD_TYPE=${{matrix.build_type}} -DCMAKE_CXX_COMPILER=/usr/bin/clang++-18 -DGITHUB_BRANCH_TYPE=${{github.ref}}
3034
3135
- name: Build the Test
3236
working-directory: Tests/Build

.github/workflows/CLANG_16-MacOS.yml renamed to .github/workflows/GCC_13-MacOS.yml

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build-and-Test-CLANG-MacOS
1+
name: Build-and-Test-GCC-MacOS
22

33
on:
44
push:
@@ -13,24 +13,22 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
clang: [17]
16+
gcc: [13]
1717
build_type: [Debug, Release]
1818
std: [20]
1919

2020
steps:
2121
- uses: actions/checkout@v3
2222

23-
- name: Install Clang17
23+
- name: Install the latest g++ compiler.
2424
run: |
25-
brew install llvm
26-
echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.bash_profile
27-
export LDFLAGS="-L/usr/local/opt/llvm/lib"
28-
export CPPFLAGS="-I/usr/local/opt/llvm/include"
25+
brew install gcc
2926
3027
- name: Configure CMake
3128
working-directory: Tests
3229
run: |
33-
cmake -S . -B ./Build -DCMAKE_BUILD_TYPE=${{matrix.build_type}} -DCMAKE_CXX_COMPILER=/usr/local/opt/llvm/bin/clang-16 -DGITHUB_BRANCH_TYPE=${{github.ref}}
30+
31+
cmake -S . -B ./Build -DCMAKE_BUILD_TYPE=${{matrix.build_type}} -DCMAKE_CXX_COMPILER=/usr/local/opt/gcc@13/bin/g++-13 -DGITHUB_BRANCH_TYPE=${{github.ref}}
3432
3533
- name: Build the Test
3634
working-directory: Tests/Build

CMake/DetectArchitecture.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ endif()
4545

4646
set(CMAKE_REQUIRED_FLAGS_SAVE "${CMAKE_REQUIRED_FLAGS}")
4747

48+
set(AVX_FLAG "-march=native")
4849
set(AVX_TYPE "T_Fallback")
4950

5051
if ((${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") OR (${CMAKE_SYSTEM_PROCESSOR} MATCHES "i386") OR (${CMAKE_SYSTEM_PROCESSOR} MATCHES "AMD64"))

CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,21 @@ target_compile_options(
7979
"$<$<CXX_COMPILER_ID:MSVC>:/GL>"
8080
"$<$<CXX_COMPILER_ID:GNU>:-pedantic>"
8181
"$<$<CXX_COMPILER_ID:CLANG>:-pedantic>"
82+
"$<$<CXX_COMPILER_ID:CLANG>:-I/usr/local/opt/llvm/include/c++/v1/>"
83+
"$<$<CXX_COMPILER_ID:CLANG>:-I/usr/local/opt/llvm/include>"
8284
"${AVX_FLAG}"
8385
)
8486

8587
target_link_options(
8688
"${PROJECT_NAME}" INTERFACE
8789
"$<$<CXX_COMPILER_ID:GNU>:$<$<STREQUAL:${ASAN_ENABLED},TRUE>:-fsanitize=address>>"
90+
"$<$<CXX_COMPILER_ID:CLANG>:-Wl,-rpath,/usr/local/opt/llvm/lib>"
91+
"$<$<CXX_COMPILER_ID:CLANG>:-L/usr/local/opt/llvm/lib>"
92+
)
93+
94+
target_link_libraries(
95+
"${PROJECT_NAME}" INTERFACE
96+
"$<$<CXX_COMPILER_ID:CLANG>:-lc++>"
8897
)
8998

9099
set(CONFIG_FILE_NAME "${PROJECT_NAME}Config.cmake")

Include/jsonifier/Allocator.hpp

+30-23
Original file line numberDiff line numberDiff line change
@@ -28,50 +28,57 @@
2828
#include <memory_resource>
2929
#include <utility>
3030

31-
#if defined(_MSC_VER)
32-
#define ALIGNED_ALLOC(size, alignment) _aligned_malloc(size, alignment)
33-
#define ALIGNED_FREE(ptr) _aligned_free(ptr)
34-
#else
35-
#define ALIGNED_ALLOC(size, alignment) aligned_alloc(alignment, size)
36-
#define ALIGNED_FREE(ptr) free(ptr)
37-
#endif
38-
3931
#if defined T_AVX512
4032
#define ALIGNMENT 64
4133
#elif defined T_AVX2
4234
#define ALIGNMENT 32
4335
#elif defined T_AVX
4436
#define ALIGNMENT 16
4537
#else
46-
#define ALIGNMENT 8
38+
#define ALIGNMENT 16
4739
#endif
4840

41+
inline uint64_t findNextClosestMultiple(uint64_t number) {
42+
if constexpr (ALIGNMENT == 0) {
43+
return 0;
44+
}
45+
46+
uint64_t remainder = number % ALIGNMENT;
47+
if (remainder == 0) {
48+
return number;
49+
}
50+
51+
uint64_t nextMultiple = number + (ALIGNMENT - remainder);
52+
return nextMultiple;
53+
}
54+
4955
namespace JsonifierInternal {
5056

51-
template<typename ValueType> class AlignedAllocator {
57+
template<typename ValueType> class AlignedAllocator : public std::pmr::polymorphic_allocator<ValueType> {
5258
public:
5359
using value_type = ValueType;
5460
using pointer = value_type*;
5561
using size_type = uint64_t;
62+
using allocator = std::pmr::polymorphic_allocator<ValueType>;
5663

57-
inline pointer allocate(size_type n) const {
64+
inline pointer allocate(size_type n) {
5865
if (n == 0) {
5966
return nullptr;
6067
}
61-
return static_cast<pointer>(ALIGNED_ALLOC(n * sizeof(ValueType), ALIGNMENT));
68+
return static_cast<ValueType*>(allocator::allocate_bytes(findNextClosestMultiple(n * sizeof(value_type)), ALIGNMENT));
6269
}
6370

64-
inline void deallocate(pointer p, size_type) const {
65-
if (p) {
66-
ALIGNED_FREE(p);
71+
inline void deallocate(pointer ptr, size_type n) {
72+
if (ptr) {
73+
allocator::deallocate_bytes(ptr, findNextClosestMultiple(n * sizeof(value_type)), ALIGNMENT);
6774
}
6875
}
6976

70-
template<typename... Args> inline void construct(pointer p, Args&&... args) const {
77+
template<typename... Args> inline void construct(pointer p, Args&&... args) {
7178
new (p) value_type(std::forward<Args>(args)...);
7279
}
7380

74-
inline void destroy(pointer p) const {
81+
inline void destroy(pointer p) {
7582
p->~value_type();
7683
}
7784
};
@@ -81,24 +88,24 @@ namespace JsonifierInternal {
8188
using value_type = ValueType;
8289
using pointer = value_type*;
8390
using size_type = uint64_t;
84-
using allocator = const AlignedAllocator<value_type>;
85-
using allocator_traits = const std::allocator_traits<allocator>;
91+
using allocator = AlignedAllocator<value_type>;
92+
using allocator_traits = std::allocator_traits<allocator>;
8693

8794
inline AllocWrapper(){};
8895

89-
inline pointer allocate(size_type count) const {
96+
inline pointer allocate(size_type count) {
9097
return allocator_traits::allocate(*this, count);
9198
}
9299

93-
inline void deallocate(pointer ptr, size_type count) const {
100+
inline void deallocate(pointer ptr, size_type count) {
94101
allocator_traits::deallocate(*this, ptr, count);
95102
}
96103

97-
template<typename... Args> inline void construct(pointer ptr, Args&&... args) const {
104+
template<typename... Args> inline void construct(pointer ptr, Args&&... args) {
98105
allocator_traits::construct(*this, ptr, std::forward<Args>(args)...);
99106
}
100107

101-
inline void destroy(pointer ptr) const {
108+
inline void destroy(pointer ptr) {
102109
allocator_traits::destroy(*this, ptr);
103110
}
104111

Include/jsonifier/Base.hpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ namespace JsonifierInternal {
5454

5555
class Serializer;
5656

57-
template<typename T> struct always_false : std::false_type {};
57+
template<typename ValueType> struct always_false : std::false_type {};
5858

59-
template<typename T> constexpr bool always_false_v = always_false<T>::value;
59+
template<typename ValueType> constexpr bool always_false_v = always_false<ValueType>::value;
6060

61-
template<typename T> void printTypeInCompilationError(T&&) noexcept {
62-
static_assert(always_false_v<T>, "Compilation failed because you failed to specialize the Core<> template for the following class:");
61+
template<typename ValueType> void printTypeInCompilationError(ValueType&&) noexcept {
62+
static_assert(always_false_v<ValueType>, "Compilation failed because you failed to specialize the Core<> template for the following class:");
6363
}
6464

6565
template<typename ValueType = void> struct Hash {
@@ -106,8 +106,10 @@ namespace JsonifierInternal {
106106
};
107107

108108
template<typename ValueType>
109-
concept VectorSubscriptable = requires(ValueType value) {
110-
{ value[std::size_t{}] } -> std::same_as<typename ValueType::value_type&>;
109+
concept VectorSubscriptable = requires(ValueType value, uint64_t index) {
110+
{ value[index] } -> std::same_as<typename std::remove_reference_t<ValueType>::reference>;
111+
} || requires(ValueType value, uint64_t index) {
112+
{ value[index] } -> std::same_as<typename std::remove_reference_t<ValueType>::const_reference>;
111113
};
112114

113115
template<typename ValueType>
@@ -228,12 +230,12 @@ namespace JsonifierInternal {
228230
};
229231
}
230232

231-
template<std::size_t... Is> struct seq {};
232-
template<std::size_t Count, std::size_t... Is> struct gen_seq : gen_seq<Count - 1, Count - 1, Is...> {};
233-
template<std::size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};
233+
template<std::size_t... Is> struct Sequence {};
234+
template<std::size_t Count, std::size_t... Is> struct GenSeq : GenSeq<Count - 1, Count - 1, Is...> {};
235+
template<std::size_t... Is> struct GenSeq<0, Is...> : Sequence<Is...> {};
234236

235237
template<std::size_t N1, std::size_t... I1, std::size_t N2, std::size_t... I2>
236-
constexpr std::array<const char, N1 + N2 - 1> concat(const char (&a1)[N1], const char (&a2)[N2], seq<I1...>, seq<I2...>) {
238+
constexpr std::array<const char, N1 + N2 - 1> concat(const char (&a1)[N1], const char (&a2)[N2], Sequence<I1...>, Sequence<I2...>) {
237239
return { { a1[I1]..., a2[I2]... } };
238240
}
239241

@@ -267,7 +269,7 @@ namespace JsonifierInternal {
267269

268270
template<std::size_t n, typename Func> constexpr auto forEach(Func&& f) {
269271
return indexer<n>()([&](auto&&... i) {
270-
(std::forward<Func>(f)(i), ...);
272+
(std::forward<RefUnwrap<Func>>(f)(i), ...);
271273
});
272274
}
273275

0 commit comments

Comments
 (0)