<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rui Eugénio on Medium]]></title>
        <description><![CDATA[Stories by Rui Eugénio on Medium]]></description>
        <link>https://medium.com/@rui.eugenio?source=rss-b013484a279e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*j8h7xWxblbTZSa2jbXiKdA.jpeg</url>
            <title>Stories by Rui Eugénio on Medium</title>
            <link>https://medium.com/@rui.eugenio?source=rss-b013484a279e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 05:33:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rui.eugenio/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[OutSystems Compiler Data Optimizations]]></title>
            <link>https://medium.com/outsystems-engineering/outsystems-compiler-data-optimizations-9bb4b3cb8567?source=rss-b013484a279e------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bb4b3cb8567</guid>
            <category><![CDATA[outsystems]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[Rui Eugénio]]></dc:creator>
            <pubDate>Fri, 22 Apr 2016 16:47:45 GMT</pubDate>
            <atom:updated>2017-12-27T15:13:19.555Z</atom:updated>
            <content:encoded><![CDATA[<p>The <a href="https://www.outsystems.com/?utm_source=medium&amp;utm_medium=top_of_article&amp;utm_content=OutSystems%20Compiler%20Data%20Optimizations&amp;utm_campaign=blog">OutSystems</a> platform uses a visual programming language to represent our applications. It is this visual language that enables our customers to easily create and maintain amazing applications.</p><p>However, that language can’t be used directly by the machine, and so it needs to be translated. This translation is a phase that happens when you publish your applications. We call this translation phase the <strong>compilation</strong>, and like many other programming languages that are <strong>compiled</strong>, we use this phase to perform some optimizations.</p><p>In this blog post I will talk about one class of optimizations that we perform during compilation, which we’ll call <em>compiler data optimizations</em>.</p><p>First, I will present to you the problems that we are addressing with the optimizations and next I will explain how we achieved that.</p><h3>The need for Compiler Data Optimizations</h3><h4>Typical server-client interaction</h4><p><a href="https://www.outsystems.com/?utm_source=medium&amp;utm_medium=middle_of_article&amp;utm_content=OutSystems%20Compiler%20Data%20Optimizations&amp;utm_campaign=blog">OutSystems</a> applications are server-rendered web applications.</p><p>This means that the typical flow of our applications is like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/644/1*etFgcBpJQLZghqoE7vbHtQ.jpeg" /></figure><p>NOTE: For AJAX interactions, the flow is more or less the same. In AJAX interactions we skip the preparation after the screen action and only render part of the screen.</p><p>If you analyse the picture you’ll see one arrow that represents data that is sent from the server to the client and one arrow that represents data that is sent from the client back to the server. Those data exchanges happen a lot in our modern interactive applications.</p><p>Although we know that Internet bandwidth is increasing every day, there is still the need to keep the size of data that travels back and forth at a minimum so the interactivity and the overall responsiveness of the application stays great.</p><h4>Data exchange between server and client</h4><p>Consider the following screen of a simple application built with the <a href="https://www.outsystems.com/platform?utm_source=medium&amp;utm_medium=middle_of_article&amp;utm_content=OutSystems%20Compiler%20Data%20Optimizations&amp;utm_campaign=blog">OutSystems Platform</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rBVxFJ4exl_0AS3W." /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/0*acZYqvuCEbLTbX3J." /></figure><p>And the following screen action associated with the click of the <strong>Apply </strong>button:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/0*mtPJbq-5ByJIH8Wq." /></figure><p>Some things of notice:</p><ol><li>We read two attributes of the <strong>Contact </strong>entity list in the <strong>UpdateContactPhone </strong>screen action. This data must be kept somewhere so it’s available when the screen action executes. Since the HTTP protocol is stateless this data is sent to the client along with the screen, so when we execute the server action it is available. This data is what we call <strong>viewstate </strong>(for historical reasons, since the first ASP.NET implementations also called it that).</li><li>The <strong>UpdateContactPhone </strong>screen action may be called for any row of the list, so the entire list needs to be stored in the <strong>viewstate</strong>.</li><li>We only need two attributes of the Contact entity in the screen action. This is especially important because the <strong>Contact </strong>entity contains the binary that represents the photo of the <strong>Contact</strong>.</li><li>We never need the <strong>Photo </strong>attribute in the screen. So we don’t even need to get it from the database.</li><li>After <strong>UpdateContactPhone </strong>screen action ends, the preparation is called again and the screen is re-rendered. During this we read the <strong>Contact </strong>entity list again. However we don’t need the to keep that list because is going to be re-created in the preparation when we run the aggregate to fetch the <strong>Contact </strong>entity list again.</li></ol><p>Without any compilation optimizations, we <em>must </em>assume that anything can be read anywhere and all the screen data must go into the <strong>viewstate</strong>. However in practice this is a waste of resources because, as we said above, we usually only need a part of it.</p><p>In this example, only the attributes <strong>Id </strong>and <strong>Phone </strong>of the <strong>Contact </strong>entity list are needed in the <strong>viewstate</strong>. There’s no need to allocate resources to save all those photos to the <strong>viewstate </strong>and bounce them back and forwards between the client and the server.</p><h3>How the Compiler Data Optimizations work</h3><h4>General Strategy</h4><p>The <strong>Compiler Data Optimizations </strong>work by performing what is known in compiler theory as <strong>live variable analysis</strong>. From <a href="https://en.wikipedia.org/wiki/Live_variable_analysis">wikipedia</a>:</p><blockquote>In compiler theory, live variable analysis (or simply liveness analysis) is a classic data-flow analysis performed by compilers to calculate for each program point the variables that may be potentially read before their next write, that is, the variables that are live at the exit from each program point. Stated simply: a variable is live if it holds a value that may be needed in the future.</blockquote><p>We need to analyse variable liveness in two specific points: At the end of the screen rendering, so we know exactly what needs to be stored in the <strong>viewstate</strong>; and on every aggregate execution so we know exactly what is needed from the database.</p><p>We made some small changes to the <strong>live variable analysis </strong>algorithm, but the basic concepts still remain.</p><p>This algorithm uses a graph to represent the data-flow of what we need to analyse and then perform a “<em>backwards way</em>” analysis (The analysis is done in a backwards order, i.e., in the opposite of the normal execution order).</p><h4>Graph representation</h4><p>Like we said in the previous section, we start by constructing a graph that represents the data-flow during the execution of the screen. This graph is constructed as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*iABE9_9N2KPNXCaz6op1ow.jpeg" /></figure><p>Note that each circle represents a sub graph:</p><ul><li>The preparation sub graph represents the preparation action. Since in the <a href="https://www.outsystems.com/?utm_source=medium&amp;utm_medium=middle_of_article&amp;utm_content=OutSystems%20Compiler%20Data%20Optimizations&amp;utm_campaign=blog">OutSystems</a> language the actions are already represented as graphs, this sub graph is usually the same as the preparation action graph itself. The end nodes of the preparation are connected to the first node of the screen rendering sub graph. The destination nodes are not connected to anything.</li><li>The screen rendering sub graph is in fact a sequence of nodes representing the widgets of the screen. The last node of this sequence is connected to each of the screen action sub graph.</li><li>The screen action sub graphs represent the flow of each screen action. Like the preparation sub graph, the screen action sub graphs are usually the same as the screen action graphs themselves. The end nodes of each screen action are connected to the preparation sub graph in the loop-back.</li></ul><h4>Identifier Concept</h4><p>Instead of working directly with variables like any common live variable analysis algorithm, we created a concept called Identifier.</p><p>Those Identifiers represent unique pieces of basic types of information, be it attributes of a compound object or list or simply variables of basic types. In the case of composite objects or lists, the identifier is a sequence, starting with the variable reference and containing all the attributes names in the path.</p><p>This will help us optimize the data, down to the minimum indivisible data type. As our applications have typically large objects containing multiple fields (and like we said in the previous section, sometimes with large binary chunks of data), this has significant impact in the performance of the algorithm.</p><p>Examples:</p><ul><li>Variable called <strong>Enabled</strong>, datatype Boolean, has Identifier <strong>[Enabled]</strong></li><li>Variable called <strong>Count</strong>, datatype Integer, has Identifier <strong>[Count]</strong></li><li>Variable called <strong>AddressInfo</strong>, whose datatype is a composite object (Address) containing attributes <strong>City </strong>(datatype: Text) and <strong>Country </strong>(datatype: date), has two Identifiers: <strong>[AddressInfo, City]</strong> and <strong>[AddressInfo, Country]</strong>. Since the <strong>AddressInfo </strong>is a composite object we flat-out it’s 2 attributes into 2 identifiers. Those identifiers are both sequences.</li><li>Variable called <strong>UserInfo</strong>, whose datatype is a composite object (User) containing attributes <strong>Name </strong>(datatype: Text) and <strong>Address </strong>(datatype: complex object Address), has three Identifiers: <strong>[UserInfo, Name]</strong>, <strong>[UserInfo, Address, City]</strong> and <strong>[UserInfo, Address, Country]</strong>. Note that we flat out any composite object attributes in the path.</li></ul><h4>Graph Node Information</h4><p>Each node contains the following information:</p><ul><li>In[N]: a set of Identifiers alive at the point immediately before the node N. This is also sometimes called LiveIN[N] or Input[N] in some live variable analysis implementations.</li><li>Out[N]: a set of Identifiers alive at the point immediately after the node N. This is also sometimes called LiveOUT[N] or Output[N] in some implementations.</li><li>Assignment[N]: this can be either empty or a pair of (identifier, expression) if the node N contains an assignment. The first element of this pair is the identifier of the left-side of the assignment. The second element of this pair is the expression of the right-side of the assignment. Expressions in <a href="https://www.outsystems.com/?utm_source=medium&amp;utm_medium=middle_of_article&amp;utm_content=OutSystems%20Compiler%20Data%20Optimizations&amp;utm_campaign=blog">OutSystems</a> are like in any other language, basically any legal combination of symbols that represents a value. This isn’t used in a common live variable analysis implementation.</li><li>Def[N]: a set of Identifiers that are assigned a value in N (in some implementations, Def[N] is also defined as a set of Identifiers assigned a value in N before any use, but this doesn’t change the solution of the dataflow equation). This is also sometimes called Kill[N] in some implementations.</li><li>Use[N]: the set of Identifiers that are used in N before any assignment. Sometimes called Gen[N] in some implementations. What is different from common live variable analysis implementations is that the right side Identifiers on any assignment are usually not considered uses. The exceptions are Identifiers used as arguments to functions and the right side Identifiers in a assign to a global variable (like a session variable).</li></ul><h4>Algorithm</h4><p>The algorithm proceeds as follows:</p><pre>While changes to any of the in’s occur do<br> For each node N do Begin<br>  Out[N] = Disjunction of all In[s], where s is a successor of N<br>  In[N] = Use[N] + (Out[N] — Def[N])</pre><pre>  If exist assignment[N]<br>   For each outputID in Out[N]<br>    If left-side[assignment[N]] is a prefix of outputID<br>     If right-side[assignment[N]] is a Identifier Begin<br>      suffix = suffix of outputID not in left-side[assignment[N]]<br>      Add right-side[assignment[N]].suffix to In[N]<br>     End Else<br>      Add all Identifiers in rigth-side[assignment[N]] to In[N]<br>End</pre><h4>Algorithm Notes</h4><p>The algorithm basically runs for all nodes while there are changes in In[N] for any node N.</p><p>Note that the algorithm treats assignments in a special way: if we assign from one identifier to another, we only transport the liveness of the identifier on the right side to the identifier on the left side. So a flow containing only assignments doesn’t affect the liveness of variables at all and may be seen as a NOP.</p><p>What this means in practice is that we can assign one composite object to another one and this won’t mark the whole object as being used. Like we said before, since the objects in our applications tend to have lots of attributes, this also has a big impact in the output of the algorithm.</p><h3>Conclusions</h3><p>The usage of this algorithm assures that we keep <strong>viewstate </strong>usage as minimum. For the example screen, if we assume that each contact photo has an average of 40KB (if we use thumbnails instead of original resolution images) and the contact list contains 50 entries at a time (the default Line Count for <a href="https://www.outsystems.com/?utm_source=medium&amp;utm_medium=middle_of_article&amp;utm_content=OutSystems%20Compiler%20Data%20Optimizations&amp;utm_campaign=blog">OutSystems</a> list widgets), we save 2MB on the <strong>viewstate </strong>and we also manage those 2MB in the Application Server&lt;-&gt;Database communication, which help us make our applications more scalable while also giving a better experience to the end users of those applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bb4b3cb8567" width="1" height="1" alt=""><hr><p><a href="https://medium.com/outsystems-engineering/outsystems-compiler-data-optimizations-9bb4b3cb8567">OutSystems Compiler Data Optimizations</a> was originally published in <a href="https://medium.com/outsystems-engineering">OutSystems Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>