Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions benchmark/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#include <algorithm> // std::ranges
#include <cassert> // assert

#include <sourcemeta/core/io.h>
#include <sourcemeta/core/json.h>

#include <filesystem> // std::filesystem
#include <sstream> // std::ostringstream

static void JSON_Array_Of_Objects_Unique(benchmark::State &state) {
// From Unreal Engine `uproject` files
const auto document{sourcemeta::core::parse_json(R"JSON([
Expand Down Expand Up @@ -322,6 +326,20 @@ static void JSON_Parse_Decimal(benchmark::State &state) {
}
}

static void JSON_Parse_Schema_ISO_Language(benchmark::State &state) {
auto stream{sourcemeta::core::read_file(
std::filesystem::path{CURRENT_DIRECTORY} / "schemas" /
"2020_12_iso_language_2023_set_3.json")};
std::ostringstream buffer;
buffer << stream.rdbuf();
const auto schema{buffer.str()};
for (auto _ : state) {
auto result{sourcemeta::core::parse_json(schema)};
assert(result.is_object());
benchmark::DoNotOptimize(result);
}
}

static void JSON_Fast_Hash_Helm_Chart_Lock(benchmark::State &state) {
// From `helm-chart-lock`
const auto document{sourcemeta::core::parse_json(R"JSON({
Expand Down Expand Up @@ -543,6 +561,7 @@ BENCHMARK(JSON_Array_Of_Objects_Unique);
BENCHMARK(JSON_Parse_1);
BENCHMARK(JSON_Parse_Real);
BENCHMARK(JSON_Parse_Decimal);
BENCHMARK(JSON_Parse_Schema_ISO_Language);
BENCHMARK(JSON_Fast_Hash_Helm_Chart_Lock);
BENCHMARK(JSON_Equality_Helm_Chart_Lock);
BENCHMARK(JSON_String_Equal)->Args({10})->Args({100});
Expand Down
18 changes: 9 additions & 9 deletions src/core/json/include/sourcemeta/core/json_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ class SOURCEMETA_CORE_JSON_EXPORT JSON {
/// A set of types
using TypeSet = std::bitset<8>;

/// The context type for parse callbacks
enum class ParseContext : std::uint8_t { Root, Property, Index };

/// An optional callback that can be passed to parsing functions to obtain
/// metadata during the parsing process. Each subdocument will emit 2 events:
/// a "pre" and a "post". When parsing object and arrays, during the "pre"
/// event, the value corresponds to the property name or index, respectively.
using ParseCallback =
std::function<void(const ParsePhase phase, const Type type,
const std::uint64_t line, const std::uint64_t column,
// TODO: Instead of taking a JSON value, we should take
// either an index or a string view to the key
const JSON &value)>;
/// metadata during the parsing process
using ParseCallback = std::function<void(
const ParsePhase phase, const Type type, const std::uint64_t line,
const std::uint64_t column, const ParseContext context,
const std::size_t index, const StringView property)>;

/// A comparison function between object property keys.
/// See https://en.cppreference.com/w/cpp/named_req/Compare
using KeyComparison = std::function<bool(const String &, const String &)>;
Expand Down
169 changes: 81 additions & 88 deletions src/core/json/parser.h

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class SOURCEMETA_CORE_JSONPOINTER_EXPORT PointerPositionTracker {
std::tuple<std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t>;
auto operator()(const JSON::ParsePhase phase, const JSON::Type,
const std::uint64_t line, const std::uint64_t column,
const JSON &value) -> void;
const JSON::ParseContext context, const std::size_t index,
const JSON::StringView property) -> void;
[[nodiscard]] auto get(const Pointer &pointer) const
-> std::optional<Position>;
[[nodiscard]] auto size() const -> std::size_t;
Expand Down
29 changes: 19 additions & 10 deletions src/core/jsonpointer/position.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@

namespace sourcemeta::core {

auto PointerPositionTracker::operator()(const JSON::ParsePhase phase,
const JSON::Type,
const std::uint64_t line,
const std::uint64_t column,
const JSON &value) -> void {
auto PointerPositionTracker::operator()(
const JSON::ParsePhase phase, const JSON::Type, const std::uint64_t line,
const std::uint64_t column, const JSON::ParseContext context,
const std::size_t index, const JSON::StringView property) -> void {
if (phase == JSON::ParsePhase::Pre) {
this->stack.emplace(line, column);
if (value.is_string()) {
this->current.push_back(value.to_string());
} else if (value.is_integer()) {
this->current.push_back(
static_cast<Pointer::Token::Index>(value.to_integer()));
switch (context) {
case JSON::ParseContext::Property:
this->current.push_back(JSON::String{property});
break;
case JSON::ParseContext::Index:
this->current.push_back(index);
break;
case JSON::ParseContext::Root:
break;
}
} else if (phase == JSON::ParsePhase::Post) {
assert(!this->stack.empty());
Expand All @@ -35,16 +38,22 @@ auto PointerPositionTracker::operator()(const JSON::ParsePhase phase,

auto PointerPositionTracker::get(const Pointer &pointer) const
-> std::optional<Position> {
assert(this->stack.empty());
assert(this->current.empty());
const auto result{this->data.find(pointer)};
return result == this->data.cend() ? std::nullopt
: std::optional<Position>{result->second};
}

auto PointerPositionTracker::size() const -> std::size_t {
assert(this->stack.empty());
assert(this->current.empty());
return this->data.size();
}

auto PointerPositionTracker::to_json() const -> JSON {
assert(this->stack.empty());
assert(this->current.empty());
auto result{JSON::make_object()};
for (const auto &entry : this->data) {
result.assign_assume_new(to_string(entry.first),
Expand Down
Loading