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

Translate C to Swift #62

Merged
merged 1 commit into from
Nov 4, 2023
Merged

Translate C to Swift #62

merged 1 commit into from
Nov 4, 2023

Conversation

jpsim
Copy link
Owner

@jpsim jpsim commented Dec 30, 2022

Migrate the CMicrophonePitchTracker C module to a new ZenPTrack Swift module.

When compiling the new module with -Ounchecked performance is within 0%-2% of the C version. Without -Ounchecked it has a 20%-25% overhead.

$ hyperfine --warmup 20 --runs 50 pitchbench-c pitchbench-swift-checked pitchbench-swift-unchecked
Benchmark 1: ./pitchbench-c
  Time (mean ± σ):     391.7 ms ±   0.7 ms    [User: 379.3 ms, System: 8.6 ms]
  Range (min … max):   390.7 ms … 394.1 ms    50 runs
 
Benchmark 2: ./pitchbench-swift-checked
  Time (mean ± σ):     472.1 ms ±   0.8 ms    [User: 459.5 ms, System: 8.9 ms]
  Range (min … max):   471.0 ms … 475.8 ms    50 runs
 
Benchmark 3: ./pitchbench-swift-unchecked
  Time (mean ± σ):     391.7 ms ±   0.5 ms    [User: 379.5 ms, System: 8.6 ms]
  Range (min … max):   390.9 ms … 392.9 ms    50 runs
 
Summary
  './pitchbench-c' ran
    1.00 ± 0.00 times faster than './pitchbench-swift-unchecked'
    1.21 ± 0.00 times faster than './pitchbench-swift-checked'

-Ounchecked breaks Swift's memory safety, but the fact that it's only enabled in release mode, combined with Swift's additional safety over C even with that flag, I believe means we're in a safer position than the C code we're replacing. I think.

@jpsim jpsim force-pushed the translate-c-to-swift branch 6 times, most recently from e011ca5 to f945b73 Compare October 19, 2023 12:44
Migrate the `CMicrophonePitchTracker` C module to a new `ZenPTrack`
Swift module.

When compiling the new module with `-Ounchecked` performance is within
0%-2% of the C version. Without `-Ounchecked` it has a 20%-25% overhead.

```console
$ hyperfine --warmup 30 --runs 50 \
    pitchbench-c \
    pitchbench-swift-checked \
    pitchbench-swift-unchecked
Benchmark 1: ./pitchbench-c
  Time (mean ± σ):     391.7 ms ±   0.7 ms    [User: 379.3 ms, System: 8.6 ms]
  Range (min … max):   390.7 ms … 394.1 ms    50 runs
 
Benchmark 2: ./pitchbench-swift-checked
  Time (mean ± σ):     472.1 ms ±   0.8 ms    [User: 459.5 ms, System: 8.9 ms]
  Range (min … max):   471.0 ms … 475.8 ms    50 runs
 
Benchmark 3: ./pitchbench-swift-unchecked
  Time (mean ± σ):     391.7 ms ±   0.5 ms    [User: 379.5 ms, System: 8.6 ms]
  Range (min … max):   390.9 ms … 392.9 ms    50 runs
 
Summary
  './pitchbench-c' ran
    1.00 ± 0.00 times faster than './pitchbench-swift-unchecked'
    1.21 ± 0.00 times faster than './pitchbench-swift-checked'
```

`-Ounchecked` breaks Swift's memory safety, but the fact that it's only
enabled in release mode, combined with Swift's additional safety over C
even with that flag, means we're in a safer position than the C code
we're replacing.
@jpsim jpsim marked this pull request as ready for review October 21, 2023 03:50
@jpsim jpsim merged commit 671e568 into main Nov 4, 2023
5 checks passed
@jpsim jpsim deleted the translate-c-to-swift branch November 4, 2023 18:53
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.

None yet

1 participant