Understanding Uint8Array Memory Blocks Usage: 3 Bytes Data Consuming 3 Blocks (14 bytes per block) #6185
Replies: 1 comment
-
Posted at 2023-08-14 by @fanoush the name "buffer" is stored there too, try to name it 'b', try to set the three byte array to string variable 's' via E.toString() - that could maybe take less space(?) Posted at 2023-08-14 by user156224 Thank you for your suggestions! I've tried both changing the variable name to 'b' and converting the three-byte array to a string using E.toString(). Unfortunately, neither approach seems to have reduced the number of blocks used. Posted at 2023-08-14 by @fanoush how do you determine the size? just tried
Posted at 2023-08-14 by user156224 Either by checking the free blocks before creating the array and after using "process.memory().free" Posted at 2023-08-14 by @fanoush also
maybe that is what E.toString takes from the array too in the previous example. Still it is interesting that only the string takes 1 and the arraybuffer needs 2 (and typed array even 3) Posted at 2023-08-15 by @gfwilliams There's a small bit of info on http://www.espruino.com/Performance#array-buffers-are-the-most-efficient-way-to-store-data But basically Uint8Array/etc store their data as a string, but they have to have one variable allocated which explains what type (Uint8/Int16/etc) they are, with size and offset. They also have to link to an ArrayBuffer which then references the string, which is mainly so you can do:
If I just automatically made an ArrayBuffer every time someone referenced You'll say why can't the ArrayBuffer store data directly and the reason is that a String isn't just one datatype - we have string_1,_2,etc depending on the amount of characters in that string block - so we'd have to do the same with ArrayBuffer which would then use up more space in our enum than we have. It can help to use
So it shows @fanoush I'm not sure what happened in your test, but a 3 char string should only be one var:
So actually the docs are misleading. A String is the most efficient form of storage, but Uint8Array/etc allows you to reference those in a useful way (because normally Strings cannot be modified once created) Posted at 2023-08-15 by @fanoush
Don't have newest build but just retried on 52840 dongle with 2.17 and I still see 2, but in Bangle 2 Emulator I see 1 indeed.
EDIT: added trace EDIT2: maybe that's older E.toString() which still only made flat strings? Posted at 2023-08-15 by @gfwilliams
Ahh! Yes, that'll be it :) The newer one uses whatever takes the least space Posted at 2023-08-15 by user156224 Thank you for the detailed explanation; it all makes sense now! I've included my code below where I store data in the "compressedData = []" array. Is there a way to save this data with fewer storage blocks or space?
Posted at 2023-08-15 by @gfwilliams Well, having the compressedData array isn't going to be very efficient as it's just a normal array by the look of it. I'd consider just having a big ArrayBuffer and then use a DataView to set the 3 bytes directly. It even has a setUint16 so you can avoid that shifting you were doing. Posted at 2023-08-15 by @fanoush
so this is not about Uint8Array size since in your code it is only temporary variable. The remaining blocks going down by three is about the ordinary array having three integers added there by push.apply
Posted at 2023-08-15 by user156224 Thank you both...you really helped me a lot with your explanations. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2023-08-14 by user156224
I am working on a project where I generate data that initially occupies 17 bytes, and I compress it down to 3 bytes. These 3 bytes are then stored in a Uint8Array, as shown in the code snippet below:
I've noticed that this 3-byte array is taking up 3 blocks in memory, with each block being 14 bytes on my system. When I write this array to a binary file, the size of the file is just 3 bytes, as expected.
I would like to understand why this 3-byte array is consuming 3 blocks instead of being represented within a single block, and if there's a way to optimize this. Any insights would be greatly appreciated. Thank you!
Beta Was this translation helpful? Give feedback.
All reactions