# Message queue problem with repaint throttling

**URL:** <https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875>\
**Category:** MacOSX and iOS\
**Created:** [November 10, 2016, 2:56pm UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875 "2016-11-10T14:56:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 10, 2016, 2:56pm UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/1 "2016-11-10T14:56:53Z")

</div>

I already postet this in this thread

`https://forum.juce.com/t/on-latest-tip-gui-on-osx-is-much-laggier-than-on-windows-w-video/18080/15`

but because it think this is an entirely new issue, i created this new thread:

The fix which was introduced to throttling the redraw in Mac OS is causing new issues, for other plugins which are running in the same host at the same time.

My assumption is the triggerAsyncUpdate inside the handleAsyncUpdate can result in a massive message ping pong (this does not happen always, but sometimes), which can block the whole message queue for other plugins in the host  
(why - the answer does only know apple)

Alternative Solution:  
So why not use a timer which waits until the minimum is reached, instead of massively posting messages trigger/handle/trigger/handle into the queue  
(It also looks repaint is smoother with this technique)

And here is the code

```
  void handleAsyncUpdate() override
    {
       #if JucePlugin_Build_AAX || JucePlugin_Build_RTAS || JucePlugin_Build_AUv3 || JucePlugin_Build_AU || JucePlugin_Build_VST3 || JucePlugin_Build_VST
        const bool shouldThrottle = true;
       #else
        const bool shouldThrottle = areAnyWindowsInLiveResize();
       #endif

        // When windows are being resized, artificially throttling high-frequency repaints helps
        // to stop the event queue getting clogged, and keeps everything working smoothly.
        // For some reason Logic also needs this throttling to recored parameter events correctly.
        

        // already a timer running -> stop
        if (isTimerRunning()) return;
        
        int64 msSinceLastRepaint= Time::getCurrentTime().toMilliseconds() - lastRepaintTime.toMilliseconds();
       
        static int minimumRepaintInterval= 1000 / 30; // 30fps
        
        if (shouldThrottle
            && msSinceLastRepaint < minimumRepaintInterval );
        {
            startTimer(minimumRepaintInterval-msSinceLastRepaint);
            return;
        }

        setNeedsDisplayRectangles();
    }
    
    void timerCallback() override
    {
        setNeedsDisplayRectangles();
        stopTimer();
    };
    
    void setNeedsDisplayRectangles()
    {
        for (const Rectangle<float>* i = deferredRepaints.begin(), *e = deferredRepaints.end(); i != e; ++i)
            [view setNeedsDisplayInRect: makeNSRect (*i)];
        
        lastRepaintTime = Time::getCurrentTime();
        deferredRepaints.clear();
    };
```

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 10, 2016, 2:59pm UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/2 "2016-11-10T14:59:45Z")

</div>

Maybe the AsyncUpdater could be also removed completely

---

<div class="post-metadata">

**Author:** ![xeneize](https://avatars.discourse-cdn.com/v4/letter/x/ce7236/32.png) [@xeneize](https://forum.juce.com/u/xeneize)\
**Post date:** [November 11, 2016, 3:22pm UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/3 "2016-11-11T15:22:55Z")

</div>

Hi chkn  
I’m using this from yesterday, seems to works fine.

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 11, 2016, 3:48pm UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/4 "2016-11-11T15:48:08Z")

</div>

Cool, works here great too!

But i want to make sure what the juce team says about this?

Further explanation why the current behavior is bad:  
If a second repaint() call comes in a time interval below 33ms, maybe after 1ms, the rest of the time (32ms), the plugin will constantly post triggerAsyncUpdate() messages into the queue

This causes real world issues (bothers other plugins messaging), its a problem!

Any other plugins which build with the current juce, can harm other plugins messaging, its urgent and needs to be fixed soon.

---

<div class="post-metadata">

**Author:** ![myhrman](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@myhrman](https://forum.juce.com/u/myhrman)\
**Post date:** [November 11, 2016, 3:52pm UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/5 "2016-11-11T15:52:21Z")

</div>

We’re also currently facing big problems with this behaviour in plugins running on macOS, didn’t discover until recently that it was actually related to the message thread event handling and repaint throttling.

I will also do some tests with the above code snippet. Seems like a reasonable change.

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 14, 2016, 3:12pm UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/6 "2016-11-14T15:12:08Z")

</div>

bump, this is an acute problem, and should be resolved as soon as possible.

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [November 14, 2016, 5:49pm UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/7 "2016-11-14T17:49:48Z")

</div>

I remember we tried this with a timer before, but there were some race conditions where the last repaint message would sometimes get lost. I’ll check if I can still wrap my head around why we didn’t go for the Timer solution which we originally intended.

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 14, 2016, 5:59pm UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/8 "2016-11-14T17:59:44Z")

</div>

Okay thanks for replying to this, the current solution is definitely a big problem. If you have a plugin which redraws faster than the 30fps, other plugins may receive no messages anymore until the redraw is lower than 30fps.

But this happens not all the time, but often enough. It seems that macOs (is this the new spelling) somehow prioritize the message queue differently ( no FIFO behavior )

This must be fixed, also on master, because all vendors who now build plugins with this current behavior will create potential issues on other plugins.

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 16, 2016, 11:25am UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/9 "2016-11-16T11:25:51Z")

</div>

I can’t see any race-condition in my solution where the last repaint message might get lost. If you find some please let me know. Hopefully this will fixed soon (also on master, because of the non-cooperative handling regarding the message thread )

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [November 16, 2016, 11:39am UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/10 "2016-11-16T11:39:08Z")

</div>

Just for reference - this is why we put in that commit in the first place:

> [@Steppy automation recording on OSX](https://forum.juce.com/t/steppy-automation-recording-on-osx/18308/25):
>
> I filed a bug about the laggy UI in El Capitan a long time ago. We have some friends at Apple in the CoreAudio team but this bug is not really part of CoreAudio. The bug is still marked as open by the way.

> [@Laggy GUIs on OSX El Capitan?](https://forum.juce.com/t/laggy-guis-on-osx-el-capitan/15717/14):
>
> We're experiencing this on a desktop application (the Raven) and it also seems to be related to frequent calls being made to repaint for our meters. This only became an issue after the El Capitan update. In our case, we have a separate thread running at 60fps which looks for any changes made to our incoming metering values and calls repaint() on the meter whenever necessary. After the El Capitan update, it seems that something is happening where the repaint calls are "piling up" and causing our …

I need to make sure that your fix does not break this again. The problem is, I think this only happened on El Capitan. I can’t reproduce it on Sierra even when i completely remove the async updater thing (and redraw directly).

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 16, 2016, 11:52am UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/11 "2016-11-16T11:52:31Z")

</div>

Yes, i know, it looks like redraws which will be posted often will somehow prioritized so that other messages will be slowed down, but in the same way you reduce redraws, by posting new messages, these are again become prioritized, which again slows down other message handling. So the key to success is, not posting more messages (or posting permanent messages as a pseudo timer), instead use a real timer thread.

PS: I wouldn’t remove repaint throttling at all, because i think 98% of all audio people still use El Captian), and we have to be sure that the initial issue is really removed with Sierra

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [November 16, 2016, 2:19pm UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/12 "2016-11-16T14:19:58Z")

</div>

Thanks chkn. I’ve pushed your fix to develop. Please test!

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 17, 2016, 4:41pm UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/13 "2016-11-17T16:41:51Z")

</div>

Thanks!

> [@fabian](#):
>
> Please test!

Yes, i will do that

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 18, 2016, 11:55am UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/14 "2016-11-18T11:55:05Z")

</div>

I just wondering, if its better to measure the time with Time::getMillisecondCounter(), because its the same how the Timer-Class it does, to make it more exact?

Here is the diff

```
diff --git a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm
index 8281315..04eee0d 100644
--- a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm
+++ b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm
@@ -83,6 +83,7 @@ static NSRect flippedScreenRect (NSRect r) noexcept
           textWasInserted (false),
           isStretchingTop (false), isStretchingLeft (false),
           isStretchingBottom (false), isStretchingRight (false),
+ lastRepaintTime (Time::getMillisecondCounter()),^M
           notificationCenter (nil)
     {
         appFocusChangeCallback = appFocusChanged;
@@ -907,8 +908,11 @@ void repaint (const Rectangle<int>& area) override
 
         // already a timer running -> stop
         if (isTimerRunning()) return;
-
- int64 msSinceLastRepaint = Time::getCurrentTime().toMilliseconds() - lastRepaintTime.toMilliseconds();
+ ^M
+ const uint32 now = Time::getMillisecondCounter();^M
+ const int msSinceLastRepaint = (int) (now >= lastRepaintTime ? (now - lastRepaintTime)^M
+ : (std::numeric_limits<uint32>::max() - (lastRepaintTime - now)));^M
+ ^M
         static int minimumRepaintInterval = 1000 / 30; // 30fps
 
         // When windows are being resized, artificially throttling high-frequency repaints helps
@@ -917,9 +921,9 @@ void repaint (const Rectangle<int>& area) override
         if (shouldThrottle
             && msSinceLastRepaint < minimumRepaintInterval)
         {
- startTimer (static_cast<int> (static_cast<int64> (minimumRepaintInterval) - msSinceLastRepaint));
+ startTimer (minimumRepaintInterval - msSinceLastRepaint);^M
             return;
- }
+ };^M
 
         setNeedsDisplayRectangles();
     }
@@ -935,7 +939,8 @@ void setNeedsDisplayRectangles()
         for (const Rectangle<float>* i = deferredRepaints.begin(), *e = deferredRepaints.end(); i != e; ++i)
             [view setNeedsDisplayInRect: makeNSRect (*i)];
 
- lastRepaintTime = Time::getCurrentTime();
+ lastRepaintTime = Time::getMillisecondCounter();^M
+ ^M
         deferredRepaints.clear();
     };
 
@@ -1364,7 +1369,7 @@ void textInputRequired (Point<int>, TextInputTarget&) override {}
     NSNotificationCenter* notificationCenter;
 
     RectangleList<float> deferredRepaints;
- Time lastRepaintTime;
+ uint32 lastRepaintTime;^M
 
     static ModifierKeys currentModifiers;
     static ComponentPeer* currentlyFocusedPeer;
```

---

<div class="post-metadata">

**Author:** ![xeneize](https://avatars.discourse-cdn.com/v4/letter/x/ce7236/32.png) [@xeneize](https://forum.juce.com/u/xeneize)\
**Post date:** [November 21, 2016, 3:33am UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/15 "2016-11-21T03:33:44Z")

</div>

hi chkn,  
Today I had issues with your first mod (not the last patch). repaint() calls from my component stopped working. I do not know why, since it was working perfectly.  
For a moment I thought it was something I changed in my project, then I have returned to an old commit where everything worked perfect, but the problem persisted. It has been solved by returning to the original version of NSViewComponentPeer.

It is very weird, since all these days it was working perfectly.

EDIT: Now I’ve applied your last patch and it works again.

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [November 21, 2016, 9:28am UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/16 "2016-11-21T09:28:24Z")

</div>

@chkn: OK I’ve changed the workaround to use `Time::getMillisecondCounter`.

@xeneize: Does this also happen on the latest develop branch? I hope not!

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 21, 2016, 9:41am UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/17 "2016-11-21T09:41:45Z")

</div>

Thanks fabian!

My first version, still uses the AsyncUpdater (which was always triggered by any repaint), while the patch in the develop-branch (and the latest version) directly posts the rectangles when needed (setNeedsDisplayInRect) or uses the timer, maybe thats the difference, why the current version works better.

I recommend to use the latest patch from the develop branch, and if still something going on, to debug.

BTW: The reason why i preferred Time::getMillisecondCounter was, I also measure repaints which are still faster than 30fps, i guess this is something how the Timer works (i double-checked the whole timer code, maybe Time::getMillisecondCounter isn’t just precise enough), but otherwise it effectively reduces the amount of repaints.

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 21, 2016, 9:46am UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/18 "2016-11-21T09:46:54Z")

</div>

@fabian  
is this right? in your patch

`(std::numeric_limits<uint32>::max() - lastRepaintTime) + now);`

should it be

`(std::numeric_limits<uint32>::max() - (lastRepaintTime - now)));`

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [November 21, 2016, 9:48am UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/19 "2016-11-21T09:48:24Z")

</div>

I think that’s the same right? I always like to write it the first way: A + B where A is the amount of elements until we wrap around and B is anything left over.

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 21, 2016, 9:51am UTC](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875/20 "2016-11-21T09:51:51Z")

</div>

Ah yes, you are right! Thats more clear!

[Next page](https://forum.juce.com/t/message-queue-problem-with-repaint-throttling/19875.md?page=2)
