Jump to content

UniStream structs optimization


Recommended Posts

I recently came a cross with a UniLogic application that use large structs. For example, one struct was composed of 80 members, and most of the members type was INT16, like so:


In memory the struct look like this:   

struct MyStruct            //struct size is 80 * 4 = 320 bytes   
                           INT32         ID;                        //size 4              
INT16         Val_1;                 //size 2              
CHAR[2]    padding1;       //size 2            
INT16         Val_2;                 //size 2              
CHAR[2]    padding2;       //size 2            
INT16         Val_79;              //size 2              
CHAR[2]    padding79;   //size 2   

notice the padding that added to the struct. That's  common in software in order to align the memory so work will perform more effectively. This padding cause every 2 bytes in an INT16  type an extra "weight" of 2 bytes – so every INT16 is 4 bytes. One can think that the struct size is 4 + 2 * 79 = 162 bytes, when in fact it 320 bytes, almost two times bigger (!).


Now, let's try the next approach:


In memory:   

          struct MyStruct_NEW            //struct size is 4 + 2  * 79 = 162 bytes   
                            INT32            ID;                    //size 4              
INT16[79]     Val_Arr;      //size 158     

And we got a struct size 162 bytes, as intended. Just imagine we use struct "MyStruct" in a 3,000 line Data Table, we will waste almost 0.5M bytes (3,000  * 158), while "MyStruct_NEW"  waste nothing.


For conclusion,
when creating structs in UniLogic consider the padding factor in order to reduce your PLC memory consumption.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...