Skip to content

Conversation

bvanjoi
Copy link
Contributor

@bvanjoi bvanjoi commented Apr 7, 2025

Description:

Using const fn can generally help lower both runtime overhead.

BREAKING CHANGE:

nothing.

Related issue (if exists):

Fixes #9962

@bvanjoi bvanjoi requested a review from a team as a code owner April 7, 2025 04:13
Copy link

changeset-bot bot commented Apr 7, 2025

⚠️ No Changeset found

Latest commit: 2bad14e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

codspeed-hq bot commented Apr 7, 2025

CodSpeed Performance Report

Merging #10320 will not alter performance

Comparing bvanjoi:const-span (2bad14e) with main (4976b12)

Summary

✅ 140 untouched benchmarks


#[inline]
pub fn new(mut lo: BytePos, mut hi: BytePos) -> Self {
if lo > hi {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually #9962 is about removing this swap

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. But I have two questions:
• Does this change break existing code?
• Should we add assert!(lo < hi) or debug_assert!(lo < hi)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for the first question. (It’s a bug IMO)

And debug assert would be enough considering the number of parser tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving swap to the caller code will make it slower. That's why simply changing it to a const fn slowed it. (swap operation is inlined into call side with constant swap)

image

if lo > hi {
std::mem::swap(&mut lo, &mut hi);
pub const fn new(lo: BytePos, hi: BytePos) -> Self {
if lo.0 > hi.0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed and callers should be fmodified to call Span::new with correct argument

@bvanjoi bvanjoi requested review from a team as code owners April 7, 2025 07:55
@bvanjoi
Copy link
Contributor Author

bvanjoi commented Apr 7, 2025

The latest benchmark report doesn’t show any regressions (I’m guessing it’s just some noise in the es/lints/libs/terser metrics).

Not sure why the run_fixture_test_tests__exec__generator__ecma262_pr_2819__exec_js failed though – can’t reproduce it locally. Happy to dig deeper if we decide to move forward with landing this.

@kdy1 What do you think? Should we keep pursuing this change?

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of this PR?
swap operation is not removed at all...

@kdy1
Copy link
Member

kdy1 commented Jul 1, 2025

Are you still willing to work on removal of std::mem::swap calls?

@kdy1 kdy1 added this to the Planned milestone Jul 1, 2025
@bvanjoi bvanjoi marked this pull request as draft July 2, 2025 09:01
@kdy1 kdy1 removed this from the Planned milestone Jul 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

swc_common: Make Span::new const fn
2 participants