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

fix(path): fix common() results that depend on the order and/or number of input paths #4414

Merged
merged 2 commits into from
Mar 5, 2024
Merged

Conversation

jviide
Copy link
Contributor

@jviide jviide commented Feb 28, 2024

This pull request addresses (and adds tests for) cases where common() returns different values depending on the order and/or number of the input paths, when different results may not be intended.

For example in this case it's probably expected that both cases return an empty string:

> common(["/", ""], "/")
'/'
> common(["", "/"], "/")
''

In the next case the result depends on the number of paths, even if the paths are all equal. The expected result in both cases is likely '/foo/bar':

> common(["/foo/bar"], "/")
'/foo/'      // missing 'bar'
> common(["/foo/bar", "/foo/bar"], "/")
'/foo/bar/'  // extra slash at the end

This pull request adds a variable (append) to track whether a separator should be appended to the end of the result string. Early returns are removed to keep this tracking consistent. The performance doesn't seem to degrade in the following tested case:

Deno.bench("original common()", () => {
  commonOriginal([
    "./deno/std/path/mod.ts",
    "./deno/std/fs/mod.ts",
  ], "/")
});

Deno.bench("modified common()", () => {
  commonModified([
    "./deno/std/path/mod.ts",
    "./deno/std/fs/mod.ts",
  ], "/")
});

Results for Deno v1.41.0, MacOS 14.3.1:

benchmark              time (avg)        iter/s             (min … max)       p75       p99      p995
----------------------------------------------------------------------- -----------------------------
original common()     225.89 ns/iter   4,426,966.2 (219.21 ns … 255.68 ns) 227.8 ns 246.43 ns 247.09 ns
modified common()     196.07 ns/iter   5,100,328.9  (188.82 ns … 217.8 ns) 196.75 ns 216.05 ns 216.22 ns

@jviide jviide requested a review from kt3k as a code owner February 28, 2024 23:52
@CLAassistant
Copy link

CLAassistant commented Feb 28, 2024

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the path label Feb 28, 2024
Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

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

All suggested changes seem reasonable to me. Thanks for working on this!

@kt3k kt3k merged commit 0c6f5e8 into denoland:main Mar 5, 2024
9 checks passed
@jviide jviide deleted the patch-01 branch March 5, 2024 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants