Skip to content

Conversation

@tgarc
Copy link
Contributor

@tgarc tgarc commented Sep 20, 2017

switch to out of line mode to improve sounddevice module import time.

this adds a separate sounddevice_build.py file that becomes part of
the setup.py build process and is only run once at install time rather
than everytime the module is imported

@mgeier
Copy link
Member

mgeier commented Sep 21, 2017 via email

@tgarc
Copy link
Contributor Author

tgarc commented Sep 21, 2017

I will test on windows.

interesting side-note: making the same change for the soundfile library seems to have no impact on its import time

@tgarc
Copy link
Contributor Author

tgarc commented Sep 23, 2017

with windows ran the same command:

$ (for i in {1..20}; do { time python -c 'import sounddevice' ; } 2>&1 | grep -oE '0\.[0-9]+'; done) | awk '{total += $1} END {print total}'

Linux:

A couple runs with sounddevice-0.3.8 gave 416ms, and 424ms
A couple of runs with out-of-line build gave 227ms and 247ms

Windows:
A couple runs with sounddevice-0.3.8 gave 371ms and 372ms
A couple of runs with out-of-line build gave 267ms and 267ms

@mgeier
Copy link
Member

mgeier commented Oct 11, 2017

Sorry for the delay, now I'm back ...

I've tried it on macOS, but it produces an error:

>>> import sounddevice
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../sounddevice.py", line 2646, in <module>
    _ignore_stderr()
  File "/.../sounddevice.py", line 2638, in _ignore_stderr
    stdio.stderr = devnull
ffi.error: symbol 'stderr' not found in library '<None>': dlsym(RTLD_DEFAULT, stderr): symbol not found

It seems that instead of a KeyError we get an ffi.error.
I'm not sure if that's a bug in CFFI, but if I change KeyError to _ffi.error, it works!

@mgeier
Copy link
Member

mgeier commented Oct 11, 2017

interesting side-note: making the same change for the soundfile library seems to have no impact on its import time

That's indeed interesting.
If I've counted correctly, the soundfile library has about 120 lines of C code to parse, while sounddevice has more than 300.
Is that the reason or is there something else going on?

@tgarc
Copy link
Contributor Author

tgarc commented Oct 11, 2017 via email

@mgeier
Copy link
Member

mgeier commented Oct 14, 2017

@tgarc Do you want to add the fix for macOS or should I do that?

Apart from that, I think this is ready to merge.

@tgarc
Copy link
Contributor Author

tgarc commented Oct 14, 2017 via email

switch to out of line mode to improve sounddevice module import time.

this adds a separate sounddevice_build.py file that becomes part of
the setup.py build process and is only run once at install time rather
than everytime the module is imported
@mgeier
Copy link
Member

mgeier commented Oct 14, 2017

Thanks! Yes, I think both should be changed, since theoretically there might be neither stderr nor __stderrp available.

@tgarc
Copy link
Contributor Author

tgarc commented Oct 14, 2017

done!

@tgarc
Copy link
Contributor Author

tgarc commented Oct 15, 2017

okay I lied, I do see a modest improvement applying this change to the soundfile library as well. I think I just didn't see it on the machine I originally tested on because of other latency issues

@mgeier
Copy link
Member

mgeier commented Oct 15, 2017

Thanks @tgarc!
I've added another twist in #103, what do you think about it?

mgeier added a commit that referenced this pull request Oct 19, 2017
Switching to out-of-line ABI mode (#102) broke this.
mgeier added a commit to mgeier/python-soundfile that referenced this pull request Oct 19, 2017
Switching to out-of-line ABI mode (bastibe#211) broke this.
See also spatialaudio/python-sounddevice#102.
mgeier added a commit that referenced this pull request Oct 21, 2017
Switching to out-of-line ABI mode (#102) broke this.
@tgarc
Copy link
Contributor Author

tgarc commented Oct 22, 2017

thanks for merging this! The same changes has now been merged with PySoundFile as well. This dropped my command line application run time (which imports sounddevice and soundfile) down from 130ms to about 50ms. This is a far cry from the <10ms launch times of e.g. aplay (alsa utility for playing audio) but is tolerable for my purposes.

Also FYI, if you're ever using entry_points with setup.py there's a huge runtime delay added unless the module is installed as a wheel (issue report here). Just another fun fact about setuptools.

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