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

Writing the world map to JTextPane is sometimes too slow #176

Closed
bernardosulzbach opened this issue Nov 11, 2015 · 6 comments
Closed

Writing the world map to JTextPane is sometimes too slow #176

bernardosulzbach opened this issue Nov 11, 2015 · 6 comments
Assignees

Comments

@bernardosulzbach
Copy link
Owner

I've had some really poor performance with some dense maps being written.

@bernardosulzbach
Copy link
Owner Author

http://javatechniques.com/blog/faster-jtextpane-text-insertion-part-i/ has some fantastic insights into this issue.

Basically, there are two causes for slow JTextPane updates: each insertion triggers a re-rendering of the display and some methods in the DefaultStyledDocument class are synchronized, so there is the overhead of acquiring locks.

I will work around the first issue and see if it is enough, it should be.

The current idea is to keep two identical StyledDocuments. One is set to the text pane while the other awaits for an update. When we need to write something new, we first update the one that is resting. Then set it to the pane, then update the other one.

Note to self: remember to check that clearTextPane() clears both documents. Also check that everything that currently changes these documents will work with the new structure.

Maybe even write a class that handles these update mechanism for the window. The GameWindow class already does too much. That class would encapsulate implementation details and provide a write method. It would then perform the write-swap-write idea behind the scenes.

@bernardosulzbach
Copy link
Owner Author

Microbenchmarking results for my first implementation of the solution

Old:

[2015-11-22 18:05:25] (FINE) : Finished rendering 'debug map' after 86 ms.
[2015-11-22 18:05:25] (FINE) : Finished rendering 'debug map' after 66 ms.
[2015-11-22 18:05:26] (FINE) : Finished rendering 'debug map' after 71 ms.
[2015-11-22 18:05:26] (FINE) : Finished rendering 'debug map' after 81 ms.
[2015-11-22 18:05:26] (FINE) : Finished rendering 'debug map' after 78 ms.
[2015-11-22 18:05:26] (FINE) : Finished rendering 'debug map' after 88 ms.

statistics.mean([88,78,81,71,66,86])
78.3

New:

[2015-11-22 18:03:48] (FINE) : Finished rendering 'debug map' after 61 ms.
[2015-11-22 18:03:49] (FINE) : Finished rendering 'debug map' after 76 ms.
[2015-11-22 18:03:49] (FINE) : Finished rendering 'debug map' after 54 ms.
[2015-11-22 18:03:49] (FINE) : Finished rendering 'debug map' after 48 ms.
[2015-11-22 18:03:49] (FINE) : Finished rendering 'debug map' after 47 ms.
[2015-11-22 18:03:50] (FINE) : Finished rendering 'debug map' after 57 ms.

statistics.mean([61,76,54,48,47,57])
57.2

@bernardosulzbach
Copy link
Owner Author

Acceptable results. One of the things that makes the first debug map command so slow is that it usually triggers the creation of several new locations.

@bernardosulzbach
Copy link
Owner Author

Wrote an extra command to better benchmark the performance improvement.

On Ubuntu 14.04 using OpenJDK 1.7.0_85, I got the following:

Old:

[2015-11-22 18:34:43] (FINE) : Finished rendering 'extra text' after 1460 ms.
[2015-11-22 18:34:49] (FINE) : Finished rendering 'extra text' after 1148 ms.
[2015-11-22 18:34:54] (FINE) : Finished rendering 'extra text' after 1115 ms.
[2015-11-22 18:35:00] (FINE) : Finished rendering 'extra text' after 1340 ms.
[2015-11-22 18:35:07] (FINE) : Finished rendering 'extra text' after 1323 ms.
[2015-11-22 18:35:14] (FINE) : Finished rendering 'extra text' after 1618 ms.
statistics.mean((1460,1148,1115,1340,1323,1618))
1334.0

New:

[2015-11-22 18:35:28] (FINE) : Finished rendering 'extra text' after 905 ms.
[2015-11-22 18:35:32] (FINE) : Finished rendering 'extra text' after 939 ms.
[2015-11-22 18:35:33] (FINE) : Finished rendering 'extra text' after 753 ms.
[2015-11-22 18:35:35] (FINE) : Finished rendering 'extra text' after 808 ms.
[2015-11-22 18:35:37] (FINE) : Finished rendering 'extra text' after 731 ms.
[2015-11-22 18:35:38] (FINE) : Finished rendering 'extra text' after 718 ms.

statistics.mean((905,939,753,808,731,718))
809.0

@bernardosulzbach
Copy link
Owner Author

I suppose that for smaller pieces of text the new method may be just as good as, or even worse, than the old one (just updating the current Document of the JTextPane). However, the problem is only present for larger amounts of output, so this fix is good for now and will be merged into master.

@bernardosulzbach bernardosulzbach changed the title World map writing performance. Writing the world map to JTextPane is sometimes too slow Nov 22, 2015
@bernardosulzbach
Copy link
Owner Author

Post related to the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant