Closed Bug 1583953 Opened 5 years ago Closed 5 years ago

mozilla::Span iterators not optimized out in operator==

Categories

(Core :: MFBT, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla71
Tracking Status
firefox71 --- fixed

People

(Reporter: adrian17, Assigned: adrian17)

Details

Attachments

(1 file)

When profiling the microbenchmark from bug 1420440 comment 0 on nightly and stable, I noticed the following stack close to the top of leaf functions list:

3.3% js::SharedScriptDataHasher::match(js::SharedScriptData *,RefPtr<js::SharedScriptData> const &)
3.2%    bool std::equal<mozilla::span_details::span_iterator<mozilla::Span<const unsigned char,18446744073709551615>,0>,...
3.0%        bool std::_Equal_unchecked<mozilla::span_details::span_iterator<mozilla::Span<const unsigned char,18446744073709551615>,0>,...

It seems like Clang* isn't able to optimize out the release assertions in Span iterators and produces a heavy loop.
Changing Span's operator== implementation from std::equal(l.begin(), l.end(), r.begin() to std::equal(l.data(), l.data() + l.size(), r.data() to avoid the iterators does let the compiler generate a memcmp/bcmp call instead.

Same probably applies for operator< and std::lexicographical_compare.

I'm assuming the release assertions can't be dropped, so I propose at least updating the std::equal, std::lexicographical_compare calls.

*For the record, using GCC (or disabling iterator assertions) seems to generate better code, but still not memcmp.

Assignee: nobody → adrian.wielgosik
Status: NEW → ASSIGNED

This lets us avoid pointlessly checking release assertions in a safe comparison loop.
In particular, for Span<char>, this lets the compiler use memcmp, speeding up comparisons by over an order of magnitude.

Small try run (as currently there is only a single user of these operators, in RuntimeScriptDataHasher):
https://treeherder.mozilla.org/#/jobs?repo=try&revision=9d0b1dd08c5a5c5e9561825913680f5527bec042

Keywords: checkin-needed

Pushed by ncsoregi@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/10de08776fe8
avoid assert-heavy iterators in Span comparisons. r=jwalden

Keywords: checkin-needed
Status: ASSIGNED → RESOLVED
Closed: 5 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla71
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: