From a91ac8d5a66396960ff37d805ecf03079ee00391 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Fri, 17 Apr 2015 12:06:29 +0200 Subject: Optimize QXcbKeyboard::lookupString. For the common case of strings smaller than 32 chars this removes a temporary QByteArray allocation and the second call to xkb_state_key_get_utf8. Change-Id: I81cbbf2df683476b38c2ffb96119293cd5b09a90 Reviewed-by: Oswald Buddenhagen Reviewed-by: Marc Mutz --- src/plugins/platforms/xcb/qxcbkeyboard.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/plugins/platforms') diff --git a/src/plugins/platforms/xcb/qxcbkeyboard.cpp b/src/plugins/platforms/xcb/qxcbkeyboard.cpp index 3b356525e3..6c80c1b30e 100644 --- a/src/plugins/platforms/xcb/qxcbkeyboard.cpp +++ b/src/plugins/platforms/xcb/qxcbkeyboard.cpp @@ -1515,10 +1515,12 @@ void QXcbKeyboard::handleKeyEvent(xcb_window_t sourceWindow, QEvent::Type type, QString QXcbKeyboard::lookupString(struct xkb_state *state, xcb_keycode_t code) const { - QByteArray chars; - chars.resize(1 + xkb_state_key_get_utf8(state, code, 0, 0)); - // equivalent of XLookupString + QVarLengthArray chars(32); const int size = xkb_state_key_get_utf8(state, code, chars.data(), chars.size()); + if (Q_UNLIKELY(size + 1 > chars.size())) { // +1 for NUL + chars.resize(size + 1); + xkb_state_key_get_utf8(state, code, chars.data(), chars.size()); + } return QString::fromUtf8(chars.constData(), size); } -- cgit v1.2.3