# UDPSocket is also slow

**URL:** <https://ifnotnil.com/t/udpsocket-is-also-slow/1597>\
**Category:** General Programming\
**Created:** [20 March 2021 21:14 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597 "2021-03-20T21:14:37Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjr](https://ifnotnil.com/user_avatar/ifnotnil.com/jjr/32/874_2.png) [@jjr](https://ifnotnil.com/u/jjr)\
**Post date:** [20 March 2021 21:14 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/1 "2021-03-20T21:14:37Z")

</div>

I ran into a situation similar to this one: [Faster TCPSockets](https://ifnotnil.com/t/faster-tcpsockets/1376) but backwards.

I was developing some automation for a particular device (not with Xojo), but the person I was going to borrow the hardware from sold it (thanks Covid).

Rather than give up, I built a simulator in Xojo based on the protocol documentation. I have done this for a few other simple devices. This one is more involved, but now I could test the remote automation I was writing without the actual hardware. The protocol uses UDP so I used `UDPSocket` for communication.

On startup, the remote control takes a snapshot of the device settings (over 3200 values) in order to maintain a live indication of the status.

I was very disappointed to discover the throughput from my emulator was only 10-15 values per second. After inserting as may #pragma options as possible, I ended up with 25-35 per second. This still took almost 2 minutes to initialize.

I was starting to worry about the usability of my automation program, then I obtained access to a live unit. It is on another continent, but, Internet!

Using a teamware-like remote into the remote network, controlling a real device from there loads an average of 1300 values per second. 2-3 seconds to initialize is very acceptable.

Using a remote VPN (from US to Europe) averages 300 per second depending on internet traffic. This is usable for development without tying up a remote computer.

Based on the discussion in the other thread, I re-implemented the core socket using @MonkeybreadSoftware `UDPSocketMBS`. That version averages around 300 per second.

Conclusion: Xojo `UDPSocket` is 10x slower than MBS, and 40x slower than a real device.

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [20 March 2021 21:49 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/2 "2021-03-20T21:49:10Z")

</div>

If our socket is too slow, you can call poll method in a timer to speed up.

Is that macOS, Windows or Linux?

---

<div class="post-metadata">

**Author:** ![jjr](https://ifnotnil.com/user_avatar/ifnotnil.com/jjr/32/874_2.png) [@jjr](https://ifnotnil.com/u/jjr)\
**Post date:** [20 March 2021 22:00 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/3 "2021-03-20T22:00:55Z")

</div>

This is on linux. As it is, yours is a big improvement.

The process is completely async.  
DataAvailable fires… read everything & process.

---

<div class="post-metadata">

**Author:** ![xojo\_spartacus](https://ifnotnil.com/letter_avatar_proxy/v4/letter/x/94ad74/32.png) [@xojo\_spartacus](https://ifnotnil.com/u/xojo_spartacus)\
**Post date:** [21 March 2021 00:44 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/4 "2021-03-21T00:44:33Z")

</div>

Yes the built-in UDP socket on Linux in Xojo is dog slow.

The UDPSocketMBS is much faster

For even better results with data transfer throughput consider using console app rather than GUI and use a

**while my\_UDPSocketMBS.AvailableBytes\>0**

**My\_datagramMBS=my\_UDPSocketMBS.read(false)**

**//use that datagram here**

my\_UDPSocketMBS.poll

**wend**

Also for best transfer speed you will also need to increase ReceiveBufferSize and SendBufferSize to a higher value.

If you need good UDP data transfer, then the above will easily get you 20 MBs (160 megabits)+ sustained on even a netbook (assuming you have written a efficient sliding window to work the transfer and a binary class that can keep it fed using another helper process using MBS shared memory)

Xojo UDP is hot garbage, **MBS for the win.**

![LS](https://ifnotnil.com/uploads/default/original/2X/3/3d296237e524d12745ab04829a3f53550bfc0346.jpeg)

---

<div class="post-metadata">

**Author:** ![jjr](https://ifnotnil.com/user_avatar/ifnotnil.com/jjr/32/874_2.png) [@jjr](https://ifnotnil.com/u/jjr)\
**Post date:** [21 March 2021 01:14 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/5 "2021-03-21T01:14:28Z")

</div>

> [@xojo\_spartacus](#):
>
> ```auto
> while my_UDPSocketMBS.AvailableBytes>0
> My_datagramMBS=my_UDPSocketMBS.read(false)
> //use that datagram here**
> my_UDPSocketMBS.poll
> wend
> 
> ```

That is almost identical to what I have.  
Adding `poll` increased throughput 2-3%.

There was not much difference increasing the standard buffer sizes past 2x.

I definitly need the GUI. Almost everything on this screen is interactive and sends any adjustments made to the remote, as well as displays any updates the remote asks for.

 ![image](https://ifnotnil.com/uploads/default/original/2X/6/650975a5a4abcdac60dfffa596be9e007fec3d21.png)

---

<div class="post-metadata">

**Author:** ![xojo\_spartacus](https://ifnotnil.com/letter_avatar_proxy/v4/letter/x/94ad74/32.png) [@xojo\_spartacus](https://ifnotnil.com/u/xojo_spartacus)\
**Post date:** [21 March 2021 01:18 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/6 "2021-03-21T01:18:03Z")

</div>

Cool interface.

You could use a combination of console and GUI with shared memory in between as a low latency IPC. (separate Xojo linux processes for socket operations and for drawing the interface will also benefit from multiple cores on the machine and give the absolute best interactive user experience)

That gives the best of all worlds, but I think for the amount of data you get in a stream on a soundboard the GUI poll rate should be enough.

---

<div class="post-metadata">

**Author:** ![jjr](https://ifnotnil.com/user_avatar/ifnotnil.com/jjr/32/874_2.png) [@jjr](https://ifnotnil.com/u/jjr)\
**Post date:** [21 March 2021 01:31 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/7 "2021-03-21T01:31:14Z")

</div>

After the initial data sync, it works well. The 2 minute startup was annoying.  
I can deal with 10 seconds.

I only use about 3500 of the 30000 data points actually available in the console.

For now, it pretends to be a console. After I finish with the other project, I may try inverting the conversation to see how well this would drive a real board.

---

<div class="post-metadata">

**Author:** ![jjr](https://ifnotnil.com/user_avatar/ifnotnil.com/jjr/32/874_2.png) [@jjr](https://ifnotnil.com/u/jjr)\
**Post date:** [27 March 2021 19:18 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/8 "2021-03-27T19:18:45Z")

</div>

I ended up removing the `poll`.  
After adding `my_UDPSocketMBS.poll` to the `DataAvailable` event, the routine occasionally breaks with a `StackOverflow` .

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [27 March 2021 19:41 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/9 "2021-03-27T19:41:37Z")

</div>

I’ll fix that stack overflow.  
If you like to call poll, better do it from a timer. Check [TimerMBS](https://www.monkeybreadsoftware.net/class-timermbs.shtml) for a fast running timer.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [27 March 2021 19:59 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/10 "2021-03-27T19:59:25Z")

</div>

if you Poll and process data in the DataAvailable and in there ALSO call Poll again you can cause a world of hurt for yourself

---

<div class="post-metadata">

**Author:** ![jjr](https://ifnotnil.com/user_avatar/ifnotnil.com/jjr/32/874_2.png) [@jjr](https://ifnotnil.com/u/jjr)\
**Post date:** [27 March 2021 20:03 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/11 "2021-03-27T20:03:19Z")

</div>

I figured that out the hard way 🙂  
The rest of the code does not seem to be able to handle the data any faster and adding extra `poll`s just stacks/clogs the pipeline.

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [27 March 2021 20:15 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/12 "2021-03-27T20:15:39Z")

</div>

Future versions of the plugin will ignore the inner Poll calls.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [27 March 2021 20:32 UTC](https://ifnotnil.com/t/udpsocket-is-also-slow/1597/13 "2021-03-27T20:32:17Z")

</div>

fair

the same issue arises in Xojo’s implementations of sockets and serial
