6a2f9fd
From: Peter Lieven <pl@kamp.de>
6a2f9fd
Date: Thu, 30 Jun 2016 12:00:46 +0200
6a2f9fd
Subject: [PATCH] vnc-enc-tight: use thread local storage for palette
6a2f9fd
6a2f9fd
currently the color counting palette is allocated from heap, used and destroyed
6a2f9fd
for each single subrect. Use a static palette per thread for this purpose and
6a2f9fd
avoid the malloc and free for each update.
6a2f9fd
6a2f9fd
Signed-off-by: Peter Lieven <pl@kamp.de>
6a2f9fd
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
6a2f9fd
Message-id: 1467280846-9674-1-git-send-email-pl@kamp.de
6a2f9fd
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
6a2f9fd
(cherry picked from commit 095497ffc66b7f031ff2a17f1e50f5cb105ce588)
6a2f9fd
---
6a2f9fd
 ui/vnc-enc-tight.c | 23 ++++++++++++-----------
6a2f9fd
 1 file changed, 12 insertions(+), 11 deletions(-)
6a2f9fd
6a2f9fd
diff --git a/ui/vnc-enc-tight.c b/ui/vnc-enc-tight.c
6a2f9fd
index 678c5df..877c093 100644
6a2f9fd
--- a/ui/vnc-enc-tight.c
6a2f9fd
+++ b/ui/vnc-enc-tight.c
6a2f9fd
@@ -349,7 +349,7 @@ tight_detect_smooth_image(VncState *vs, int w, int h)
6a2f9fd
     tight_fill_palette##bpp(VncState *vs, int x, int y,                 \
6a2f9fd
                             int max, size_t count,                      \
6a2f9fd
                             uint32_t *bg, uint32_t *fg,                 \
6a2f9fd
-                            VncPalette **palette) {                     \
6a2f9fd
+                            VncPalette *palette) {                      \
6a2f9fd
         uint##bpp##_t *data;                                            \
6a2f9fd
         uint##bpp##_t c0, c1, ci;                                       \
6a2f9fd
         int i, n0, n1;                                                  \
6a2f9fd
@@ -396,23 +396,23 @@ tight_detect_smooth_image(VncState *vs, int w, int h)
6a2f9fd
             return 0;                                                   \
6a2f9fd
         }                                                               \
6a2f9fd
                                                                         \
6a2f9fd
-        *palette = palette_new(max, bpp);                               \
6a2f9fd
-        palette_put(*palette, c0);                                      \
6a2f9fd
-        palette_put(*palette, c1);                                      \
6a2f9fd
-        palette_put(*palette, ci);                                      \
6a2f9fd
+        palette_init(palette, max, bpp);                                \
6a2f9fd
+        palette_put(palette, c0);                                       \
6a2f9fd
+        palette_put(palette, c1);                                       \
6a2f9fd
+        palette_put(palette, ci);                                       \
6a2f9fd
                                                                         \
6a2f9fd
         for (i++; i < count; i++) {                                     \
6a2f9fd
             if (data[i] == ci) {                                        \
6a2f9fd
                 continue;                                               \
6a2f9fd
             } else {                                                    \
6a2f9fd
                 ci = data[i];                                           \
6a2f9fd
-                if (!palette_put(*palette, (uint32_t)ci)) {             \
6a2f9fd
+                if (!palette_put(palette, (uint32_t)ci)) {              \
6a2f9fd
                     return 0;                                           \
6a2f9fd
                 }                                                       \
6a2f9fd
             }                                                           \
6a2f9fd
         }                                                               \
6a2f9fd
                                                                         \
6a2f9fd
-        return palette_size(*palette);                                  \
6a2f9fd
+        return palette_size(palette);                                   \
6a2f9fd
     }
6a2f9fd
 
6a2f9fd
 DEFINE_FILL_PALETTE_FUNCTION(8)
6a2f9fd
@@ -421,7 +421,7 @@ DEFINE_FILL_PALETTE_FUNCTION(32)
6a2f9fd
 
6a2f9fd
 static int tight_fill_palette(VncState *vs, int x, int y,
6a2f9fd
                               size_t count, uint32_t *bg, uint32_t *fg,
6a2f9fd
-                              VncPalette **palette)
6a2f9fd
+                              VncPalette *palette)
6a2f9fd
 {
6a2f9fd
     int max;
6a2f9fd
 
6a2f9fd
@@ -1458,9 +1458,11 @@ static int send_sub_rect_jpeg(VncState *vs, int x, int y, int w, int h,
6a2f9fd
 }
6a2f9fd
 #endif
6a2f9fd
 
6a2f9fd
+static __thread VncPalette color_count_palette;
6a2f9fd
+
6a2f9fd
 static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
6a2f9fd
 {
6a2f9fd
-    VncPalette *palette = NULL;
6a2f9fd
+    VncPalette *palette = &color_count_palette;
6a2f9fd
     uint32_t bg = 0, fg = 0;
6a2f9fd
     int colors;
6a2f9fd
     int ret = 0;
6a2f9fd
@@ -1489,7 +1491,7 @@ static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
6a2f9fd
     }
6a2f9fd
 #endif
6a2f9fd
 
6a2f9fd
-    colors = tight_fill_palette(vs, x, y, w * h, &bg, &fg, &palette);
6a2f9fd
+    colors = tight_fill_palette(vs, x, y, w * h, &bg, &fg, palette);
6a2f9fd
 
6a2f9fd
 #ifdef CONFIG_VNC_JPEG
6a2f9fd
     if (allow_jpeg && vs->tight.quality != (uint8_t)-1) {
6a2f9fd
@@ -1502,7 +1504,6 @@ static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
6a2f9fd
     ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
6a2f9fd
 #endif
6a2f9fd
 
6a2f9fd
-    palette_destroy(palette);
6a2f9fd
     return ret;
6a2f9fd
 }
6a2f9fd