<?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"><channel><title><![CDATA[Untitled RSS Feed]]></title><description><![CDATA[Untitled RSS Feed]]></description><link>https://kuskmen.github.io</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Nov 2017 22:21:57 GMT</lastBuildDate><atom:link href="https://kuskmen.github.io/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Improve your instantiation of objects.]]></title><description><![CDATA[<div class="paragraph">
<p>Hi people, I was busy at work doing some automations stuff where we began to face rather interesing issue.</p>
</div>
<div class="paragraph">
<p>Because we had so much test fixtures and each of them intantiating new  <em>HTTP</em> client(which can be heavy from time to time) we started to see that there are performance issues doing so. I&#8217;ve reserached a bit and noticed that this is common problem when you are creating new heavy objects frequently.Of course, there was already a solution to this - <strong><em>Object Pool pattern</em></strong>.</p>
</div>
<div class="paragraph">
<p>Main benefits when using classic object pool pattern:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Re-uses objects that were already created</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>and this is huge, because lets say you work with objects that takes a while to build, which doesn&#8217;t actually captures state into it, why bother creating new instances everytime we need it instead of "caching" the instance somewhere?</p>
</div>
<div class="paragraph">
<p>Some drawbacks:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Object pool pattern is not intended for storage, so only some optimal number of instances is good (for instance number of your CPU cores is a good start) to have otherwise we go into the place where we use memory but never release, which is bad.</p>
</li>
<li>
<p>Its implied that when you use instance of an object from the pool you will return it short, its ok if you use it longer than usuall but this only reduce the usefullness of the pool.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This is one example object pool implementation in C#:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>public class ObjectPool&lt;T&gt; : IObjectPool&lt;T&gt; where T : class
    {
        private readonly Func&lt;T&gt; _factory;
        private T _firstItem;
        private readonly T[] _items;

        public ObjectPool(Func&lt;T&gt; factory) : this(factory, Environment.ProcessorCount * 2) { }
        public ObjectPool(Func&lt;T&gt; factory, int size)
        {
            Debug.Assert(size &gt;= 1);
            _factory = factory;
            _items = new T[size - 1];
        }

        public virtual T Allocate()
        {
            // Try to get first object first.
            var inst = _firstItem;
            if (inst == null || inst != Interlocked.CompareExchange(ref _firstItem, null, inst))
            {
                // If first is null simply search for the next non-null object in the items.
                var items = _items;
                for (var i = 0; i &lt; items.Length; i++)
                {
                    var inst2 = items[i];
                    // return first object that we find it is not null
                    if (inst2 != null &amp;&amp; inst2 == Interlocked.CompareExchange(ref items[i], null, inst2))
                    {
                        return inst2;
                    }
                }
                // If we fail to find initialized object, create one ourselves.
                return _factory();
            }
            // return instance of the first object (this is not null for sure).
            return inst;
        }
        public virtual void Free(T obj)
        {
            Debug.Assert(obj != null);
            Debug.Assert(obj != _firstItem);

            // Try to return object as first first.
            if (_firstItem == null)
            {
                _firstItem = obj;
            }
            // If we fail we return it to the next best place.
            else
            {
                var items = _items;
                for (var i = 0; i &lt; items.Length; i++)
                {
                    if (items[i] == null)
                    {
                        items[i] = obj;
                        break;
                    }
                }
            }
        }
    }

    public interface IObjectPool&lt;T&gt;
    {
        /// &lt;summary&gt;
        ///  Produces an instance of &lt;see cref="T"/&gt;.
        /// &lt;/summary&gt;
        /// &lt;returns&gt; Newly created object of &lt;see cref="T"/&gt;. &lt;/returns&gt;
        T Allocate();

        /// &lt;summary&gt;
        ///  Returns object to the pool.
        /// &lt;/summary&gt;
        /// &lt;param name="obj"&gt; Object to return. &lt;/param&gt;
        void Free(T obj);
    }</code></pre>
</div>
</div>
<div class="paragraph">
<p>Bottom line, Object Pool pattern is really powerful, but on the right place, you should definitely first identify the problem and then decide if to implement it.</p>
</div>]]></description><link>https://kuskmen.github.io/2017/11/29/Improve-your-instantiation-of-objects.html</link><guid isPermaLink="true">https://kuskmen.github.io/2017/11/29/Improve-your-instantiation-of-objects.html</guid><dc:creator><![CDATA[Ianislav Vasilev]]></dc:creator><pubDate>Wed, 29 Nov 2017 00:00:00 GMT</pubDate></item><item><title><![CDATA[Hi, my name is Yanislav!]]></title><description><![CDATA[<div class="paragraph">
<p>Hi, this will be my very first blog, so this will be some sort of explanation for its purpose and which audience it targets.</p>
</div>
<div class="paragraph">
<p>I am currently <em>junior software developer</em> at <a href="https://www.sbtech.com/">SBTech</a> a company for sports betting, I am 24 years old and I live in Sofia, but enough of me.</p>
</div>
<div class="paragraph">
<p>This blog will be for software development(mostly, I can&#8217;t predict the future) and it will have the purpose of explaining principles and interesting topics I&#8217;ve read in internet or heard from podcasts on my journey to work every day.</p>
</div>
<div class="paragraph">
<p>While blogging, except the usuall tags that I will use I will use 5 different more in order for you to understand if this topic is suitable for you or not. They are borrowed from the <a href="https://en.wikipedia.org/wiki/Dreyfus_model_of_skill_acquisition">Dreyfus model of skill acquisition</a> - read it , really good stuff - helped me a lot.</p>
</div>
<div class="paragraph">
<p>Most of the examples will be from the <code>.NET framework</code> because this is what technology I am using when I am at work , and this is where I feel most comfortable, but I will try to be language agnostic.</p>
</div>
<div class="paragraph">
<p>As you may have guessed by now I am not linguist so please don&#8217;t mind the grammar ;)</p>
</div>]]></description><link>https://kuskmen.github.io/2017/08/06/Hi-my-name-is-Yanislav.html</link><guid isPermaLink="true">https://kuskmen.github.io/2017/08/06/Hi-my-name-is-Yanislav.html</guid><dc:creator><![CDATA[Ianislav Vasilev]]></dc:creator><pubDate>Sun, 06 Aug 2017 00:00:00 GMT</pubDate></item></channel></rss>