Skip to content

Commit d73e32f

Browse files
committed
refactor(fe): use span_size instead of int for object literal sizes
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.0034 +0.0034 2074253 2081328 2074235 2081259 benchmark_parse_file +0.0026 +0.0026 2070954 2076320 2070943 2076303 benchmark_parse_file +0.0005 +0.0005 2069999 2071135 2069965 2071102 benchmark_parse_file -0.0011 -0.0012 2075410 2073025 2075382 2072956 benchmark_parse_file -0.0061 -0.0061 2086029 2073215 2086002 2073176 benchmark_parse_file -0.0033 -0.0032 2080115 2073346 2080054 2073305 benchmark_parse_file -0.0004 -0.0004 2074856 2074118 2074823 2074050 benchmark_parse_file +0.0058 +0.0058 2066870 2078872 2066839 2078835 benchmark_parse_file +0.0064 +0.0064 2065419 2078649 2065359 2078608 benchmark_parse_file +0.0065 +0.0065 2065748 2079127 2065716 2079083 benchmark_parse_file_pvalue 0.2413 0.2413 U Test, Repetitions: 10 vs 10 benchmark_parse_file_mean +0.0014 +0.0014 2072965 2075914 2072932 2075868 benchmark_parse_file_median +0.0013 +0.0012 2072603 2075219 2072589 2075177 benchmark_parse_file_stddev -0.4841 -0.4845 6600 3405 6601 3403 benchmark_parse_file_cv -0.4849 -0.4852 0 0 0 0 OVERALL_GEOMEAN +0.0014 +0.0014 0 0 0 0
1 parent 484d55e commit d73e32f

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,9 @@ class expression {
270270
// Remove wrapping paren expressions, if any.
271271
expression *without_paren() const noexcept;
272272

273-
// TODO(strager): Return span_size instead.
274-
int object_entry_count() const noexcept;
273+
span_size object_entry_count() const noexcept;
275274

276-
object_property_value_pair object_entry(int) const noexcept;
275+
object_property_value_pair object_entry(span_size) const noexcept;
277276

278277
source_code_span span() const noexcept;
279278

@@ -1243,19 +1242,18 @@ inline expression *expression::without_paren() const noexcept {
12431242
return const_cast<expression *>(ast);
12441243
}
12451244

1246-
inline int expression::object_entry_count() const noexcept {
1245+
inline span_size expression::object_entry_count() const noexcept {
12471246
switch (this->kind_) {
12481247
case expression_kind::object:
1249-
// TODO(strager): Remove this cast.
1250-
return narrow_cast<int>(static_cast<const expression::object *>(this)->entries_.size());
1248+
return static_cast<const expression::object *>(this)->entries_.size();
12511249

12521250
default:
12531251
QLJS_UNEXPECTED_EXPRESSION_KIND();
12541252
}
12551253
}
12561254

1257-
inline object_property_value_pair expression::object_entry(int index) const
1258-
noexcept {
1255+
inline object_property_value_pair expression::object_entry(
1256+
span_size index) const noexcept {
12591257
switch (this->kind_) {
12601258
case expression_kind::object:
12611259
return static_cast<const expression::object *>(this)->entries_[index];

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void parser::visit_expression(expression* ast, parse_visitor_base& v,
148148
break;
149149
}
150150
case expression_kind::object:
151-
for (int i = 0; i < ast->object_entry_count(); ++i) {
151+
for (span_size i = 0; i < ast->object_entry_count(); ++i) {
152152
auto entry = ast->object_entry(i);
153153

154154
if (entry.init && entry.is_merged_property_and_value_shorthand()) {
@@ -251,7 +251,7 @@ void parser::maybe_visit_assignment(expression* ast, parse_visitor_base& v) {
251251
}
252252
break;
253253
case expression_kind::object:
254-
for (int i = 0; i < ast->object_entry_count(); ++i) {
254+
for (span_size i = 0; i < ast->object_entry_count(); ++i) {
255255
expression* value = ast->object_entry(i).value;
256256
this->maybe_visit_assignment(value, v);
257257
}

0 commit comments

Comments
 (0)