Skip to content

Commit

Permalink
Turn JSON into a plain enum (#1380)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti authored Dec 11, 2024
1 parent e7ea274 commit ffe0b09
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 80 deletions.
1 change: 0 additions & 1 deletion benchmark/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ static void JSON_Fast_Hash_Helm_Chart_Lock(benchmark::State &state) {

for (auto _ : state) {
auto result{document.fast_hash()};
assert(result >= 0);
benchmark::DoNotOptimize(result);
}
}
Expand Down
30 changes: 25 additions & 5 deletions src/json/include/sourcemeta/jsontoolkit/json_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
#include <string> // std::basic_string, std::char_traits
#include <string_view> // std::basic_string_view
#include <type_traits> // std::enable_if_t, std::is_same_v
#include <utility> // std::in_place_type, std::pair
#include <variant> // std::variant
#include <utility> // std::pair

namespace sourcemeta::jsontoolkit {

Expand Down Expand Up @@ -83,7 +82,9 @@ class SOURCEMETA_JSONTOOLKIT_JSON_EXPORT JSON {
// On some systems, `std::int64_t` might be equal to `long`
template <typename T = std::int64_t,
typename = std::enable_if_t<!std::is_same_v<T, std::int64_t>>>
explicit JSON(const long value) : data{std::in_place_type<Integer>, value} {}
explicit JSON(const long value) : current_type{Type::Integer} {
this->data_integer = value;
}

/// This constructor creates a JSON document from an real number type. For
/// example:
Expand Down Expand Up @@ -189,6 +190,15 @@ class SOURCEMETA_JSONTOOLKIT_JSON_EXPORT JSON {
/// A copy constructor for the object type.
explicit JSON(const Object &value);

/// Misc constructors
JSON(const JSON &);
JSON(JSON &&);
auto operator=(const JSON &) -> JSON &;
auto operator=(JSON &&) -> JSON &;

/// Destructor
~JSON();

/// This function creates an empty JSON array. For example:
///
/// ```cpp
Expand Down Expand Up @@ -1286,7 +1296,7 @@ class SOURCEMETA_JSONTOOLKIT_JSON_EXPORT JSON {
auto erase_keys(Iterator first, Iterator last) -> void {
assert(this->is_object());
for (auto iterator = first; iterator != last; ++iterator) {
std::get_if<Object>(&this->data)->data.erase(*iterator);
this->data_object.data.erase(*iterator);
}
}

Expand Down Expand Up @@ -1488,16 +1498,26 @@ class SOURCEMETA_JSONTOOLKIT_JSON_EXPORT JSON {
auto into_object() -> void;

private:
Type current_type = Type::Null;

// Exporting symbols that depends on the standard C++ library is considered
// safe.
// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
#if defined(_MSC_VER)
#pragma warning(disable : 4251)
#endif
std::variant<std::nullptr_t, bool, Integer, Real, String, Array, Object> data;
union {
bool data_boolean;
Integer data_integer;
Real data_real;
String data_string;
Array data_array;
Object data_object;
};
#if defined(_MSC_VER)
#pragma warning(default : 4251)
#endif
auto maybe_destruct_union() -> void;
};

} // namespace sourcemeta::jsontoolkit
Expand Down
Loading

4 comments on commit ffe0b09

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (macos/llvm)

Benchmark suite Current: ffe0b09 Previous: e7ea274 Ratio
JSON_Array_Of_Objects_Unique 322.1279253704097 ns/iter 308.57873895075977 ns/iter 1.04
JSON_Parse_1 20826.00294886635 ns/iter 19769.150557775934 ns/iter 1.05
JSON_Fast_Hash_Helm_Chart_Lock 46.489101866022104 ns/iter 45.80387021559119 ns/iter 1.01
JSON_Equality_Helm_Chart_Lock 109.76860320899456 ns/iter 115.58033701457038 ns/iter 0.95
Regex_Lower_S_Or_Upper_S_Asterisk 1.5860271459969546 ns/iter 1.5688516040790224 ns/iter 1.01
Regex_Caret_Lower_S_Or_Upper_S_Asterisk_Dollar 1.575334964298531 ns/iter 1.5854036958874125 ns/iter 0.99
Regex_Period_Asterisk 1.5732463220696382 ns/iter 1.5748275530902953 ns/iter 1.00
Regex_Group_Period_Asterisk_Group 1.570154977643591 ns/iter 1.5907038852136621 ns/iter 0.99
Regex_Period_Plus 1.9092873763019267 ns/iter 1.9731027220657 ns/iter 0.97
Regex_Period 1.911537111739421 ns/iter 1.968931466844463 ns/iter 0.97
Regex_Caret_Period_Plus_Dollar 1.9332336102068712 ns/iter 1.8829373905111304 ns/iter 1.03
Regex_Caret_Group_Period_Plus_Group_Dollar 1.9261274878779622 ns/iter 1.883176559467659 ns/iter 1.02
Regex_Caret_Period_Asterisk_Dollar 1.5783627441741397 ns/iter 1.5833791119272171 ns/iter 1.00
Regex_Caret_Group_Period_Asterisk_Group_Dollar 1.5879733547929635 ns/iter 1.638566650884203 ns/iter 0.97
Regex_Caret_X_Hyphen 6.2705038763787755 ns/iter 6.345826331412898 ns/iter 0.99
Regex_Period_Md_Dollar 67.6214659709654 ns/iter 67.35529485117756 ns/iter 1.00
Regex_Caret_Slash_Period_Asterisk 5.400639352903772 ns/iter 5.531656444812192 ns/iter 0.98
Regex_Caret_Period_Range_Dollar 2.052257292155492 ns/iter 2.14911227490561 ns/iter 0.95
Regex_Nested_Backtrack 706.9144535901468 ns/iter 710.940458224336 ns/iter 0.99
Pointer_Object_Traverse 20.29677959468358 ns/iter 23.201862220966287 ns/iter 0.87
Pointer_Object_Try_Traverse 28.594820022041752 ns/iter 37.050729496199196 ns/iter 0.77

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (linux/llvm)

Benchmark suite Current: ffe0b09 Previous: e7ea274 Ratio
JSON_Array_Of_Objects_Unique 469.820556891455 ns/iter 391.28594254924474 ns/iter 1.20
JSON_Parse_1 31168.16991804876 ns/iter 29823.4834094885 ns/iter 1.05
JSON_Fast_Hash_Helm_Chart_Lock 54.50361749689677 ns/iter 50.44675418537584 ns/iter 1.08
JSON_Equality_Helm_Chart_Lock 156.25235565918257 ns/iter 161.2903375049744 ns/iter 0.97
Regex_Lower_S_Or_Upper_S_Asterisk 2.1680042673408533 ns/iter 3.0990811330706967 ns/iter 0.70
Regex_Caret_Lower_S_Or_Upper_S_Asterisk_Dollar 2.4757007792382613 ns/iter 2.7853248226924023 ns/iter 0.89
Regex_Period_Asterisk 2.167902753889384 ns/iter 2.798368238400802 ns/iter 0.77
Regex_Group_Period_Asterisk_Group 2.4762624077767774 ns/iter 2.787406557334701 ns/iter 0.89
Regex_Period_Plus 2.4812189020189797 ns/iter 3.0951229184894826 ns/iter 0.80
Regex_Period 2.4738043828999334 ns/iter 2.784875298059981 ns/iter 0.89
Regex_Caret_Period_Plus_Dollar 2.4943364934283236 ns/iter 2.784350978242394 ns/iter 0.90
Regex_Caret_Group_Period_Plus_Group_Dollar 2.47428079972202 ns/iter 2.796881284331452 ns/iter 0.88
Regex_Caret_Period_Asterisk_Dollar 3.403900992841814 ns/iter 2.7856231572957557 ns/iter 1.22
Regex_Caret_Group_Period_Asterisk_Group_Dollar 3.4023723745211325 ns/iter 2.786357325161827 ns/iter 1.22
Regex_Caret_X_Hyphen 10.213737212913028 ns/iter 6.802870429518718 ns/iter 1.50
Regex_Period_Md_Dollar 104.884835780387 ns/iter 106.22016873242968 ns/iter 0.99
Regex_Caret_Slash_Period_Asterisk 5.260733436196487 ns/iter 6.506077910837412 ns/iter 0.81
Regex_Caret_Period_Range_Dollar 2.786867467237996 ns/iter 4.366173864073805 ns/iter 0.64
Regex_Nested_Backtrack 811.6041808241575 ns/iter 827.991411731457 ns/iter 0.98
Pointer_Object_Traverse 37.761299209220674 ns/iter 39.02216036289055 ns/iter 0.97
Pointer_Object_Try_Traverse 55.03344647534025 ns/iter 55.10104116790542 ns/iter 1.00

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (linux/gcc)

Benchmark suite Current: ffe0b09 Previous: e7ea274 Ratio
Pointer_Object_Traverse 44.712560560195804 ns/iter 53.21217770775975 ns/iter 0.84
Pointer_Object_Try_Traverse 23.19958340562648 ns/iter 26.38765542057277 ns/iter 0.88
Regex_Lower_S_Or_Upper_S_Asterisk 1.547383359311777 ns/iter 1.261695068086196 ns/iter 1.23
Regex_Caret_Lower_S_Or_Upper_S_Asterisk_Dollar 1.2398170817250869 ns/iter 1.2376915547481449 ns/iter 1.00
Regex_Period_Asterisk 1.5463786745313208 ns/iter 1.2381036815487838 ns/iter 1.25
Regex_Group_Period_Asterisk_Group 1.2378858424750698 ns/iter 1.2368191937578856 ns/iter 1.00
Regex_Period_Plus 1.546362864917774 ns/iter 1.2377140159469742 ns/iter 1.25
Regex_Period 1.2400608428386886 ns/iter 1.2598258158881097 ns/iter 0.98
Regex_Caret_Period_Plus_Dollar 1.5466830514848755 ns/iter 1.2384658851126924 ns/iter 1.25
Regex_Caret_Group_Period_Plus_Group_Dollar 1.2372191824962855 ns/iter 1.2391676420676403 ns/iter 1.00
Regex_Caret_Period_Asterisk_Dollar 1.5585593446025285 ns/iter 1.2388437676655542 ns/iter 1.26
Regex_Caret_Group_Period_Asterisk_Group_Dollar 1.2371340426279003 ns/iter 1.2493461426255974 ns/iter 0.99
Regex_Caret_X_Hyphen 12.057839753779396 ns/iter 12.054412431190771 ns/iter 1.00
Regex_Period_Md_Dollar 94.46817603619532 ns/iter 93.34389601040924 ns/iter 1.01
Regex_Caret_Slash_Period_Asterisk 4.654534883325085 ns/iter 4.332451050013245 ns/iter 1.07
Regex_Caret_Period_Range_Dollar 1.5454014273996084 ns/iter 1.5511562707712843 ns/iter 1.00
Regex_Nested_Backtrack 852.6173952988972 ns/iter 845.4990151789632 ns/iter 1.01
JSON_Array_Of_Objects_Unique 392.7676706561385 ns/iter 398.1123250665627 ns/iter 0.99
JSON_Parse_1 34067.237479885414 ns/iter 32110.711968012012 ns/iter 1.06
JSON_Fast_Hash_Helm_Chart_Lock 68.30136763808106 ns/iter 68.98532624285812 ns/iter 0.99
JSON_Equality_Helm_Chart_Lock 148.46528540589483 ns/iter 175.30362908791977 ns/iter 0.85

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (windows/msvc)

Benchmark suite Current: ffe0b09 Previous: e7ea274 Ratio
JSON_Array_Of_Objects_Unique 509.6541000000343 ns/iter 456.42705854633914 ns/iter 1.12
JSON_Parse_1 76297.70089285464 ns/iter 75384.34151785266 ns/iter 1.01
JSON_Fast_Hash_Helm_Chart_Lock 67.85151785714609 ns/iter 60.67435714285807 ns/iter 1.12
JSON_Equality_Helm_Chart_Lock 263.84244035013154 ns/iter 276.95112156469173 ns/iter 0.95
Regex_Lower_S_Or_Upper_S_Asterisk 5.953114955357108 ns/iter 6.2671348214290585 ns/iter 0.95
Regex_Caret_Lower_S_Or_Upper_S_Asterisk_Dollar 5.324256249999896 ns/iter 5.366936607143 ns/iter 0.99
Regex_Period_Asterisk 5.333212775032447 ns/iter 5.363696428571326 ns/iter 0.99
Regex_Group_Period_Asterisk_Group 5.48671000000013 ns/iter 5.509066071428695 ns/iter 1.00
Regex_Period_Plus 5.67491964285795 ns/iter 5.6798401785716885 ns/iter 1.00
Regex_Period 5.66839553571441 ns/iter 5.6852299107136774 ns/iter 1.00
Regex_Caret_Period_Plus_Dollar 5.827448214285523 ns/iter 5.63987857142832 ns/iter 1.03
Regex_Caret_Group_Period_Plus_Group_Dollar 5.67032589285686 ns/iter 5.914242857142784 ns/iter 0.96
Regex_Caret_Period_Asterisk_Dollar 5.283279464285735 ns/iter 5.466927999999598 ns/iter 0.97
Regex_Caret_Group_Period_Asterisk_Group_Dollar 5.6719174107135375 ns/iter 5.500948214285627 ns/iter 1.03
Regex_Caret_X_Hyphen 13.024953571429268 ns/iter 12.832633871282825 ns/iter 1.01
Regex_Period_Md_Dollar 145.9693662513744 ns/iter 139.33650717248278 ns/iter 1.05
Regex_Caret_Slash_Period_Asterisk 8.698059336170292 ns/iter 9.136420758928715 ns/iter 0.95
Regex_Caret_Period_Range_Dollar 6.010829464285296 ns/iter 5.949794642857244 ns/iter 1.01
Regex_Nested_Backtrack 612.06714285709 ns/iter 587.7375892858083 ns/iter 1.04
Pointer_Object_Traverse 34.133408198809505 ns/iter 33.24826666514845 ns/iter 1.03
Pointer_Object_Try_Traverse 44.39175000000262 ns/iter 47.517547003231115 ns/iter 0.93

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.