Bug 26265

Summary: Slowness using uim in QT applications
Product: UIM Reporter: Harald Hvaal <haraldhv>
Component: libuimAssignee: uim-bugs
Status: RESOLVED NOTOURBUG QA Contact:
Severity: normal    
Priority: medium CC: zorael
Version: unspecified   
Hardware: x86-64 (AMD64)   
OS: Linux (All)   
Whiteboard:
i915 platform: i915 features:
Attachments: Callgrind output
The mentioned screenshot.
Callgraph from uim with debug symbols
New screenshot from kcachegrind

Description Harald Hvaal 2010-01-27 02:18:54 UTC
Created attachment 32842 [details]
Callgrind output

When typing in QT-applications, there is something slowing the typing down significantly, and the characters appearing is kind of split into 500-ms intervals. This does not happen when using uim in gtk-applications, so I think it might be a qt-only bug.
It happens with both anthy and hangul input methods that I tested. 

I recorded the performance of inputting a series of characters in a textfield with cachegrind, and the raw file (gzipped) is attached.

Also attached a screenshot from the callgrind file opened in kcachegrind, showing 82% of the time spent in uim_press_key. Perhaps someone can spot the problem easily from this?
Comment 1 Harald Hvaal 2010-01-27 02:19:37 UTC
Created attachment 32843 [details]
The mentioned screenshot.
Comment 2 JR 2010-01-30 15:45:13 UTC
It occurs when using ibus as well, so I imagine it's a Qt4 or KDE bug.

Furthermore, Anthy input into Kate 4.4rc* using either UIM or ibus is broken (for me). See https://bugs.kde.org/show_bug.cgi?id=222620 - and feel free to comment and confirm/deny.

The state of IMEs in the upcoming KDE SC 4.4 seems a bit flakey.
Comment 3 Harald Hvaal 2010-02-21 04:30:52 UTC
Created attachment 33470 [details]
Callgraph from uim with debug symbols

have now installed debug symbols of uim (should've done this before), and this gives us a better callgraph of what's happening.
this is from a simple qt application that just shows a text field, and I repeatedly typed ime-letters, slow and delayed as usual.

it would be great if an uim developer could at least look at the callgraph and tell me if the time spent in the various functions are sensible
for example, is it not unusual that for twenty seconds of activity some of these uim methods are called almost 400 thousand times?
Comment 4 Harald Hvaal 2010-02-21 04:32:49 UTC
Created attachment 33472 [details]
New screenshot from kcachegrind

New screenshot from kcachegrind, showing better what's happening
Comment 5 Etsushi Kato 2010-02-21 20:06:37 UTC
Could you tell me what version of qt4 exactly you use?  Perhaps something is changed in the filter event handling in the recent version of qt4... as I don't see any such problem with qt-4.5.3-9.fc12 (on Fedora).   Anyway I'll debug with latest qt4 later in my free time.
Comment 6 Harald Hvaal 2010-02-22 00:15:05 UTC
I'm using Qt 4.6.1. This bug is rather recent, I actually think it was not there in qt 4.5, but appeared once I upgraded to 4.6.
Comment 7 Etsushi Kato 2010-02-28 21:14:33 UTC
(In reply to comment #6)
> I'm using Qt 4.6.1. This bug is rather recent, I actually think it was not
> there in qt 4.5, but appeared once I upgraded to 4.6.
> 

I've just tried Qt 4.6.2, and confirmed the problem.  Even with XIM, its slowness existed.  It seems that event handling in drawing preedit of Qt 4.6.x is somewhat broken.  This should be solved in Qt itself.
Comment 8 Harald Hvaal 2010-03-01 03:26:21 UTC
(In reply to comment #7)
> (In reply to comment #6)
> > I'm using Qt 4.6.1. This bug is rather recent, I actually think it was not
> > there in qt 4.5, but appeared once I upgraded to 4.6.
> > 
> 
> I've just tried Qt 4.6.2, and confirmed the problem.  Even with XIM, its
> slowness existed.  It seems that event handling in drawing preedit of Qt 4.6.x
> is somewhat broken.  This should be solved in Qt itself.
> 

Thanks for confirming this bug. I am very interested in getting it fixed, so would you mind describing the bug more in detail so that I can submit it to qt bugtrackers?
Comment 9 Etsushi Kato 2010-03-01 04:57:04 UTC
(In reply to comment #8)
> Thanks for confirming this bug. I am very interested in getting it fixed, so
> would you mind describing the bug more in detail so that I can submit it to qt
> bugtrackers?

It seems Qt 4.6.x doen't draw and update preedit string synchronously even the event sent from the input method has already handled internally.  Since committing text from input method doesn't show such delays, event handling just for drawing preedit text seems culprit.  Perhaps this problem only appears on X11 platform but I'm not sure.
Comment 10 Etsushi Kato 2010-03-01 05:01:50 UTC
(In reply to comment #9)
> It seems Qt 4.6.x doen't draw and update preedit string synchronously even the
> event sent from the input method has already handled internally.  Since
> committing text from input method doesn't show such delays, event handling just
> for drawing preedit text seems culprit.  Perhaps this problem only appears on
> X11 platform but I'm not sure.


I've just noticed the delay of displaying preedit text seem only existed on QLineEdit widget.  Inputting text on QTextEdit *does* work fine even with Qt 4.6.2.
Comment 11 Etsushi Kato 2010-03-06 06:12:33 UTC
(In reply to comment #10)
> I've just noticed the delay of displaying preedit text seem only existed on
> QLineEdit widget.  Inputting text on QTextEdit *does* work fine even with Qt
> 4.6.2.

For the bug in qlineedit, a patch for Qt 4.6.2 pasted below should fix the problem.  It is a bug in QLineControl::updateDisplayText() which doesn't check the existence of preedit text when updating the area. 


--- qt-kde-qt-mac/src/gui/widgets/qlinecontrol.cpp.orig 2010-02-17 12:18:04.0000
00000 +0900
+++ qt-kde-qt-mac/src/gui/widgets/qlinecontrol.cpp      2010-03-06 23:05:48.0000
00000 +0900
@@ -102,7 +102,7 @@
     m_textLayout.endLayout();
     m_ascent = qRound(l.ascent());

-    if (str != orig)
+    if (str != orig || !preeditAreaText().isEmpty())
         emit displayTextChanged(str);
 }
Comment 12 JR 2010-03-06 06:20:42 UTC
Apologies if this comes through as unrelated, but could this also explain the bug at https://bugs.kde.org/show_bug.cgi?id=222620? Both were introduced at roughly the same time, and if this patch to Qt4 fixes them both then that's happy news.
Comment 13 Etsushi Kato 2010-03-06 06:38:27 UTC
(In reply to comment #11)

Maybe this should be better.  It also update the line when deleting preedit text.

--- qt-kde-qt-mac/src/gui/widgets/qlinecontrol.cpp.orig	2010-02-17 12:18:04.000000000 +0900
+++ qt-kde-qt-mac/src/gui/widgets/qlinecontrol.cpp	2010-03-06 23:35:54.000000000 +0900
@@ -406,8 +406,9 @@
 void QLineControl::processInputMethodEvent(QInputMethodEvent *event)
 {
     int priorState = 0;
+    QString oldPreeditText = preeditAreaText();
     bool isGettingInput = !event->commitString().isEmpty()
-            || event->preeditString() != preeditAreaText()
+            || event->preeditString() != oldPreeditText
             || event->replacementLength() > 0;
     bool cursorPositionChanged = false;
 
@@ -477,6 +478,8 @@
     }
     m_textLayout.setAdditionalFormats(formats);
     updateDisplayText();
+    if (preeditAreaText() != oldPreeditText)
+        emit displayTextChanged(preeditAreaText());
     if (cursorPositionChanged)
         emitCursorPositionChanged();
     if (isGettingInput)


Comment 14 Etsushi Kato 2010-03-06 06:42:17 UTC
(In reply to comment #12)
> Apologies if this comes through as unrelated, but could this also explain the
> bug at https://bugs.kde.org/show_bug.cgi?id=222620? Both were introduced at
> roughly the same time, and if this patch to Qt4 fixes them both then that's
> happy news.
> 

I'm not sure as I haven't setup KDE yet.  But I guess KWrite uses its own input method handling perhaps.
Comment 15 Etsushi Kato 2010-03-06 23:24:44 UTC
(In reply to comment #14)
> (In reply to comment #12)
> > Apologies if this comes through as unrelated, but could this also explain the
> > bug at https://bugs.kde.org/show_bug.cgi?id=222620? Both were introduced at
> > roughly the same time, and if this patch to Qt4 fixes them both then that's
> > happy news.
> > 
> 
> I'm not sure as I haven't setup KDE yet.  But I guess KWrite uses its own input
> method handling perhaps.

For the kwrite problem, changes in kdelibs-4.4.0/kdelibs-4.4.0/kate/view/kateviewinternal.cpp seems to be culprit.

I think following patch is the right way to check what it should be.  As far as I tested, it fixes the problem with uim.


--- kateviewinternal.cpp.orig   2009-12-31 22:29:14.000000000 +0900
+++ kateviewinternal.cpp        2010-03-07 15:36:48.073367864 +0900
@@ -3778,7 +3778,7 @@

   // if the input method event is text that should be inserted, call KateDocument::typeChars() with
   // the text. that method will handle the input and take care of overwrite mode, etc.
-  if ( e->commitString().length() > 0 && doc()->typeChars( m_view, e->commitString() ) ) {
+  if ( e->commitString().length() > 0 && !m_imPreeditRange && doc()->typeChars( m_view, e->commitString() ) ) {
     e->accept();
     return;
   }

But I'm not sure what the original code is intended, so please consult KDE developer about this.
Comment 16 Harald Hvaal 2010-03-07 08:46:35 UTC
Etsushi, thanks a lot for your efforts. I have posted your patch and bug description to the Qt bugtracker, and have linked it to this bug for more information. It is found here:

http://bugreports.qt.nokia.com/browse/QTBUG-8807

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.