Skip to content

Commit 484d55e

Browse files
committed
refactor(fe): use span_size instead of int for child indexes
Reduce narrow_cast-ing. On x86_64, this seems to have no performance penalty: Benchmark Time CPU Time Old Time New CPU Old CPU New -------------------------------------------------------------------------------------------------------------------------- benchmark_parse_file +0.0013 +0.0013 2093468 2096186 2093456 2096140 benchmark_parse_file -0.0018 -0.0018 2090347 2086606 2090317 2086594 benchmark_parse_file -0.0030 -0.0030 2093381 2087100 2093362 2087045 benchmark_parse_file -0.0038 -0.0038 2095640 2087710 2095594 2087659 benchmark_parse_file -0.0033 -0.0033 2093896 2087037 2093830 2087014 benchmark_parse_file -0.0001 -0.0001 2088977 2088818 2088963 2088754 benchmark_parse_file -0.0040 -0.0040 2094990 2086705 2094951 2086637 benchmark_parse_file -0.0002 -0.0002 2096330 2095965 2096290 2095919 benchmark_parse_file +0.0001 +0.0001 2098723 2098967 2098694 2098919 benchmark_parse_file +0.0001 +0.0001 2097609 2097810 2097591 2097793 benchmark_parse_file_pvalue 0.2123 0.2123 U Test, Repetitions: 10 vs 10 benchmark_parse_file_mean -0.0015 -0.0015 2094336 2091290 2094305 2091247 benchmark_parse_file_median -0.0030 -0.0030 2094443 2088264 2094391 2088206 benchmark_parse_file_stddev +0.7220 +0.7250 3029 5215 3025 5218 benchmark_parse_file_cv +0.7245 +0.7275 0 0 0 0 OVERALL_GEOMEAN -0.0015 -0.0015 0 0 0 0
1 parent 150cb02 commit 484d55e

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

src/quick-lint-js/fe/expression.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,19 +257,20 @@ class expression {
257257
identifier variable_identifier() const noexcept;
258258
token_type variable_identifier_token_type() const noexcept;
259259

260-
int child_count() const noexcept;
260+
span_size child_count() const noexcept;
261261

262262
expression *child_0() const noexcept { return this->child(0); }
263263
expression *child_1() const noexcept { return this->child(1); }
264264
expression *child_2() const noexcept { return this->child(2); }
265265

266-
expression *child(int) const noexcept;
266+
expression *child(span_size) const noexcept;
267267

268268
expression_arena::array_ptr<expression *> children() const noexcept;
269269

270270
// Remove wrapping paren expressions, if any.
271271
expression *without_paren() const noexcept;
272272

273+
// TODO(strager): Return span_size instead.
273274
int object_entry_count() const noexcept;
274275

275276
object_property_value_pair object_entry(int) const noexcept;
@@ -1127,12 +1128,11 @@ inline token_type expression::variable_identifier_token_type() const noexcept {
11271128
}
11281129
}
11291130

1130-
inline int expression::child_count() const noexcept {
1131-
// TODO(strager): Remove this cast.
1132-
return narrow_cast<int>(this->children().size());
1131+
inline span_size expression::child_count() const noexcept {
1132+
return this->children().size();
11331133
}
11341134

1135-
inline expression *expression::child(int index) const noexcept {
1135+
inline expression *expression::child(span_size index) const noexcept {
11361136
return this->children()[index];
11371137
}
11381138

src/quick-lint-js/fe/parse-expression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2237,7 +2237,7 @@ expression* parser::parse_arrow_function_expression_remainder(
22372237
auto* call = expression_cast<expression::call>(parameters_expression);
22382238
if (this->peek().type == token_type::left_curly) {
22392239
parameter_list_begin = call->left_paren_span().begin();
2240-
for (int i = 1; i < call->child_count(); ++i) {
2240+
for (span_size i = 1; i < call->child_count(); ++i) {
22412241
parameters.emplace_back(call->child(i));
22422242
}
22432243
// We will report

src/quick-lint-js/fe/parse.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ void parser::error_on_pointless_string_compare(
337337
return *s == '"' || *s == '\'' || *s == '`';
338338
};
339339

340-
for (int i = 0; i < ast->child_count() - 1; i++) {
340+
for (span_size i = 0; i < ast->child_count() - 1; i++) {
341341
expression* lhs = ast->child(i)->without_paren();
342342
expression* rhs = ast->child(i + 1)->without_paren();
343343

@@ -420,7 +420,7 @@ void parser::error_on_pointless_compare_against_literal(
420420
return s == u8"=="sv || s == u8"==="sv || s == u8"!="sv || s == u8"!=="sv;
421421
};
422422

423-
for (int i = 0; i < ast->child_count() - 1; i++) {
423+
for (span_size i = 0; i < ast->child_count() - 1; i++) {
424424
source_code_span op_span = ast->operator_spans_[i];
425425
if (is_comparison_operator(op_span.string_view())) {
426426
this->check_compare_against_literal(ast->child(i), ast->child(i + 1),

0 commit comments

Comments
 (0)