Skip to content

chore: use const fn in Span #10320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

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: 649c394

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 degrade performances by 2.42%

Comparing bvanjoi:const-span (649c394) with main (3fb1950)

Summary

⚡ 1 improvements
❌ 1 regressions
✅ 150 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
serialization of serde 2.8 µs 2.8 µs +1.06%
es/lints/libs/terser 29 ms 29.7 ms -2.42%

self.lo
}

#[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...

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