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

Set stdout and stdin to binary mode on Python 2 and Windows #1150

Merged
merged 1 commit into from Jun 21, 2018

Conversation

micbou
Copy link
Contributor

@micbou micbou commented Jun 20, 2018

In PR #1046, a workaround was implemented on Windows to avoid control characters on stdin and stdout because those streams are open in text mode on Python 2. The workaround consists to convert the binary data to its hexadecimal representation. The issue with that approach is that it can be really inefficient when the data is big. A better solution is to put stdin and stdout in binary mode on Windows and Python 2.

Here are some measurements obtained with this script when completing the os, numpy, and cv2 (opencv-python) modules before and after the changes on Windows:

Interpreter Python 2.7 Python 3.6
Environment Python 2.7 Python 3.6 Python 2.7 Python 3.6
Before After Before After Before After Before After
os 0.111s 0.141s 0.088s 0.13s 0.062s 0.062s 0.04s 0.041s
numpy 1.01s 0.286s 1.08s 0.344s 0.204s 0.167s 0.167s 0.153s
cv2 32.9s 1.37s 43.3s 1.98s 1.89s 0.493s 1.95s 0.854s

As you can see, speed improvements are huge for the cv2 module on Python 2 (up to 20 times faster). Only completing the os module on Python 2 is slower but the speed improvement in PR #1149 takes care of that.


This change is Reviewable

@codecov-io
Copy link

codecov-io commented Jun 20, 2018

Codecov Report

Merging #1150 into master will decrease coverage by 0.05%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1150      +/-   ##
==========================================
- Coverage   92.78%   92.73%   -0.06%     
==========================================
  Files          57       57              
  Lines        6902     6906       +4     
==========================================
  Hits         6404     6404              
- Misses        498      502       +4
Impacted Files Coverage Δ
jedi/evaluate/compiled/subprocess/__init__.py 75.5% <0%> (-1.55%) ⬇️

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 f9e90e8...440b9ab. Read the comment docs.

file.flush()
# Python on Windows don't throw EPIPE errors for pipes. So reraise them with
# the correct type and error number.
except OSError:
Copy link
Owner

Choose a reason for hiding this comment

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

Can we maybe check if the OS is windows here and otherwise just raise the error again? I generally don't like it when exceptions are caught that might be happening, because of an unrelated issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

elif sys.platform == 'win32':
import msvcrt
msvcrt.setmode(stdout.fileno(), os.O_BINARY)
msvcrt.setmode(stdin.fileno(), os.O_BINARY)
Copy link
Owner

Choose a reason for hiding this comment

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

That makes a LOT of sense. Thanks for finding it. How did you realize that was the solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The comment about control characters being consumed on Windows made me think that something were wrong with how the streams were open. Adding the -u option to the Python command confirmed my suspicion as this option forces the streams to be open in binary mode. However, it also make the streams unbuffered which has a big impact on performance (as seen in PR #1149) so I searched another way to put the streams in binary mode and found this solution.

@davidhalter
Copy link
Owner

Awesome, thanks!

@davidhalter davidhalter merged commit 106b11f into davidhalter:master Jun 21, 2018
@micbou micbou deleted the pickle-python2 branch June 21, 2018 22:16
zzbot added a commit to ycm-core/ycmd that referenced this pull request Jun 23, 2018
[READY] Include Jedi performance improvements

This includes PRs davidhalter/jedi#1149, davidhalter/jedi#1150, davidhalter/jedi#1151, and davidhalter/jedi#1152 which significantly improve performance especially on Windows and Python 2.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/valloric/ycmd/1056)
<!-- Reviewable:end -->
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

3 participants