Skip to content
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

Small lints #3244

Merged
merged 5 commits into from
Aug 25, 2024
Merged

Small lints #3244

merged 5 commits into from
Aug 25, 2024

Conversation

romgrk
Copy link
Contributor

@romgrk romgrk commented Aug 22, 2024

Various minor lints, see comments below.

Copy link

changeset-bot bot commented Aug 22, 2024

🦋 Changeset detected

Latest commit: 1fa3297

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@emotion/serialize Patch
@emotion/utils Patch

Not sure what this means? Click here to learn what changesets are.

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

registeredStyles.push(`${registered[className]};`)
} else {
registeredStyles.push(registered[className] as string)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

registered contains serialized styles that are already terminated by a ; by the serialization code. It avoids allocating a new string.

Copy link
Member

Choose a reason for hiding this comment

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

This was added in #1656 . Maybe it's good to consider that a mistake in hindsight but it doesn't give me confidence that we can remove this safely now. I'm surprised that tests are OK with this change (well, some failed but I'm not sure if this is related, I see some hashes changed but I'd expect some functional break too). Maybe we are missing a test case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ; should definitely be changing the hash of strings passing through here, so that's expected.

The ; feels overly defensive to me and I don't like that it's also creating more strings for the same reasons explained in the other comment. It would be nice to ensure that what goes in cache.registered is strictly ; terminated, then we can safely do the change here and avoid modifying the strings everytime they pass here.

I'll revert it for now though, might require more investigation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For more context, I frequently see strings like this one:

image

Copy link

codesandbox-ci bot commented Aug 22, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

registeredStyles.push(`${registered[className]};`)
} else {
registeredStyles.push(registered[className] as string)
} else if (className) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

classNames could have multiple sequential spaces, in which case .split(' ') would produce empty strings.

Copy link
Member

Choose a reason for hiding this comment

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

a test case probably could be written for this, does it create a problem though? a rawClassName has already been allocated, can't this use some kind of string ropes or something to stay optimized?

Copy link
Contributor Author

@romgrk romgrk Aug 22, 2024

Choose a reason for hiding this comment

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

Yeah someone might have inserted additional whitespaces in their classnames.

So sure engines will usually use a concatenated string like I describe here to optimize concat operations, but adding a single ' ' is likely too small to be worth it, engines are most likely just allocating a new string for those bytes.

I wrote a benchmark with only this difference, it does seem to have a positive impact: (I inserted a bunch of spaces to highlight the impact more clearly)

image

let labelPattern = /label:\s*([^\s;\n{]+)\s*(;|$)/g
let labelPattern = /label:\s*([^\s;{]+)\s*(;|$)/g
Copy link
Contributor Author

Choose a reason for hiding this comment

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

\s contains \n. See node -p "'\\n'.match(/\\s/)"

@Andarist
Copy link
Member

Could you add short changesets for this? Otherwise, those changes won't get released until some other PR mentions the touched packages in its changesets. The text that you put there will be automatically copied over to CHANGELOG files so it should be relevant for its potential readers (users of the projects, and not us - maintainers)

@romgrk
Copy link
Contributor Author

romgrk commented Aug 23, 2024

I have added the changeset, please let me know if there's anything else to do on my side.

@Andarist Andarist merged commit ad630e3 into emotion-js:main Aug 25, 2024
8 of 9 checks passed
@github-actions github-actions bot mentioned this pull request Aug 25, 2024
Copy link

codecov bot commented Aug 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.83%. Comparing base (f4640f6) to head (1fa3297).
Report is 68 commits behind head on main.

Additional details and impacted files
Files Coverage Δ
packages/serialize/src/index.ts 99.47% <100.00%> (ø)
packages/utils/src/index.ts 100.00% <100.00%> (ø)

... and 42 files with indirect coverage changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants