Skip to content

Commit

Permalink
♻️ Rename allocator classes
Browse files Browse the repository at this point in the history
  • Loading branch information
yosh-matsuda committed Feb 24, 2023
1 parent 54ac851 commit e8db98f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 51 deletions.
65 changes: 33 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

Ultra-fast and intuitive C++ JSON reader/writer with yyjson backend.

1. [Features](#features)
2. [Requirements](#requirements)
3. [Overview](#overview)
1. [JSON Reader](#json-reader)
2. [JSON Writer](#json-writer)
3. [Serialization and Deserialization](#serialization-and-deserialization)
4. [Installation](#installation)
1. [Using CMake](#using-cmake)
5. [Benchmark](#benchmark)
1. [Read performance](#read-performance)
2. [Write performance](#write-performance)
6. [Reference](#reference)
1. [Namespaces](#namespaces)
2. [Immutable JSON classes](#immutable-json-classes)
3. [Mutable JSON classes](#mutable-json-classes)
4. [Serialize and deserialize JSON](#serialize-and-deserialize-json)
5. [Performance best practices](#performance-best-practices)
6. [Misc](#misc)
7. [Author](#author)
1. [Features](#features)
2. [Requirements](#requirements)
3. [Overview](#overview)
1. [JSON Reader](#json-reader)
2. [JSON Writer](#json-writer)
3. [Serialization and Deserialization](#serialization-and-deserialization)
4. [Installation](#installation)
1. [Using CMake](#using-cmake)
5. [Benchmark](#benchmark)
1. [Read performance](#read-performance)
2. [Write performance](#write-performance)
6. [Reference](#reference)
1. [Namespaces](#namespaces)
2. [Immutable JSON classes](#immutable-json-classes)
3. [Mutable JSON classes](#mutable-json-classes)
4. [Serialize and deserialize JSON](#serialize-and-deserialize-json)
5. [Performance best practices](#performance-best-practices)
6. [Misc](#misc)
7. [Author](#author)

## Features

Expand Down Expand Up @@ -274,7 +274,8 @@ The `yyjson` namespace includes the following function and classes. Typically, y

| Function | |
| -------------- | ---------------------------------------------------------------------------------------------------------- |
| `yyjson::read` | Read a JSON string and return an *immutable* JSON document class which is alias of `yyjson::reader::value` |
| `yyjson::read` | Read a JSON s
tring and return an *immutable* JSON document class which is alias of `yyjson::reader::value` |

| Type | |
| ---------------- | -------------------------------------------------------------- |
Expand Down Expand Up @@ -322,22 +323,22 @@ If the read option has the [`ReadInsitu`](https://ibireme.github.io/yyjson/doc/d
This library provides the following two special allocators. Both are pool allocators and are useful for tasks that parse multiple JSON strings with a single buffer. You must be careful not to destroy the allocator or perform any modification operations on the allocator buffer while an instance of the JSON class is in use.
##### `yyjson::reader::allocator`
##### `yyjson::reader::pool_allocator`
This allocator is based on `std::vector` and has a buffer on the heap. The `read` function will reallocate the buffer if it is not large enough for the given JSON string.
This pool_allocator is based on `std::vector` and has a buffer on the heap. The `read` function will reallocate the buffer if it is not large enough for the given JSON string.
**Constructor**
```cpp
// Default constructors
allocator() = default;
allocator(const allocator&) = default;
allocator(allocator&&) noexcept = default;
pool_allocator() = default;
pool_allocator(const pool_allocator&) = default;
pool_allocator(pool_allocator&&) noexcept = default;
// Allocate specified bytes of buffer
explicit allocator(std::size_t size_byte);
explicit pool_allocator(std::size_t size_byte);
// Allocate a buffer large enough to read the specified JSON string with read flags
explicit allocator(std::string_view json, ReadFlag flag = ReadFlag::NoFlag);
explicit pool_allocator(std::string_view json, ReadFlag flag = ReadFlag::NoFlag);
```

**Member function**
Expand All @@ -359,17 +360,17 @@ void shrink_to_fit();
bool check_capacity(std::string_view json, ReadFlag = ReadFlag::NoFlag) const;
```
##### `yyjson::reader::stack_allocator<std::size_t Byte>`
##### `yyjson::reader::stack_pool_allocator<std::size_t Byte>`
This allocator is based on `std::array` and has a fixed buffer on the stack. The `check_capacity` function should be called to check if the buffer size is sufficient before using it because the `read` function cannot increase the size of the `stack_alloctor` buffer.
**Constructor**
```cpp
// Default constructors
stack_allocator() = default;
stack_allocator(const stack_allocator&) = default;
stack_allocator(stack_allocator&&) noexcept = default;
stack_pool_allocator() = default;
stack_pool_allocator(const stack_pool_allocator&) = default;
stack_pool_allocator(stack_pool_allocator&&) noexcept = default;
```

**Member function**
Expand Down Expand Up @@ -407,7 +408,7 @@ auto val = read(json_str, ReadFlag::AllowTrailingCommas);
// Read JSON string with heap allocator and ReadInsitu flag
auto json_str_insitu = fmt::format("{}{}", json_obj_str, std::string(YYJSON_PADDING_SIZE, '\0')); // Padded
auto heap_alloc = reader::allocator(); // Heap allocator can automatically expand the buffer
auto heap_alloc = reader::pool_allocator(); // Heap allocator can automatically expand the buffer
auto val = read(json_str_insitu, json_str.size(), heap_alloc, ReadFlag::ReadInsitu);
```

Expand Down
26 changes: 13 additions & 13 deletions include/cpp_yyjson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3148,10 +3148,10 @@ namespace yyjson
template <yyjson_allocator Alloc>
value read(char*, std::size_t, Alloc&, ReadFlag = ReadFlag::NoFlag);

class allocator
class pool_allocator
{
template <std::size_t N>
friend class stack_allocator;
friend class stack_pool_allocator;

struct alignas(alignof(char)) char_like
{
Expand All @@ -3169,11 +3169,11 @@ namespace yyjson
yyjson_alc alc_ = init_allocator();

public:
allocator() = default;
allocator(const allocator&) = default;
allocator(allocator&&) noexcept = default;
explicit allocator(std::size_t size_byte) : buf_(size_byte) {}
explicit allocator(std::string_view json, ReadFlag flag = ReadFlag::NoFlag)
pool_allocator() = default;
pool_allocator(const pool_allocator&) = default;
pool_allocator(pool_allocator&&) noexcept = default;
explicit pool_allocator(std::size_t size_byte) : buf_(size_byte) {}
explicit pool_allocator(std::string_view json, ReadFlag flag = ReadFlag::NoFlag)
: buf_(yyjson_read_max_memory_usage(json.size(), magic_enum::enum_integer(flag)))
{
}
Expand Down Expand Up @@ -3212,9 +3212,9 @@ namespace yyjson
};

template <std::size_t Byte>
class stack_allocator
class stack_pool_allocator
{
std::array<allocator::char_like, Byte> buf_;
std::array<pool_allocator::char_like, Byte> buf_;
yyjson_alc alc_ = init();
yyjson_alc init()
{
Expand All @@ -3227,9 +3227,9 @@ namespace yyjson
auto& get() & { return alc_; }
const auto& get() const& { return alc_; }
auto get() && { return std::move(alc_); }
stack_allocator() = default;
stack_allocator(const stack_allocator&) = default;
stack_allocator(stack_allocator&&) noexcept = default;
stack_pool_allocator() = default;
stack_pool_allocator(const stack_pool_allocator&) = default;
stack_pool_allocator(stack_pool_allocator&&) noexcept = default;
[[nodiscard]] constexpr auto size() const { return buf_.size(); }
[[nodiscard]] bool check_capacity(std::string_view json, ReadFlag flag = ReadFlag::NoFlag) const
{
Expand Down Expand Up @@ -3360,7 +3360,7 @@ namespace yyjson
if (!alc.check_capacity({str, len}, read_flag))
{
throw std::runtime_error(
fmt::format("Insufficient capacity in the allocator pool for {}", NAMEOF_TYPE(Alloc)));
fmt::format("Insufficient capacity in the pool allocator for {}", NAMEOF_TYPE(Alloc)));
}
}
result = yyjson_read_opts(str, len, magic_enum::enum_integer(read_flag), &alc.get(), &err);
Expand Down
6 changes: 3 additions & 3 deletions test/bench_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ void read_cpp_yyjson_single(benchmark::State& state)
{
using namespace yyjson;
const auto json = read_file(std::get<0>(json_files[state.range(0)]));
auto alloc = reader::allocator(json);
auto alloc = reader::pool_allocator(json);
for (auto _ : state)
{
auto counter = json_count();
Expand All @@ -648,7 +648,7 @@ void read_cpp_yyjson_insitu_single(benchmark::State& state)
{
using namespace yyjson;
const auto json = read_file(std::get<0>(json_files[state.range(0)]));
auto alloc = reader::allocator(json, ReadFlag::ReadInsitu);
auto alloc = reader::pool_allocator(json, ReadFlag::ReadInsitu);
for (auto _ : state)
{
auto counter = json_count();
Expand All @@ -675,7 +675,7 @@ void read_cpp_yyjson_insitu_single_copy(benchmark::State& state)
{
using namespace yyjson;
const auto json = read_file(std::get<0>(json_files[state.range(0)]));
auto alloc = reader::allocator(json, ReadFlag::ReadInsitu);
auto alloc = reader::pool_allocator(json, ReadFlag::ReadInsitu);
for (auto _ : state)
{
auto counter = json_count();
Expand Down
6 changes: 3 additions & 3 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2415,9 +2415,9 @@ TEST(Reader, Allocator)
using namespace std::literals::string_literals;
using namespace std::string_view_literals;

auto h_alloc = reader::allocator();
auto s_alloc_s = reader::stack_allocator<10>();
auto s_alloc_l = reader::stack_allocator<512>();
auto h_alloc = reader::pool_allocator();
auto s_alloc_s = reader::stack_pool_allocator<10>();
auto s_alloc_l = reader::stack_pool_allocator<512>();
auto result = std::string();

auto json_str = "[1,2,3,4,5]"sv;
Expand Down

0 comments on commit e8db98f

Please sign in to comment.