Skip to content
This repository was archived by the owner on Aug 27, 2025. It is now read-only.

improve console perfs - #46

Closed
ydaveluy wants to merge 4 commits into
eclipse-platform:masterfrom
ydaveluy:feature/fast_console_links
Closed

ydaveluy wants to merge 4 commits into
eclipse-platform:masterfrom
ydaveluy:feature/fast_console_links

Conversation

@ydaveluy

Copy link
Copy Markdown

The console is slow when using unlimited console output with a lot of links.
This PR provides a faster implementation.

Use an internal list of positions that permit faster access, avoid
unnecessary copies and reduce memory consumption compared to IDocument
Position handling.

Here are some benchmarks for the execution of this code:
`

public static void main(String[] args) {

	NumberFormatException e = new NumberFormatException("Invalid number: z");
	long start = System.nanoTime();
	for (int i = 0; i < 1_000_000; ++i) {
		e.printStackTrace(System.out);
	}
	System.out.println((System.nanoTime() - start) / 1_000_000_000.+"s");
}

`

with unlimited console output:

  • before: 49s
  • after: 19s
    --> almost impossible to scroll the console before this PR

Use an internal list of positions that permit faster access, avoid
unnecessary copies and reduce memory consumption compared to IDocument
Position handling.
@ydaveluy
ydaveluy marked this pull request as ready for review July 2, 2022 17:35
@mihnita

mihnita commented Jul 4, 2022

Copy link
Copy Markdown
Contributor

This affects any kind of positions.
It would be really great if it would be possible to fix the official document handling of positions instead of just use a different mechanism for hyperlinks.

Some of preliminary digging I did for mihnita/ansi-econsole#66:
When the console buffer is "trimmed" it dumps a lot of lines from the beginning and shifts everything
The operation updates the positions, and in the process removes the ones that are in the deleted lines.
There are 2 arrays for positions, one for starts, sorted, and one for ends, unsorted.

But the operation is something like this:

for each position in allPositions
    if position needs to be deleted // in the line ranges to be deleted
        delete position document.getStartingPositions /*sorted*/ true 
        delete position document.getEndingPositions /*sorted*/ false

Note: these are not real names. I did the research, but didn't write them down.
I can do it if the direction seems promissing.

The start / end positions are stored in ArrayList
Each delete happens at the beginning, and the whole array is moved (ArrayList calls System.arraycopy in remove).
Do that for tens of thousands of deleted lines, and for several positions per line, and you have a performance problem.


Possible solution:

There are probably kinds of documents that always

  • add positions at the end
  • the positions never overlap
  • delete lines from the beginning

Probably any doc attached to a console behaves like that.

The hyperlinks and ansi escapes are examples of non-overlapping positions.

For the solution it would be nice if we detect that and make the list of endings also sorted.

And then, of the document deletion happens from the beginning, find the range of positions to delete, and call ArrayList.removeRange instead of remove

@ydaveluy

ydaveluy commented Jul 5, 2022

Copy link
Copy Markdown
Author

I would like to add some information: ending positions in AbstractDocument are sorted by end offset whereas starting positions are sorted by start offset. It is used by AbstractDocument.getPositions(String, int, int, boolean, boolean) to find overlapping positions for a given range.
As you said, in case of non overlapping positions such as hyperlinks and ansi escapes, it is possible to use a faster algo to find positions for a given range.

ArrayList.removeRange' is a protected method. It seems that the correct way is ArrayList.subList(start, end).clear()

return new IHyperlink[0];
IHyperlink[] hyperlinks = new IHyperlink[fPositions.size()];
for (int i = 0; i < fPositions.size(); i++) {
ConsoleHyperlinkPosition position = fPositions.get(i);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this variable seems redundant

position= positions[mid];
position = fPositions.get(mid);
if (offset < position.getOffset()) {
if (left == mid) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

wouldn't "?" operator be more compact here?


mid = (left + right) / 2;
position = fPositions.get(mid);
if (position.getOffset() > offset) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

woulnd't "?" operator be more compact here?

@laeubi

laeubi commented Jul 1, 2023

Copy link
Copy Markdown
Contributor

This repository was merged with https://github.com/eclipse-platform/eclipse.platform if this PR is still relevant please rebase it and push it to the new origin!

@laeubi laeubi closed this Jul 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants