Skip to content

Commit 428e401

Browse files
authored
Speed up linter performance (when not auto-fixing) (#601)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent c828f79 commit 428e401

File tree

106 files changed

+4253
-1631
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+4253
-1631
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Brewfile.lock.json
1313
.DS_Store
1414
*.snap
1515
/node_modules
16+
/.cache

DEPENDENCIES

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
vendorpull https://github.com/sourcemeta/vendorpull 1dcbac42809cf87cb5b045106b863e17ad84ba02
2-
core https://github.com/sourcemeta/core 94ff49bd58ca63c5e5a7fb2435b3bc72872517c4
3-
jsonbinpack https://github.com/sourcemeta/jsonbinpack 8fae212dc7ec02af4bb0cd4e7fccd42a2471f1c1
4-
blaze https://github.com/sourcemeta/blaze 8dba65f8aebfe1ac976168b76e01c20dd406c517
2+
core https://github.com/sourcemeta/core 376f397db98e72be48d8401e2fbf2ee96f033966
3+
jsonbinpack https://github.com/sourcemeta/jsonbinpack 0c2340990bf31c630155991a93306990d9d94fd4
4+
blaze https://github.com/sourcemeta/blaze 93342104a85814bc0fd11792d305c4e83de259c0
55
hydra https://github.com/sourcemeta/hydra c86d2165a2f27f838837af1a5af24b1055a35317
66
ctrf https://github.com/ctrf-io/ctrf 93ea827d951390190171d37443bff169cf47c808

src/command_inspect.cc

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ auto print_frame(std::ostream &stream,
5050
}
5151

5252
stream << " Root : "
53-
<< location.second.root.value_or("<ANONYMOUS>") << "\n";
53+
<< (frame.root().empty() ? "<ANONYMOUS>" : frame.root()) << "\n";
5454

5555
if (location.second.pointer.empty()) {
5656
stream << " Pointer :\n";
@@ -60,7 +60,8 @@ auto print_frame(std::ostream &stream,
6060
stream << "\n";
6161
}
6262

63-
const auto position{positions.get(location.second.pointer)};
63+
const auto position{
64+
positions.get(sourcemeta::core::to_pointer(location.second.pointer))};
6465
if (position.has_value()) {
6566
stream << " File Position : " << std::get<0>(position.value())
6667
<< ":" << std::get<1>(position.value()) << "\n";
@@ -70,17 +71,19 @@ auto print_frame(std::ostream &stream,
7071

7172
stream << " Base : " << location.second.base << "\n";
7273

73-
if (location.second.relative_pointer.empty()) {
74+
const auto relative_pointer{
75+
location.second.pointer.slice(location.second.relative_pointer)};
76+
if (relative_pointer.empty()) {
7477
stream << " Relative Pointer :\n";
7578
} else {
7679
stream << " Relative Pointer : ";
77-
sourcemeta::core::stringify(location.second.relative_pointer, stream);
80+
sourcemeta::core::stringify(relative_pointer, stream);
7881
stream << "\n";
7982
}
8083

8184
stream << " Dialect : " << location.second.dialect << "\n";
82-
stream << " Base Dialect : " << location.second.base_dialect
83-
<< "\n";
85+
stream << " Base Dialect : "
86+
<< sourcemeta::core::to_string(location.second.base_dialect) << "\n";
8487

8588
if (location.second.parent.has_value()) {
8689
if (location.second.parent.value().empty()) {
@@ -114,7 +117,8 @@ auto print_frame(std::ostream &stream,
114117
stream << " Type : Dynamic\n";
115118
}
116119

117-
const auto position{positions.get(reference.first.second)};
120+
const auto position{
121+
positions.get(sourcemeta::core::to_pointer(reference.first.second))};
118122
if (position.has_value()) {
119123
stream << " File Position : " << std::get<0>(position.value())
120124
<< ":" << std::get<1>(position.value()) << "\n";
@@ -125,7 +129,8 @@ auto print_frame(std::ostream &stream,
125129
stream << " Destination : " << reference.second.destination
126130
<< "\n";
127131
stream << " - (w/o fragment) : "
128-
<< reference.second.base.value_or("<NONE>") << "\n";
132+
<< (reference.second.base.empty() ? "<NONE>" : reference.second.base)
133+
<< "\n";
129134
stream << " - (fragment) : "
130135
<< reference.second.fragment.value_or("<NONE>") << "\n";
131136
}
@@ -157,17 +162,17 @@ auto sourcemeta::jsonschema::inspect(const sourcemeta::core::Options &options)
157162
const auto identifier{
158163
sourcemeta::core::identify(schema, custom_resolver, dialect)};
159164

160-
frame.analyse(
161-
schema, sourcemeta::core::schema_walker, custom_resolver, dialect,
162-
163-
// Only use the file-based URI if the schema has no identifier,
164-
// as otherwise we make the output unnecessarily hard when it
165-
// comes to debugging schemas
166-
identifier.has_value()
167-
? std::optional<sourcemeta::core::JSON::String>(std::nullopt)
168-
: sourcemeta::core::URI::from_path(
169-
sourcemeta::core::weakly_canonical(schema_path))
170-
.recompose());
165+
frame.analyse(schema, sourcemeta::core::schema_walker, custom_resolver,
166+
dialect,
167+
168+
// Only use the file-based URI if the schema has no
169+
// identifier, as otherwise we make the output unnecessarily
170+
// hard when it comes to debugging schemas
171+
!identifier.empty()
172+
? ""
173+
: sourcemeta::core::URI::from_path(
174+
sourcemeta::core::weakly_canonical(schema_path))
175+
.recompose());
171176
} catch (
172177
const sourcemeta::core::SchemaRelativeMetaschemaResolutionError &error) {
173178
throw FileError<sourcemeta::core::SchemaRelativeMetaschemaResolutionError>(

src/command_lint.cc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,24 +155,21 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
155155
}
156156

157157
if (options.contains("list")) {
158-
std::vector<std::pair<std::reference_wrapper<const std::string>,
159-
std::reference_wrapper<const std::string>>>
160-
rules;
158+
std::vector<std::pair<std::string_view, std::string_view>> rules;
161159
for (const auto &entry : bundle) {
162160
rules.emplace_back(entry->name(), entry->message());
163161
}
164162

165-
std::sort(rules.begin(), rules.end(),
166-
[](const auto &left, const auto &right) {
167-
return left.first.get() < right.first.get() ||
168-
(left.first.get() == right.first.get() &&
169-
left.second.get() < right.second.get());
170-
});
163+
std::sort(
164+
rules.begin(), rules.end(), [](const auto &left, const auto &right) {
165+
return left.first < right.first ||
166+
(left.first == right.first && left.second < right.second);
167+
});
171168

172169
std::size_t count{0};
173170
for (const auto &entry : rules) {
174-
std::cout << entry.first.get() << "\n";
175-
std::cout << " " << entry.second.get() << "\n\n";
171+
std::cout << entry.first << "\n";
172+
std::cout << " " << entry.second << "\n\n";
176173
count += 1;
177174
}
178175

src/command_metaschema.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ auto sourcemeta::jsonschema::metaschema(
4747
try {
4848
const auto dialect{
4949
sourcemeta::core::dialect(entry.second, default_dialect_option)};
50-
if (!dialect) {
50+
if (dialect.empty()) {
5151
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
5252
entry.first);
5353
}
@@ -62,27 +62,27 @@ auto sourcemeta::jsonschema::metaschema(
6262
frame.analyse(bundled, sourcemeta::core::schema_walker, custom_resolver,
6363
default_dialect_option);
6464

65-
if (!cache.contains(dialect.value())) {
65+
if (!cache.contains(std::string{dialect})) {
6666
const auto metaschema_template{sourcemeta::blaze::compile(
6767
bundled, sourcemeta::core::schema_walker, custom_resolver,
6868
sourcemeta::blaze::default_schema_compiler, frame,
6969
sourcemeta::blaze::Mode::Exhaustive, default_dialect_option)};
70-
cache.insert({dialect.value(), metaschema_template});
70+
cache.insert({std::string{dialect}, metaschema_template});
7171
}
7272

7373
if (trace) {
7474
sourcemeta::blaze::TraceOutput output{
7575
sourcemeta::core::schema_walker, custom_resolver,
7676
sourcemeta::core::empty_weak_pointer, frame};
77-
result = evaluator.validate(cache.at(dialect.value()), entry.second,
78-
std::ref(output));
77+
result = evaluator.validate(cache.at(std::string{dialect}),
78+
entry.second, std::ref(output));
7979
print(output, entry.positions, std::cout);
8080
} else if (json_output) {
8181
// Otherwise its impossible to correlate the output
8282
// when validating i.e. a directory of schemas
8383
std::cerr << entry.first.string() << "\n";
8484
const auto output{sourcemeta::blaze::standard(
85-
evaluator, cache.at(dialect.value()), entry.second,
85+
evaluator, cache.at(std::string{dialect}), entry.second,
8686
sourcemeta::blaze::StandardOutput::Basic, entry.positions)};
8787
assert(output.is_object());
8888
assert(output.defines("valid"));
@@ -95,12 +95,12 @@ auto sourcemeta::jsonschema::metaschema(
9595
std::cout << "\n";
9696
} else {
9797
sourcemeta::blaze::SimpleOutput output{entry.second};
98-
if (evaluator.validate(cache.at(dialect.value()), entry.second,
98+
if (evaluator.validate(cache.at(std::string{dialect}), entry.second,
9999
std::ref(output))) {
100100
LOG_VERBOSE(options)
101101
<< "ok: "
102102
<< sourcemeta::core::weakly_canonical(entry.first).string()
103-
<< "\n matches " << dialect.value() << "\n";
103+
<< "\n matches " << dialect << "\n";
104104
} else {
105105
std::cerr << "fail: "
106106
<< sourcemeta::core::weakly_canonical(entry.first).string()

src/command_test.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
#include <sourcemeta/core/json.h>
55

6-
#include <chrono> // std::chrono
7-
#include <cstdlib> // EXIT_FAILURE
8-
#include <iostream> // std::cerr, std::cout
9-
#include <sstream> // std::ostringstream
10-
#include <string> // std::string
11-
#include <thread> // std::this_thread
12-
#include <vector> // std::vector
6+
#include <chrono> // std::chrono
7+
#include <cstdlib> // EXIT_FAILURE
8+
#include <iostream> // std::cerr, std::cout
9+
#include <sstream> // std::ostringstream
10+
#include <string> // std::string
11+
#include <string_view> // std::string_view
12+
#include <thread> // std::this_thread
1313

1414
#include "command.h"
1515
#include "configuration.h"
@@ -24,8 +24,8 @@ namespace {
2424

2525
auto parse_test_suite(const sourcemeta::jsonschema::InputJSON &entry,
2626
const sourcemeta::core::SchemaResolver &schema_resolver,
27-
const std::optional<std::string> &dialect,
28-
const bool json_output) -> sourcemeta::blaze::TestSuite {
27+
const std::string_view dialect, const bool json_output)
28+
-> sourcemeta::blaze::TestSuite {
2929
try {
3030
return sourcemeta::blaze::TestSuite::parse(
3131
entry.second, entry.positions, entry.first.parent_path(),

src/command_validate.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
#include <sourcemeta/blaze/evaluator.h>
99
#include <sourcemeta/blaze/output.h>
1010

11-
#include <chrono> // std::chrono
12-
#include <cmath> // std::sqrt
13-
#include <iostream> // std::cerr
14-
#include <string> // std::string
11+
#include <chrono> // std::chrono
12+
#include <cmath> // std::sqrt
13+
#include <iostream> // std::cerr
14+
#include <string> // std::string
15+
#include <string_view> // std::string_view
1516

1617
#include "command.h"
1718
#include "configuration.h"
@@ -36,8 +37,8 @@ auto get_precompiled_schema_template_path(
3637
auto get_schema_template(const sourcemeta::core::JSON &bundled,
3738
const sourcemeta::core::SchemaResolver &resolver,
3839
const sourcemeta::core::SchemaFrame &frame,
39-
const std::optional<std::string> &default_dialect,
40-
const std::optional<std::string> &default_id,
40+
const std::string_view default_dialect,
41+
const std::string_view default_id,
4142
const bool fast_mode,
4243
const sourcemeta::core::Options &options)
4344
-> sourcemeta::blaze::Template {

src/error.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,11 @@ inline auto print_exception(const bool is_json, const Exception &exception)
298298
}
299299

300300
if constexpr (requires(const Exception &current) {
301-
{ current.option() } -> std::convertible_to<std::string>;
301+
{ current.option() } -> std::convertible_to<std::string_view>;
302302
}) {
303303
if (is_json) {
304-
error_json.assign("option", sourcemeta::core::JSON{exception.option()});
304+
error_json.assign(
305+
"option", sourcemeta::core::JSON{std::string{exception.option()}});
305306
} else {
306307
std::cerr << " at option " << exception.option() << "\n";
307308
}

src/resolver.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class CustomResolver {
8080
CustomResolver(
8181
const sourcemeta::core::Options &options,
8282
const std::optional<sourcemeta::core::SchemaConfig> &configuration,
83-
const bool remote, const std::optional<std::string> &default_dialect)
83+
const bool remote, const std::string_view default_dialect)
8484
: options_{options}, configuration_{configuration}, remote_{remote} {
8585
if (options.contains("resolve")) {
8686
for (const auto &entry : for_each_json(options.at("resolve"), options)) {
@@ -121,8 +121,8 @@ class CustomResolver {
121121
}
122122

123123
auto add(const sourcemeta::core::JSON &schema,
124-
const std::optional<std::string> &default_dialect = std::nullopt,
125-
const std::optional<std::string> &default_id = std::nullopt,
124+
const std::string_view default_dialect = "",
125+
const std::string_view default_id = "",
126126
const std::function<void(const sourcemeta::core::JSON::String &)>
127127
&callback = nullptr) -> bool {
128128
assert(sourcemeta::core::is_schema(schema));
@@ -223,12 +223,12 @@ class CustomResolver {
223223

224224
inline auto
225225
resolver(const sourcemeta::core::Options &options, const bool remote,
226-
const std::optional<std::string> &default_dialect,
226+
const std::string_view default_dialect,
227227
const std::optional<sourcemeta::core::SchemaConfig> &configuration)
228228
-> const CustomResolver & {
229-
using CacheKey = std::pair<bool, std::optional<std::string>>;
229+
using CacheKey = std::pair<bool, std::string>;
230230
static std::map<CacheKey, CustomResolver> resolver_cache;
231-
const CacheKey cache_key{remote, default_dialect};
231+
const CacheKey cache_key{remote, std::string{default_dialect}};
232232

233233
// Check if resolver is already cached
234234
auto iterator{resolver_cache.find(cache_key)};

src/utils.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,29 @@
88

99
#include <sourcemeta/blaze/output.h>
1010

11-
#include <cassert> // assert
12-
#include <iterator> // std::next
13-
#include <optional> // std::optional
14-
#include <ostream> // std::ostream
15-
#include <sstream> // std::ostringstream
16-
#include <string> // std::string, std::stoull
17-
#include <tuple> // std::get
18-
#include <variant> // std::visit
11+
#include <cassert> // assert
12+
#include <iterator> // std::next
13+
#include <optional> // std::optional
14+
#include <ostream> // std::ostream
15+
#include <string> // std::string, std::stoull
16+
#include <string_view> // std::string_view
17+
#include <tuple> // std::get
18+
#include <variant> // std::visit
1919

2020
namespace sourcemeta::jsonschema {
2121

2222
inline auto default_dialect(
2323
const sourcemeta::core::Options &options,
2424
const std::optional<sourcemeta::core::SchemaConfig> &configuration)
25-
-> std::optional<std::string> {
25+
-> std::string_view {
2626
if (options.contains("default-dialect")) {
27-
return std::string{options.at("default-dialect").front()};
28-
} else if (configuration.has_value()) {
29-
return configuration.value().default_dialect;
27+
return options.at("default-dialect").front();
28+
} else if (configuration.has_value() &&
29+
configuration.value().default_dialect.has_value()) {
30+
return configuration.value().default_dialect.value();
3031
}
3132

32-
return std::nullopt;
33+
return "";
3334
}
3435

3536
inline auto parse_indentation(const sourcemeta::core::Options &options)

0 commit comments

Comments
 (0)