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

chore: Move natural-sort functionality into packages/internal #10320

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

gibson042
Copy link
Member

Ref endojs/endo#2113

Description

Removes unnecessary use of localeCompare and consolidates natural-order sorting.

Security Considerations

n/a

Scaling Considerations

n/a

Documentation Considerations

n/a

Testing Considerations

None in particular, although I probably should add something to packages/internal/test.

Upgrade Considerations

This code runs outside of all vats (and AFAIK is not exercised on-chain at all) and is safe to include in any release.

@gibson042 gibson042 requested review from warner and mhofman October 23, 2024 00:36
@gibson042 gibson042 requested a review from a team as a code owner October 23, 2024 00:36
Copy link
Member

@mhofman mhofman left a comment

Choose a reason for hiding this comment

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

Interesting, I didn't know we had this natural compare. I wish it was used in a few more places (vatID, etc.)

I'd like to come back to review this more closely.

packages/internal/src/natural-sort.js Outdated Show resolved Hide resolved
Comment on lines +1 to +6
/**
* @param {string} a
* @param {string} b
* @returns {-1 | 0 | 1}
*/
const compareNats = (a, b) => {
Copy link
Member

Choose a reason for hiding this comment

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

My understanding is that this logic works even is the params are number, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mostly, but not when either is NaN or both are infinite. And this function is not exported, so we can count on the input arguments always being decimal digit strings.

@mhofman mhofman self-requested a review October 23, 2024 02:13
Copy link

cloudflare-workers-and-pages bot commented Oct 23, 2024

Deploying agoric-sdk with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8c475e8
Status: ✅  Deploy successful!
Preview URL: https://8e6ccafb.agoric-sdk.pages.dev
Branch Preview URL: https://gibson-2024-10-natural-sort.agoric-sdk.pages.dev

View logs

@gibson042 gibson042 force-pushed the gibson-2024-10-natural-sort branch from 3240449 to 9e6b7ad Compare October 23, 2024 05:44
Copy link
Member

@warner warner left a comment

Choose a reason for hiding this comment

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

nice refactor, but that pre-existing weirdness in kernelKeeper.dumpState needs addressing

return 1;
// Perform an element-by-element natural sort.
kernelTable.sort((a, b) => {
const len = Math.min(a.length, b.length);
Copy link
Member

Choose a reason for hiding this comment

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

This feels wrong.. actually the original feels wrong too. The kernelTable data is mostly created by calling vatKeeper.dumpState(), and that returns a 3-tuple of [kernelSlot, vatID, vatSlot]. It would make the most sense to use naturalCompare separately on each of the three components (krefs, vatIDs, and vrefs all have the same letters-then-digits shape).

I don't know why the original was treating the rows as six-tuples. My best guess is it thought a[0], a[1], a[2] was individual characters of a kref, so the ko prefix would be compared by strings, and the digit suffix would be compared by number. But that doesn't make a lot of sense either.

In any case, I think we want something more like:

kernelTable.sort((a,b) =>
  naturalCompare(a[0], b[0]) ||
  naturalCompare(a[1], b[1]) ||
  naturalCompare(a[2], b[2]) || 0);

unless you can make more sense of the old code than me..

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that makes sense. I just realized I typo'd the general form anyway (a[0] and b[0] rather than a[i] and b[i]).

@gibson042 gibson042 requested a review from warner October 25, 2024 12:37
Copy link
Member

@warner warner left a comment

Choose a reason for hiding this comment

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

that looks good to me, but I'll defer to @mhofman if he's got something more specific to look at

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.

3 participants