Skip to content

Proposing changes to drawing backend #8

@M2-TE

Description

@M2-TE

Disclaimer: I am mostly talking about the drawing of graphs, as that is what I focused on for now in #6.

The current drawing method relies on the << operator of std::cout in combination of '\n' for line breaks.
This may result in uncontrolled flushes and somewhat suboptimal console writes.

Since you put "performance" as one of the TODOs, I would propose a change to the way graphs are drawn to the console via a Temporary Buffer:
The size is mostly known beforehand, where enough memory should be allocated to fit
height*width*(unicode_size+ANSI_escape_size)
which assumes that every unicode character may be accompanied by an ANSI escape code for color. The color here would be fixed width and may be 4-/8- or 24-bit color.
The entire buffer or the separate rows can be written to the console more efficiently using std::cout.write(row, row_bytes) followed by manual flushing, which will be much more efficient than using std::cout with <<.

Some side effects of using such a buffer will be the freedom of what parts of the graph can be written to and in what order; e.g. it would allow first drawing the plot/graph itself, followed by potentially overwriting via axis drawing in a second pass. Not only may this be more optimal, it may also allow some better encapsulation of the code used to draw stuff.

If not a full temporary buffer, it could instead be a good idea to simply create a new buffer for every row during the iteration in the current drawing method, at the end of which std::cout.write() can be called to write that entire row, while not really changing any of the existing code apart from replacing std::cout << instances.

Additionally, we could provide a manual buffer for std::cout itself via rdbuf and disable automatic buffer flush with sync_with_stdio (may be of interest to do especially when you want to keep using std::cout<<), since we know pretty well when flushes should occur.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions