JuliaAudio / PortAudio.jl

PortAudio wrapper for the Julia programming language, compatible with the JuliaAudio family of packages
Other
115 stars 19 forks source link

avoid fields with abstract types #100

Closed jg-854 closed 2 years ago

jg-854 commented 2 years ago

The Julia documentation states that fields with abstract types can lead to performance problems.

Buffer contains the field data::Array{Sample} where Sample. This type has a free parameter, namely the dimension of the array, and therefore , it will lead to type-instability when calling the function getproperty(buffer, :data).

I believe, from experiments on my local machine, the type-instability is leading to unnecessary allocations when calling PortAudio.read_or_write (which ideally should have 0 allocations for real-time applications?). When running the following code:

s = PortAudioStream(1,0 ; warn_xruns = false)
@ballocated(PortAudio.read_or_write(PortAudio.Pa_ReadStream, $(s).source_messanger.buffer))

3

By updating the field type such that it is now of type Array{Sample, 2}, retrieving the raw data is now type-stable and allocations are removed.

bramtayl commented 2 years ago

This looks great! Thanks!

codecov[bot] commented 2 years ago

Codecov Report

Merging #100 (f349a7a) into master (3cd4551) will increase coverage by 6.26%. The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #100      +/-   ##
==========================================
+ Coverage   91.39%   97.66%   +6.26%     
==========================================
  Files           2        2              
  Lines         337      342       +5     
==========================================
+ Hits          308      334      +26     
+ Misses         29        8      -21     
Impacted Files Coverage Δ
src/PortAudio.jl 97.76% <ø> (+5.51%) :arrow_up:
src/libportaudio.jl 97.29% <0.00%> (+9.41%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 3cd4551...f349a7a. Read the comment docs.