Skip to content

Performance improvement in print_endline for redirection#1399

Closed
ytomino wants to merge 1 commit intoocaml:trunkfrom
ytomino:noflush
Closed

Performance improvement in print_endline for redirection#1399
ytomino wants to merge 1 commit intoocaml:trunkfrom
ytomino:noflush

Conversation

@ytomino
Copy link
Copy Markdown

@ytomino ytomino commented Oct 4, 2017

This PR inserts checking isatty and avoids the calling caml_ml_flush in print_endline, print_newline, prerr_endline, and prerr_newline.

Motivation

OCaml's I/O routines is very fast because using not C stdio but the original buffering.
However, the family of print_endline are exceptions. They makes I/O would become slow, mainly, in the case that the standard I/O is redirected.

C stdlib checks isatty to avoid fflush on outputting '\n' for the performance.
It is desirable that OCaml's I/O be revised likewise.

Details of implementation

Add new flag CHANNEL_FLAG_ISATTY into flags of struct channel, and refer it to reduce the calling isatty, because, generally speaking, system calls are slower than normal functions.
(This flag may be useful for other future uses ??)

Thanks.

@ytomino
Copy link
Copy Markdown
Author

ytomino commented Oct 5, 2017

Sorry, tests/lib-dynlink-bytecode is failed.
This test mixes C stdio and OCaml's I/O.

@ytomino ytomino closed this Oct 5, 2017
@ytomino ytomino deleted the noflush branch October 5, 2017 00:20
@nojb
Copy link
Copy Markdown
Contributor

nojb commented Oct 5, 2017

FWIW, if you are worried about the performance impact of flushing, you can always use fun s -> print_string s; print_char '\n' instead of print_endline s which avoids the flush.

IMHO not flushing when redirecting to files does not seem a good idea.

@ytomino
Copy link
Copy Markdown
Author

ytomino commented Oct 5, 2017

IMHO not flushing when redirecting to files does not seem a good idea.

I agree. tests/lib-dynlink-bytecode aware me of that this change would break many existing codes calling some large C parts, or mixing stdout and stderr. It's unacceptable.

IMHO, most libraries (including other language runtimes) are using C stdio. So the OCaml's I/O is inferior to them in the case of writing a kind of application like filter command that requires speed in files and immediate flushing in CUI. Using Unix.isatty explicitly in application side is unrealistic.
I wish there is a good corporation method.

@shindere
Copy link
Copy Markdown
Contributor

shindere commented Oct 5, 2017 via email

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.

3 participants