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

[NFC] Improving edit distance string extension #446

Merged
merged 2 commits into from
Jun 5, 2022

Conversation

LucianoPAlmeida
Copy link
Contributor

Improving implementation of edit distance string extension by:

  • Using only two arrays n + 1 instead of an m x n matrix.
  • Trimming common prefix and suffix as an optimization for reducing size/complexity
  • Tests for those cases were added.

Been playing around with ways to improve it more in https://github.com/LucianoPAlmeida/strings

Benchmarks
Code:

var benchmark = Benchmark(title: "Edit distance")
let inputGeneratorCommon: (Int) -> (String, String) = { size in
    let common = String(repeating: "a", count: size / 4)
    let s1 = String(repeating: "foo", count: (size / 2) / 3)
    let s2 = String(repeating: "bar", count: (size / 2) / 3)
    return (common + s1 + common, common + s2 + common)
}

let inputGeneratorDifferent: (Int) -> (String, String) = { size in
  let s1 = String(repeating: "foo", count: size)
  let s2 = String(repeating: "bar", count: size)
  return (s1, s2)
}

benchmark.registerInputGenerator(inputGeneratorCommon)
benchmark.add(
  title: "Opt editDistance common prefix/suffix",
  input: (String, String).self,
  maxSize: 1000
) { input in
  return { timer in
    let d = input.0.editDistance(to: input.1)
    blackHole(d)
  }
}

benchmark.add(
  title: "editDistance common prefix/suffix",
  input: (String, String).self,
  maxSize: 1000
) { input in
  return { timer in
    let d = input.0.editDistanceOld(to: input.1)
    blackHole(d)
  }
}

benchmark.registerInputGenerator(inputGeneratorDifferent)
benchmark.add(
  title: "Opt editDistance",
  input: (String, String).self,
  maxSize: 1000
) { input in
  return { timer in
    let d = input.0.editDistance(to: input.1)
    blackHole(d)
  }
}

benchmark.add(
  title: "editDistance",
  input: (String, String).self,
  maxSize: 1000
) { input in
  return { timer in
    let d = input.0.editDistanceOld(to: input.1)
    blackHole(d)
  }
}

chart

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

@LucianoPAlmeida
Copy link
Contributor Author

@swift-ci Please test

@LucianoPAlmeida
Copy link
Contributor Author

@swift-ci Please test

@LucianoPAlmeida
Copy link
Contributor Author

cc @natecook1000

@natecook1000
Copy link
Member

@swift-ci Please test

@natecook1000 natecook1000 merged commit 78213f3 into apple:main Jun 5, 2022
@LucianoPAlmeida LucianoPAlmeida deleted the edit-distance branch June 10, 2022 02:17
leuski pushed a commit to leuski/swift-argument-parser that referenced this pull request Jun 17, 2022
* 'main' of github.com:apple/swift-argument-parser: (114 commits)
  Fix `AsyncParseableCommand` hierarchy (apple#436)
  Add experimental manual page generation (apple#332)
  Improving edit distance string extension (apple#446)
  List valid options in error messages for enum array argument (apple#445)
  Remove LinuxMain.swift (apple#367)
  Hide hidden subcommands from completions (apple#443)
  Update changelog for 1.1.2 release (apple#441)
  Fix error message for @option array without values (apple#435)
  Fix Repeat's endless printing (apple#437)
  build: statically link ArgumentParserToolInfo always (apple#424)
  Update changelog for the 1.1.1 release (apple#428)
  build: complete the changes from apple#423 (apple#425)
  Remove platform requirement from Package.swift (apple#427)
  build: repair the build after apple#404 (apple#423)
  Fix broken links/incorrect variance calculation (apple#422)
  Update changelog for the 1.1.0 release (apple#421)
  Update documentation (apple#420)
  Make `@OptionGroup(visibility:)` a public API (apple#419)
  Support an `async` entry point for commands (apple#404)
  Fix a typo and template links (apple#418)
  ...
rauhul added a commit that referenced this pull request Aug 26, 2022
- Start of a fix for issue 446. After an initial read through the code
  it feels like there more @argument initializers than needed and this
  logic can probably be reudced and simplified.
- Fixes isOptional derivision for non-Optional @arguments with default
  values, however the simple solution doesn't work properly for Optional
  @arguments without a non-nil default value.
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