<?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 Shivam Jaiswal on Medium]]></title>
        <description><![CDATA[Stories by Shivam Jaiswal on Medium]]></description>
        <link>https://medium.com/@jshivam?source=rss-4b986373f666------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*YrtQrcIcGBpaX4cNTHWaDQ.jpeg</url>
            <title>Stories by Shivam Jaiswal on Medium</title>
            <link>https://medium.com/@jshivam?source=rss-4b986373f666------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 11:46:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jshivam/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[Hidden cost of String allocation in Swift]]></title>
            <link>https://medium.com/macoclock/hidden-cost-of-string-allocation-in-swift-2aee2686d34a?source=rss-4b986373f666------2</link>
            <guid isPermaLink="false">https://medium.com/p/2aee2686d34a</guid>
            <category><![CDATA[string]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[apple]]></category>
            <dc:creator><![CDATA[Shivam Jaiswal]]></dc:creator>
            <pubDate>Sun, 03 May 2020 21:12:35 GMT</pubDate>
            <atom:updated>2020-05-05T07:49:44.926Z</atom:updated>
            <content:encoded><![CDATA[<h3>The hidden cost of String allocation in Swift</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tCilVRj2ugyLrCl7Xfifdg.png" /></figure><p>Swift automatically allocates and deallocates memory on your behalf. Some of that memory it allocates on the stack and some on the heap.</p><p>As we all know, any data type which is of <strong><em>Value Type</em> </strong>gets stored on <strong><em>stack</em></strong> memory and <strong><em>Reference Type</em> </strong>gets stored on <strong><em>managed heap memory</em>.</strong></p><p><strong>Right?</strong> The answer is <strong>NO</strong>! Not always. Actually it all depends upon internal implementation on the data type.</p><p>And it may come as surprise to you that despite being a <em>value type</em>, <strong><em>String</em></strong> actually stores the content of its character indirectly on the <strong><em>heap</em></strong>. And storing data on heap is much heavier operation as compared to stack.</p><p>Hence, you should use <em>String</em> wisely instead of randomly allocating it just for the sake of your convenience.</p><p>Let’s understand it by an example from a messaging application I’ve been working on. I am trying to make a bubble image, which encapsulates the chat message text.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6WV4sG9coo7VnOEgN8ChNw.png" /></figure><p>My <strong><em>makeBalloon</em></strong> function is what generates this image and it supports a configuration of different or the whole configuration space of different balloons.</p><p>Now, the <em>makeBalloon</em><strong><em> </em></strong>function needs to be really fast because I call it frequently during allocation launch and during user scrolling. And so I have added this <strong><em>caching</em></strong><em> </em><strong><em>layer</em></strong>. So, for any given configuration, I never have to generate this balloon image more than once. If I have done it once, I can just get it out the cache. The way I have done this is by <strong><em>serialising my color, orientation </em></strong><em>and</em><strong><em> tail</em></strong> into a key, which is a <strong><em>String</em></strong>.</p><p>Now there is a couple of things not to like here. String isn’t particularly a strong type for this key. I am using it to represent this configuration space, but I could just as easily put the name of my dog in that key. So, not a lot of safety there. Also, <strong><em>String</em></strong> can represent so many things because it actually stores the content of its character indirectly on the <strong><em>heap</em></strong>. So that means every time we are calling into this <em>makeBalloon</em><strong> </strong>function, <strong><em>even if we have a cache hit, we are incurring a heap allocation</em></strong><em>.</em></p><p>Hmm, let&#39;s see if can do better by reducing the cost of allocation.</p><p>Well in Swift we can represent this configuration space of <em>color, orientation </em>and<em> tail</em><strong><em> </em></strong><em>just by using a</em><strong><em> Struct. </em></strong>This is a much safer way to represent this configuration than a <strong><em>String</em></strong>. And because Structs are first-class types in Swift, they can be used as the key in our dictionary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tdrhH4HdakxGgmlMpyoKRA.png" /></figure><p>Now when we call the function, if we have a cache hit, there is no allocation overhead because construing a struct doesn&#39;t require any heap allocation. It can be allocated on the stack.</p><h4>The Time Difference</h4><p>Let’s see the time taken by <strong><em>String</em></strong> and <strong><em>Struct</em></strong> when each of them is allocated <strong><em>a million</em></strong> times.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aCIVuRCgAbposzEtmI8gWQ.png" /></figure><p>And the time consumed by Struct is more than <strong><em>100 times faster</em></strong> than String allocation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qu2BKBXwWqInYS_aNYtFtw.png" /></figure><h4>Where to go from here?</h4><p>For more details visit WWDC’16’s Understanding Swift Performance.</p><p><a href="https://developer.apple.com/videos/play/wwdc2016/416/">Understanding Swift Performance - WWDC16 - Videos - Apple Developer</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2aee2686d34a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/macoclock/hidden-cost-of-string-allocation-in-swift-2aee2686d34a">Hidden cost of String allocation in Swift</a> was originally published in <a href="https://medium.com/macoclock">Mac O’Clock</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Strategy Design Pattern & Problem it solves using Swift]]></title>
            <link>https://medium.com/macoclock/strategy-design-pattern-problem-it-solves-using-swift-f3be01e02fbe?source=rss-4b986373f666------2</link>
            <guid isPermaLink="false">https://medium.com/p/f3be01e02fbe</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[design-pattern-ios]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[strategy-design-pattern]]></category>
            <dc:creator><![CDATA[Shivam Jaiswal]]></dc:creator>
            <pubDate>Sat, 02 May 2020 16:19:56 GMT</pubDate>
            <atom:updated>2020-05-05T13:38:41.078Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FX3In2CgYoPhJ7DsC-meQQ.png" /></figure><p>In Strategy pattern, a class behaviour or its algorithm can be changed at run time. This type of design pattern comes under behaviour pattern.</p><blockquote>In Strategy pattern, we create objects which represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object.</blockquote><p>Went over your head?? 🤯 Don’t worry let’s break it down with an example step by step.</p><p><strong>Step 1</strong>: Understanding the problem.</p><p>Let say, you own a <em>Knife Factory</em> which manufactures knives using two components <em>blade</em> &amp; <em>handle and </em>right now you manufacturing only one kind of knives using <em>blade1</em> &amp; <em>handle1.</em></p><p>This is how it’s class implementation would look like.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a716af732da6baf0f38ec8a091ce3d6f/href">https://medium.com/media/a716af732da6baf0f38ec8a091ce3d6f/href</a></iframe><p>Later on another you also start making two more kinds of knives using (<em>blade2, handle2) &amp; (blade3, handle3) </em>respectively.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a5547a059bd993baaa35819914571aa/href">https://medium.com/media/8a5547a059bd993baaa35819914571aa/href</a></iframe><p>So far so good!! Right??. But there are <strong><em>two</em></strong> problems in this design we followed.</p><p><strong>Problem#1</strong>:</p><p>Right now we manufacturing 3 kind of <em>blades</em> &amp; 3 kind of <em>handles</em> i.e. we can manufacture nCr : C(3,1) * C(3,1) = 9 kinds of knives using different combinations of <em>blades &amp; handles</em>. As result we need to make 9 different <em>KnifeFactory</em> classes to support the manufacturing.</p><p>To generalise it we can say that if we are producing <em>n</em> kind of <em>blades</em> and <em>m</em> kind of <em>handles</em> &amp; if we decide to produce all possible combinations of knives then the total number of classes would be <strong><em>C(m,1) * C(n,1) = O(m * n)</em></strong>.</p><p>Which we can obviously tell is pretty inefficient, time consuming and it’s hard to manage so may classes.</p><p><strong>Problem#2:</strong></p><p>Let say we want to manufacture <em>4th</em> knife using (<em>blade2, handle3)</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fc4a7faed06ce9af14b038867e4ba26/href">https://medium.com/media/3fc4a7faed06ce9af14b038867e4ba26/href</a></iframe><p>here we can see that it uses the <em>blade()</em> implementation of <em>KniveFactory2</em> and <em>handle()</em> implementation of <em>KniveFactory3</em> just by copy pasting the code from respective classes. Which leads to writing <strong>redundant</strong> code and by no means we are able to reuse existing implementation. Which is bad coding practice.</p><p><strong>Step 2</strong>: Enough with problems let’s come to the solution.</p><p>To explain how to do it, we will answer three questions and use a diagram, which I extracted from the book <a href="https://store.raywenderlich.com/products/design-patterns-by-tutorials"><strong>Design Patterns by Tutorials</strong></a><strong>,</strong> available in Ray Wenderlich’s store, where we can differentiate all the participants very well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XC-gfXRJVj7oZTF_crCvPg.png" /></figure><ul><li><strong>What: </strong><em>A protocol that defines the action that we want to encapsulate. In our example, the action would be to </em><strong><em>produce blade </em></strong><em>&amp;</em><strong><em> handle</em></strong><em>.</em></li><li><strong>Who: </strong><em>An object who contains an object who conforms the strategy. In our example, it could be an object who </em><strong><em>using the strategy to produce Knives</em></strong><em>.</em></li><li><strong>How: </strong><em>Specific implementations of the strategy. Each implementation is different. In our example, we would have </em><strong><em>six</em></strong><em> </em><strong><em>strategies,</em></strong><em> </em><strong><em>two for each style.</em></strong></li></ul><p>So, the previous example using strategy would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b8b0cf5e67a4e1333807a50b22c385d/href">https://medium.com/media/9b8b0cf5e67a4e1333807a50b22c385d/href</a></iframe><p>And here how we can use it:</p><p>Let say we want to produce a <em>knife</em> using <em>blade3</em> &amp; <em>handle1</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2892f367b9c022fd2938bf65f71979c8/href">https://medium.com/media/2892f367b9c022fd2938bf65f71979c8/href</a></iframe><h4>How is this efficient ?</h4><ol><li>Here, the cost of creating classes depends upon number of strategies we want to implement. In our case we can say that if we are producing <em>n</em> kind of <em>blades</em> and <em>m</em> kind of <em>handles</em> then the cost of creating strategies is <strong><em>O(m + n)</em></strong></li><li>You are not repeating any implementation of <strong><em>handle() </em></strong><em>&amp;</em><strong><em> blade(). </em></strong>Hence the code <strong><em>redundancy has been completely removed .</em></strong></li></ol><h4>Breaking down the definition</h4><p>In Strategy pattern, we create objects which represent various <em>strategies</em> <strong>[i.e. <em>BladeStrategy &amp; HandleStrategy]</em></strong> and a <em>context object </em><strong><em>[i.e KnifeFactory] </em></strong>whose behavior varies as per its strategy object. The strategy object changes the <em>executing algorithm</em><strong><em> [manufacture(); behaviour </em>varies as per it’s strategy object<em>] </em></strong>of the context object.</p><h4>References</h4><ol><li><a href="https://medium.com/flawless-app-stories/strategy-pattern-in-swift-1462dbddd9fe">https://medium.com/flawless-app-stories/strategy-pattern-in-swift-1462dbddd9fe</a></li><li><a href="https://www.youtube.com/watch?v=v9ejT8FO-7I">https://www.youtube.com/watch?v=v9ejT8FO-7I</a></li><li><a href="https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm">https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f3be01e02fbe" width="1" height="1" alt=""><hr><p><a href="https://medium.com/macoclock/strategy-design-pattern-problem-it-solves-using-swift-f3be01e02fbe">Strategy Design Pattern &amp; Problem it solves using Swift</a> was originally published in <a href="https://medium.com/macoclock">Mac O’Clock</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>