# Memoryblock Question

**URL:** <https://forum.xojo.com/t/memoryblock-question/14530>\
**Category:** General\
**Created:** [February 22, 2014, 5:26pm UTC](https://forum.xojo.com/t/memoryblock-question/14530 "2014-02-22T17:26:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Denis\_Hessberger](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@Denis\_Hessberger](https://forum.xojo.com/u/Denis_Hessberger)\
**Post date:** [February 22, 2014, 5:26pm UTC](https://forum.xojo.com/t/memoryblock-question/14530/1 "2014-02-22T17:26:38Z")

</div>

Hi there,

in my App I’ve got a Subclass of the UDPSocket. The Socket has the following in its DataAvailable Handler + some condition checking, not shown here:

[code] Dim d As Datagram  
Dim mb As MemoryBlock

d = Me.Read // Read Datagram as soon as it is available  
mb = d.data // Read Datagram into a MemoryBlock so we can parse it Bytewise  
mPacketsReceived = mPacketsReceived+1  
mPacketsSent = mPacketsSent +1

mReceivedPacket ( mb, d.Address )[/code]

The function mReceivedPacket does this:

` MyClass(mb.uInt16Value ( 14 )).mb = mb
    DataReceived(Address, MyClass(mb.uInt16Value ( 14 ))) //Firing Event Handler
`

The dataAvailable Handler beforehand checks if several conditions have been met, so its save to assume that uInt16Value(14) is what it needs to be.

MyClass is doing the folliwing the setter of the mb variable:

[code] if value \<\> mmb then

```
mmb = value
setValues.Run

```

end[/code]

Whereas setValues is a Thread. This all works well, despite the fact that in mReceivedPacket the line “MyClass(mb.uInt16Value ( 14 )).mb = mb” takes up to 4ms, and thus prevents the dataAvailable from being able to fire as fast as it would need to be, which in return means the delay is adding up until packets get lost because the buffer of the UDPSocket runs full.

Now and finally… My question:

Why is this taking so long? Because it is not pointing to the original MB, but instead creating a new instance, or at least assigning a new value to a different instance of the memoryblock?  
And what could I do to avoid this issue?

Cheers and thanks once again, you guys rock!

Denis

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [February 22, 2014, 6:52pm UTC](https://forum.xojo.com/t/memoryblock-question/14530/2 "2014-02-22T18:52:43Z")

</div>

> [@66994:@Denis Hessberger](#):
>
> despite the fact that in mReceivedPacket the line “MyClass(mb.uInt16Value ( 14 )).mb = mb” takes up to 4ms,

I doubt that could take that long. How are you doing that timing? If you are using the Profiler, it’s known to give misleading results when timing threaded code.

---

<div class="post-metadata">

**Author:** ![Denis\_Hessberger](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@Denis\_Hessberger](https://forum.xojo.com/u/Denis_Hessberger)\
**Post date:** [February 22, 2014, 6:56pm UTC](https://forum.xojo.com/t/memoryblock-question/14530/3 "2014-02-22T18:56:19Z")

</div>

Yes, with Profiler, but actually it is the line ` MyClass(mb.uInt16Value ( 14 )).mb = mb` taking that long. As soon as I comment this one out, it is all good and fast (obviously). Since this one is not threaded yet, I doubt the profiler is doing wrong stuff… 🙂

---

<div class="post-metadata">

**Author:** ![Denis\_Hessberger](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@Denis\_Hessberger](https://forum.xojo.com/u/Denis_Hessberger)\
**Post date:** [February 22, 2014, 6:57pm UTC](https://forum.xojo.com/t/memoryblock-question/14530/4 "2014-02-22T18:57:55Z")

</div>

It also takes the same amount of time if the setter is just doing

```auto
    mmb = value
```

and not even starting the thread…

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [February 22, 2014, 7:25pm UTC](https://forum.xojo.com/t/memoryblock-question/14530/5 "2014-02-22T19:25:12Z")

</div>

What happens if you comment out the comparison `if value <> mmb then`?

---

<div class="post-metadata">

**Author:** ![Denis\_Hessberger](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@Denis\_Hessberger](https://forum.xojo.com/u/Denis_Hessberger)\
**Post date:** [February 22, 2014, 7:31pm UTC](https://forum.xojo.com/t/memoryblock-question/14530/6 "2014-02-22T19:31:38Z")

</div>

Hi Kem,

Even if I comment out everything in the setter except mmb = value it still takes up to 4ms average. :-/

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [February 22, 2014, 9:43pm UTC](https://forum.xojo.com/t/memoryblock-question/14530/7 "2014-02-22T21:43:07Z")

</div>

I wonder if the conversion from UInt16 to Int32 (Integer) is making a difference? What if you rewrite you code this way?

```auto
  dim index as Integer = mb.UInt16Value( 14 )
  dim c as MyClassClass = MyClass( index )
  c.mb = mb
  DataReceived(Address, c) //Firing Event Handler
```

I really wouldn’t expect this to make much difference, but you never know.

---

<div class="post-metadata">

**Author:** ![Denis\_Hessberger](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@Denis\_Hessberger](https://forum.xojo.com/u/Denis_Hessberger)\
**Post date:** [February 22, 2014, 10:11pm UTC](https://forum.xojo.com/t/memoryblock-question/14530/8 "2014-02-22T22:11:14Z")

</div>

Actually, that makes a huge difference… I also implemented a Property in myClass “isRequested as Boolean” - so the thread doesnt start to run unless I really need that data. Now average time went down to 0.5ms… Sometimes it seems to be the things you dont think that should be an issue…

Thanks for pointing me into that direction… 🙂

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [February 23, 2014, 12:02am UTC](https://forum.xojo.com/t/memoryblock-question/14530/9 "2014-02-23T00:02:29Z")

</div>

That doesn’t make much sense to me - a uint16 to uint32 conversion of a single value is going to be blindingly fast, so I can’t see how you are getting an 8x speedup.

if you really want an accurate speed measurement, do this:

```auto
  dim t0 as double = microseconds
  ... code you want to measure...
  dim deltaT as double = microseconds - t0
  system.debuglog "That took " + str(deltaT) + " microseconds"
```

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [February 23, 2014, 12:46am UTC](https://forum.xojo.com/t/memoryblock-question/14530/10 "2014-02-23T00:46:09Z")

</div>

I wonder if it’s more in looking up the object from the array only once.

---

<div class="post-metadata">

**Author:** ![Denis\_Hessberger](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@Denis\_Hessberger](https://forum.xojo.com/u/Denis_Hessberger)\
**Post date:** [February 24, 2014, 3:40pm UTC](https://forum.xojo.com/t/memoryblock-question/14530/11 "2014-02-24T15:40:04Z")

</div>

doesnt seem to be, its much quicker now… for whatever reason - Ill investigate more on this once I’ve pushed the beta out the door…

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:05pm UTC](https://forum.xojo.com/t/memoryblock-question/14530/12 "2020-10-29T17:05:38Z")

</div>


