SPARK-2367: Improve performance of message reordering

This commit is contained in:
Guus der Kinderen
2025-10-10 11:46:14 +02:00
parent eaa5e42819
commit d2181c486d
2 changed files with 40 additions and 39 deletions

View File

@ -27,6 +27,7 @@ import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.plugin.ContextMenuListener; import org.jivesoftware.spark.plugin.ContextMenuListener;
import org.jivesoftware.spark.ui.history.HistoryWindow; import org.jivesoftware.spark.ui.history.HistoryWindow;
import org.jivesoftware.spark.util.ModelUtil; import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.TaskEngine;
import org.jivesoftware.spark.util.log.Log; import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.plugin.manager.Enterprise; import org.jivesoftware.sparkimpl.plugin.manager.Enterprise;
import org.jivesoftware.sparkimpl.plugin.emoticons.EmoticonManager; import org.jivesoftware.sparkimpl.plugin.emoticons.EmoticonManager;
@ -105,37 +106,50 @@ public class TranscriptWindow extends ChatArea implements ContextMenuListener
} }
// Guarded by 'this'
boolean isReordingScheduled = false;
private synchronized void reOrder() {
// Clear and refill the UI component.
try
{
entries.sort( Comparator.comparing(TranscriptWindowEntry::isDelayed).thenComparing( TranscriptWindowEntry::getTimestamp ) );
clear();
for ( TranscriptWindowEntry e : entries )
{
e.addTo( this );
}
}
catch ( BadLocationException ex )
{
Log.error( "An exception prevented chat content to be redrawn in the user interface!", ex );
}
finally
{
isReordingScheduled = false;
}
}
protected synchronized void add( TranscriptWindowEntry entry ) protected synchronized void add( TranscriptWindowEntry entry )
{ {
final TranscriptWindow transcriptWindow = this;
//boolean reorderEverything = false;
if ( !entries.isEmpty() ) if ( !entries.isEmpty() )
{ {
if ( entry.getTimestamp().isBefore( entries.getLast().getTimestamp() ) && !(entries.getLast() instanceof CustomTextEntry) ) if ( entry.getTimestamp().isBefore( entries.getLast().getTimestamp() ) && !(entries.getLast() instanceof CustomTextEntry) )
{ {
Log.warning( "A chat entry appears to have been delivered out of order. The transcript window must be reordered!" ); Log.debug( "A chat entry appears to have been delivered out of order. The transcript window must be reordered!" );
//reorderEverything = true; if (!isReordingScheduled) {
Log.warning( "Scheduling new re-ordering of entries in the transcript window." );
isReordingScheduled = true;
TaskEngine.getInstance().schedule(new TimerTask()
{
@Override
// This is an alternative approach to the 'reorderEverything boolean + routine. The idea behind using the public void run()
// SwingUtilities is to schedule redrawing after all currently queued messages (loads of which are {
// probably also out of order), are processed, which should reduce the amount of redraws. SwingUtilities.invokeLater(() -> reOrder());
SwingUtilities.invokeLater( () -> }
{ }, 250);
// Clear and refill the UI component. }
try
{
entries.sort( Comparator.comparing(TranscriptWindowEntry::isDelayed).thenComparing( TranscriptWindowEntry::getTimestamp ) );
clear();
for ( TranscriptWindowEntry e : entries )
{
e.addTo( transcriptWindow );
}
}
catch ( BadLocationException ex )
{
Log.error( "An exception prevented chat content to be redrawn in the user interface!", ex );
}
} );
} }
if ( !entry.getTimestamp().withZoneSameInstant( ZoneId.systemDefault() ).toLocalDate().isEqual( entries.getLast().getTimestamp().withZoneSameInstant( ZoneId.systemDefault() ).toLocalDate() ) ) if ( !entry.getTimestamp().withZoneSameInstant( ZoneId.systemDefault() ).toLocalDate().isEqual( entries.getLast().getTimestamp().withZoneSameInstant( ZoneId.systemDefault() ).toLocalDate() ) )
{ {
@ -153,20 +167,7 @@ public class TranscriptWindow extends ChatArea implements ContextMenuListener
try try
{ {
// if ( reorderEverything ) entry.addTo( this );
// {
// // Clear and refill the UI component.
// entries.sort( Comparator.comparing( TranscriptWindowEntry::getTimestamp ) );
// clear();
// for ( TranscriptWindowEntry e : entries )
// {
// e.addTo( this );
// }
// }
// else
{
entry.addTo( this );
}
} }
catch ( BadLocationException ex ) catch ( BadLocationException ex )
{ {

View File

@ -128,7 +128,7 @@ public class ChatRoomImpl extends ChatRoom {
this.participantNickname = participantNickname; this.participantNickname = participantNickname;
// Loads the current history for this user. // Loads the current history for this user.
loadHistory(); SwingUtilities.invokeLater(this::loadHistory);
// Register StanzaListeners // Register StanzaListeners
final StanzaFilter directFilter = new AndFilter( final StanzaFilter directFilter = new AndFilter(