<?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 Yurii A on Medium]]></title>
        <description><![CDATA[Stories by Yurii A on Medium]]></description>
        <link>https://medium.com/@yuuurchyk?source=rss-8b1ca3274cfe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*smucrf9n3ie62dxlB0l39Q.jpeg</url>
            <title>Stories by Yurii A on Medium</title>
            <link>https://medium.com/@yuuurchyk?source=rss-8b1ca3274cfe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:22:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yuuurchyk/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[Complex Solution to a Dead Simple Concurrency Task]]></title>
            <link>https://levelup.gitconnected.com/complex-solution-to-a-dead-simple-concurrency-task-5a66ae21e3b?source=rss-8b1ca3274cfe------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a66ae21e3b</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[c-plus-plus-language]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Yurii A]]></dc:creator>
            <pubDate>Tue, 28 Sep 2021 12:33:21 GMT</pubDate>
            <atom:updated>2021-09-28T12:33:21.493Z</atom:updated>
            <content:encoded><![CDATA[<h4>Abstractions do leak when dealing with performance-critical applications. In this case, a developer cannot treat a computer as a black box anymore. Instead, one should have a solid understanding of what’s happening behind the scenes. This story illustrates how proper usage of concurrency, computer architecture and algorithms knowledge can tremendously speed up your program if used properly.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DLhcRc0e0nnms2Nd" /><figcaption>Photo by <a href="https://unsplash.com/@ryan_yoo?utm_source=medium&amp;utm_medium=referral">Ryan Yoo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>C++concurrency is a tricky thing. While giving you the incredible power of boosting your application, <strong>it does not restrain you from writing multi-threaded code that is much slower than its sequential version</strong>. To identify the potential bottlenecks, we have to know what should/shouldn’t be done in a multi-threaded setup and, most importantly, <strong>why</strong> it is the case. So let’s get started!</p><h3>Problem Statement</h3><p>Recently I received an innocent-looking test task. Please read it carefully, as we will exploit every piece of the statement in the final solution.</p><blockquote>“Write a program that counts the number of distinct words in a given file. The input text is guaranteed to contain only ‘a’…’ z’ letters and space characters in ASCII encoding”.</blockquote><p>What first seems like a breeze becomes an exciting adventure with only two extra conditions added:</p><ol><li>The program should be able to <strong>handle large inputs</strong> (i.e. 32GB).</li><li>The solution must <strong>utilize all</strong> available <strong>CPU resources.</strong></li></ol><h3>Baseline</h3><p>To have some baseline to compare to, let’s write the most straightforward sequential solution possible:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7afebc8f657ac7d1492b06f2924b335f/href">https://medium.com/media/7afebc8f657ac7d1492b06f2924b335f/href</a></iframe><p>That’s KISS in its ultimate form: read by words, put them in a set. This piece of code is neither concurrent nor optimal, but it’s a good place to start. We should not overcomplicate things until we genuinely know our needs.</p><h3>Concurrency Attempt</h3><p>Let’s add some concurrency into the mix. We should first decouple reading the words from their processing. Doing that should reduce the critical path and overall runtime of the application. I opted for implementing a single producer/multiple consumer pattern with the following threads:</p><ul><li>Reader — reads a file by chunks of words. Each chunk is represented as a vector of strings and is put on the producer’s queue.</li><li>Producer — forms a set of unique words out of each chunk and puts it on the shared consumers’ queue.</li><li>Consumer — while there are more than two elements in the consumers’ queue, merges the first two sets of unique words and puts the resulting one back.</li></ul><p><a href="https://github.com/yuuurchyk/UniqueWordsCounter/blob/main/src/method/producerConsumer.cpp">Implementation</a> is a bit cumbersome, but still no rocket science. Some general remarks:</p><ol><li>I use <strong>death pills</strong> as an inter-thread communication mechanism. These special queue elements indicate that nobody will push anything back on the container anymore. The pill can be present <strong>at most once, strictly at the end of the queue</strong>. That means that if the front element is the death pill, there are no more items to process.</li><li><strong>Critical sections should be as short as possible</strong>. We might check if the front element is the death pill while holding the mutex, but it doesn’t make much sense. Because we expect the elements to be ordinary for most cases, we should quickly steal the front item, release the mutex and process the resulting element without synchronization. And when it happens to be the death pill, we acquire the lock again and move it back.</li><li><strong>Use RAII wrappers for mutexes.</strong> When no fine-grained control is needed, std::scoped_lock is an excellent choice. It implies little to no overhead compared to lock/unlock semantics. Moreover, you no longer care about exception safety since the mutex is released automatically during stack unwinding.</li><li><strong>Use move semantics</strong>.</li><li>Make sure you <strong>know how containers work in-depth</strong>.</li></ol><h3>Results</h3><p>Here are the timing results. The baseline and producer/consumer approaches were tested on two sets of synthetically generated files: <strong>short</strong> and <strong>long</strong> words, with the words having a different mean length in each group. The table also provides the time spent by the reader thread in the producer/consumer algorithm (see <em>Reading Time</em> column):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dZoOhP2paYbtSUTXAbv-og.png" /><figcaption>Producer/consumer approach compared to sequential baseline. Please, don’t compare absolute timing result between compilers, since MSVC data was collected on a different machine.</figcaption></figure><p>Wait, what? Shouldn’t the producer/consumer solution be much faster than the baseline? Shouldn’t performance improve with the number of threads? Well, the numbers don’t lie. Introducing the concurrency made the program worse, especially for big files! Here are some observations before we move on:</p><ol><li>the methods are far from being bounded by hard drive reading speed</li><li>wall time seems to depend on the number of unique words instead of file size</li><li>16GB of RAM is not enough for 1000MB files when using the producer/consumer approach</li><li>we don’t benefit from multiple consumer threads</li></ol><h4>Small Note on Benchmarking</h4><p>Changing the order of benchmarks may sometimes significantly affect timing results. In my case, it was due to branch prediction. Because short and long words have different statistical properties, the branch deciding whether the next character is a space fails to predict when changing the words’ distribution. To overcome the issue, make sure to run enough iterations or split the benchmarks into two executables if the former is infeasible.</p><h3>Analysis</h3><h4>Memory Management</h4><p>So what’s wrong with the concurrent approach? There are no long critical sections, and every thread should have pretty much work to do. There are also no copies since move semantics is used extensively. The experienced programmers might already know the answer, but let’s step back and analyze what every thread is doing:</p><ul><li>Reader: reads strings, puts them in a vector. Vector stores its elements on the <strong>heap</strong>.</li><li>Producer: puts the strings from vector into an unordered set. Similarly to the vector, the hash table is also allocated on the <strong>heap</strong>.</li><li>Consumer: merges two unordered sets. Since we are again dealing with hash tables, we have lots of <strong>heap memory allocations.</strong></li></ul><p>But the threads are still running in parallel, right? Technically yes, but here is a catch: <strong>the heap is not parallelizable! </strong>We can’t have two simultaneous allocations unless we use system-dependent API or reserve pools of raw memory beforehand. Since no thread does pretty much anything else apart from implicit heap memory allocations under the hood of containers, the second solution is not parallel at all. Because we do more work with sets as opposed to a baseline example and inter-thread communication is not free of charge, all these factors add up to the resulting performance. It also explains why it gets worse with more threads (observation no. 4).</p><h4>Unordered Set Performance</h4><p>We know that std::unordered_set uses a hash table under the hood. But what exactly happens there? Well, <strong>every bucket </strong>of the table<strong> is a singly linked list! </strong>When inserting the new element, we first find the right bucket using the object’s hash value. Then we traverse the linked list of items inside the chosen bucket to examine whether the element has already been present. If not, we allocate a node, put the element there, and append the node to the list. The set grows when the <strong>average </strong>number of items per bucket is greater than 1. Sounds good? Here are some aspects to think about:</p><ul><li><strong>Memory allocations. </strong>Not only do we allocate the memory for the buckets, but also for the list nodes on every insertion. While the former allocations don’t happen too often, the latter are triggered much more frequently given enough unique words. This essentially kills the parallelism due to the reasons discussed above.</li><li><strong>Unused buckets. </strong>We might accidentally “hack” the hash table to use only a fraction of its buckets. For example, the unordered set of even numbers will have only <strong>half </strong>of its buckets occupied. It’s even possible for all the items to end up in a single bucket if we get really unlucky. The key takeaway is that the hash function should have good statistical properties and produce evenly distributed values.</li><li><strong>Memory access pattern, cache locality. </strong>The linked lists are incredibly horrible to traverse compared to contiguous containers. Since the CPU does not operate with individual bytes but rather 64-byte long cache lines, it is lightning fast to access subsequent elements because they are already in cache. Moreover, the hardware will detect the sequential memory access pattern and speculatively prefetch the following cache lines to reduce the latency. Unfortunately, the linked list cannot benefit from all that jazz. Because nodes are randomly scattered across the heap, you will get a <strong>cache miss on every element</strong> and resort to dealing with RAM, which is <strong>a hundred times!!! slower</strong> than the L1 cache. To verify this indeed may be an issue, I profiled the baseline implementation with <em>cachegrind. </em>There was a considerable amount of L1 data misses coming from traversing the contents of the buckets.</li></ul><p>Now we understand why file reading is not yet a bottleneck and why the resulting time depends on the number of words in the file (observations no. 1 &amp; 2).</p><h4>Other Potential Problems</h4><ul><li><strong>Can we read faster? </strong>Iostreams are known to be slow, so we may try to do better. Moreover, we haven’t actually decoupled file reading from words processing since iostreams read by words.</li><li><strong>Better queues? </strong>Why should we block two threads if they want to access the elements from different sides? We should try using lock-free queues. Despite atomics being more expensive than mutexes, they might deliver better per-thread performance. But of course, this should be profiled.</li><li><strong>Do we even need </strong><strong>std::string?</strong></li></ul><h3>Optimizing the Baseline</h3><p>Let’s backtrack a bit. We have decided on the things to aim for: <em>cache locality</em>, <em>fewer heap allocations</em>, <em>custom scanning</em>, <em>lock-free queues</em>. Since none of these is trivial, let’s gradually improve the sequential solution and introduce concurrency afterwards.</p><h4>Custom Scanning</h4><p>Since files operations are lengthy, we should read big raw buffers of data and break them into words by hand. The only corner case is when the word is on the boundary of two buffers. Let’s stick with a standard hash set implementation for now and see whether the manual reading alone can make a difference.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V3Qs86xVkaOG5FJL8paGKA.png" /><figcaption>Custom scanning approach compared to sequential baseline.</figcaption></figure><p>We get 6–12%<strong> </strong>improvement from <strong>manual reading alone</strong>. Given the fact that iostreams do something similar under the hood, we should not expect more. We can also try doing memory mapped io to avoid copying the characters from kernel to user space, but it’s out of the scope of this story.</p><p>The <a href="https://github.com/yuuurchyk/UniqueWordsCounter/blob/main/include/UniqueWordsCounter/utils/scanningImpl.h">implementation</a> is not that hard: read the file into the buffer, handle corner cases (first and last words + a rare possibility for the word to span across the whole buffer). The only interesting part is the hot loop where we deal with regular words:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1853545743e8b2c8174f8f756e9b8b79/href">https://medium.com/media/1853545743e8b2c8174f8f756e9b8b79/href</a></iframe><p>Here is why custom scanning may be faster:</p><ol><li>Few conditions inside the loop. There are no end<em> </em>conditions (I append trailing symbols to the end of the buffer). std::strpbrk is used to look for the space characters (which uses intrinsics under the hood). There is rarely more than one space between the words, so the corresponding loop is marked as unlikely<em>.</em></li><li>String construction is forwarded to std::unordered_set<em>. </em>In the baseline implementation, we had one or two constructions (reading from the stream + move constructor into the set). Here we always have only one string construction inside the set.</li></ol><h4>Better Set</h4><p>We have suspected std::unordered_set<em> </em>to be a bottleneck because of bad cache locality and lots of heap allocations. To mitigate them, we can try using <strong>open address hashing with linear probing </strong>(you can find my implementation <a href="https://github.com/yuuurchyk/UniqueWordsCounter/blob/main/include/UniqueWordsCounter/utils/wordsSet.h">here</a>). It exploits data locality and allocates memory only upon rehashing. The general idea is super-simple:</p><ul><li>Each bucket can contain only one element. That means we only work with an array of buckets. No nodes, pointers or extra allocations.</li><li>When adding the new element, we first choose the bucket to put it into. If it is already occupied, we move to the next one. We continue until we find an empty bucket and put the new element there. Since we examine the neighbouring buckets, it should be fast since they are already in cache.</li></ul><p>Let’s compare it against std::unordered_set when inserting the words from the buffer:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BJUXS5eY6DdDPnlESlgsXA.png" /><figcaption>Open addressing set is always at least twice as fast compared to std::unordered_set when inserting the words from the buffer.</figcaption></figure><p>Much better. We see the open addressing set being <strong>at least twice as fast</strong> compared to std::unordered_set across all compilers in various conditions. Here are the tricks that helped me achieve such results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KPxYKDAwP0V5emsoRFstGA.png" /><figcaption>Better Set bucket structure.</figcaption></figure><ul><li><strong>Bucket structure. </strong>Because most of the English words are not too long, I designed a bucket to be optimized for this particular scenario. Twenty-two characters are more than enough for almost every word (long words are handled separately). Moreover, the bucket perfectly fits into half of the cache line, having 32 bytes in size. Thereby we have constructed a custom version of the string that occupies 32 bytes <strong>together with the hash</strong> (as opposed to std::string<em>, </em>which often occupies the same amount of memory without the hash).</li><li><strong>Hash function sensitivity. </strong>Wikipedia says that open addressing hashing with linear probing works best with <strong>murmur hash. </strong>I can confirm it from my tests. Changing the hash function seems to degrade the performance.</li><li><strong>Don’t wait until it gets full. </strong>When the open addressing set is almost full, insertion/lookup operations turn into a linear search, which should be avoided as much as possible. The container should remain sparse. Hence, the solution is to rehash when more than 90% of the buckets are occupied.</li><li><strong>Don’t copy into the bucket until there is a real need. </strong>Because we emplace the words from the buffer, we can at first determine whether the word is already present by computing the hash from the buffer. Then we copy the word into the bucket <em>if and only if it hasn’t been present</em>. On the contrary, std::unordered_set constructs the std::string on every insertion.</li></ul><h4>Optimized Baseline</h4><p>Let’s combine the two previous approaches into one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1009/1*LWjvi8Nc6f57VDtDT7wTpA.png" /><figcaption>Optimized baseline (custom scanning + open addressing set) compared to other sequential solutions. Numbers speak for themselves.</figcaption></figure><h3>Adding Concurrency</h3><p>Since the first attempt on concurrency went pretty bad, I will compare the methods against a more practical approach: using multiple consumers with a single tbb::concurrent_unordered_set<em>. </em>It is a part of the Intel TBB library, which provides containers optimized for concurrent use. I haven’t investigated the implementation, but it seems to be using bucket-wise locking, which allows for simultaneous insertion.</p><p>The idea of the <a href="https://github.com/yuuurchyk/UniqueWordsCounter/blob/main/src/method/concurrentSetProducerConsumer.cpp"><strong>concurrentSetProducerConsumer</strong></a><strong> </strong>approach is pretty straightforward: there is a single producer thread, which reads the data as big raw buffers. Multiple consumer threads process the buffers using the custom scanning routine described above. The words are inserted into the shared instance of tbb::concurrent_unordered_set<em>.</em></p><h4>Introducing Concurrency to Optimized Baseline</h4><p>Because of the non-concurrent nature of the open addressing set, it would be hard to take advantage of multiple threads. But there are still some corners we can cut. The following tasks can be executed simultaneously:</p><ol><li>Reading from the file into the buffer</li><li>Scanning the buffer for words</li><li>Rehashing the resulting set</li></ol><p>Consequently, the <a href="https://github.com/yuuurchyk/UniqueWordsCounter/blob/main/src/method/optimizedProducerConsumer.cpp"><strong>optimizedProducerConsumer</strong></a><strong> </strong>approach uses three threads:</p><ol><li>Scanner: reads the file into big raw buffers</li><li>Producer: reads the buffer using the custom scanning routine. Unique words are placed into small open addressing set with a fixed capacity of 1024 buckets. Once the set is filled up, it is scheduled for consumption and replaced with a new one.</li><li>Consumer: merges all sets retrieved by the producer into one (by doing this, we don’t block the producer thread when rehashing occurs)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*wI41MWVCHgNUzXqKZyeFqA.png" /><figcaption>Overview of concurrent methods compared to the sequential “optimizedBaseline” approach.</figcaption></figure><p>Observations:</p><ol><li><strong>concurrentSetProducerConsumer </strong>scales with the number of threads but lacks cache locality</li><li>operations with open addressing set is still a bottleneck and should be optimized further</li></ol><h3>Further Steps</h3><p>We have saved another 20%, but we can’t take advantage of more than three threads. If only we could combine the nature of the concurrent unordered set with a cache locality of the open addressing set. It turns out that it might be possible. When deciding on the bucket index for the open addressing set, we use the least significant hash bits. We can spawn multiple instances of the open addressing set and have a one to one correspondence between the word and a set it goes into based on the <em>most significant bits</em> of the hash. Hence, we can have a distributed open addressing set and launch multiple consumer threads that don’t depend on each other. I have already experimented with it a bit. Murmur hash seems to have good statistical properties (i.e. words are evenly distributed among the sets based on the most significant bits). However, this approach requires using per-thread memory pools to handle concurrent allocations, which is slightly out of my league for now. I hope to share the results in the future.</p><h3>Closing Remarks</h3><p>Even the most straightforward task can be turned into an exciting cognitive adventure when put into the high-performance perspective. I don’t regret accepting the challenge since it helped me learn new things, refresh my knowledge of algorithms/computer architecture and work on the particular case where it is critical for performance.</p><p>The code for the methods, benchmarks and tests is available on <a href="https://github.com/yuuurchyk/UniqueWordsCounter">Github</a>. Feel free to investigate it or leave improvement suggestions.</p><p>Here are the talks that have inspired me along the way:</p><ul><li>Chandler Carruth: <a href="https://www.youtube.com/watch?v=fHNmRkzxHWs&amp;list=PL4qLUxc03WjxRzzmGeAALvqqB3sAcjLCc&amp;index=11">“Efficiency with Algorithms, Performance with Data Structures”</a>, CppCon 2014</li><li>Scott Meyers: <a href="https://www.youtube.com/watch?v=WDIkqP4JbkE&amp;list=PL4qLUxc03WjxRzzmGeAALvqqB3sAcjLCc&amp;index=12">“Cpu Caches and Why You Care”</a>, code::dive 2014</li><li>Andrei Alexandrescu: “Writing Fast Code”, code::dive 2015 (<a href="https://www.youtube.com/watch?v=vrfYLlR8X8k&amp;list=PL4qLUxc03WjxRzzmGeAALvqqB3sAcjLCc&amp;index=8">Part 1</a>, <a href="https://www.youtube.com/watch?v=9tvbz8CSI8M">Part 2</a>)</li><li>Timur Doumler: <a href="https://www.youtube.com/watch?v=BP6NxVxDQIs&amp;list=PL4qLUxc03WjxRzzmGeAALvqqB3sAcjLCc&amp;index=7&amp;t=2987s">“Want fast C++? Know your hardware!”</a>, CppCon 2016</li><li>Chandler Carruth: <a href="https://www.youtube.com/watch?v=2EWejmkKlxs&amp;list=PL4qLUxc03WjxRzzmGeAALvqqB3sAcjLCc&amp;index=14&amp;t=3288s">“Going Nowhere Faster”</a>, CppCon 2017</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a66ae21e3b" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/complex-solution-to-a-dead-simple-concurrency-task-5a66ae21e3b">Complex Solution to a Dead Simple Concurrency Task</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Journey of Upgrading My HP 250 G4: the Manual and Beyond]]></title>
            <link>https://medium.com/@yuuurchyk/the-journey-of-upgrading-my-hp-250-g4-the-manual-and-beyond-e7e7f5a6938f?source=rss-8b1ca3274cfe------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7e7f5a6938f</guid>
            <category><![CDATA[electronics]]></category>
            <category><![CDATA[hp-laptop]]></category>
            <category><![CDATA[hardware]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[laptop-repair]]></category>
            <dc:creator><![CDATA[Yurii A]]></dc:creator>
            <pubDate>Sun, 30 Aug 2020 13:35:07 GMT</pubDate>
            <atom:updated>2020-08-30T13:35:07.840Z</atom:updated>
            <content:encoded><![CDATA[<h4>Being a software engineer does not imply any expertise in electronics repair/replacement, but I think that’s a valuable skill to develop. In addition to saving you some money, it’s an excellent way to refresh your device’s look, learn something new and simply spend some time during the quarantine :)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U6MevjXlKHHblMlcXpsdEQ.png" /><figcaption>Final results</figcaption></figure><p>Note: by writing this post, I want to show that even a person with <strong>no previous experience</strong> can disassemble/reassemble the whole laptop. Also, since I’m not a professional, please <strong>do it at your own risk</strong> and don’t blame me if anything wrong happens with your device. Don’t treat this post as a complete guide but rather as a side note on what can go wrong and what is not mentioned in other articles/manuals.</p><p>In this story, I’m going to share my reflections, experience, pitfalls and conclusions made after upgrading my old laptop. So let’s get started :)</p><h3>Specs and Upgrade Options</h3><p>After switching to a brand-new Dell XPS 15, I’ve decided to give the old HP 250 G4 to my parents. However, its condition was pretty horrible: cracked plastic, dirty keyboard and poor performance — these are the reasons why I have decided to renovate it. Here are the specs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*YBtNHz4CO6MsoDmcaOqLMA.jpeg" /><figcaption>Product image taken from an official HP <a href="https://support.hp.com/hr-en/document/c04605376#AbT0">website</a></figcaption></figure><ul><li>CPU: Intel Core i3 5005U / Intel HD Graphics 5500</li><li>RAM: 8GB DDR3</li><li>GPU: AMD Radeon R5 2GB</li><li>HDD: 1TB</li><li>ODD: yes</li><li>Screen: TN HD Matte</li></ul><p>Apparently, it’s not comparable with modern laptops, but with some tuning, it is a good option for office-like apps, youtube, skype, etc.</p><p>As you may have already guessed, the hardware upgrade consisted of 2 parts:</p><ul><li>replacing ODD with <strong>SSD</strong> (as hard-drive is a bottleneck for performance)</li><li>adding <strong>extra 8GB of RAM</strong></li></ul><p>SSD should help to reduce Windows startup time and response time in general while additional RAM should help with multitasking.</p><p>After that, I also replaced all external components, which involved:</p><ul><li>bottom cover</li><li>palm rest</li><li>keyboard</li><li>screen</li><li>screen cover</li></ul><h3>First Steps</h3><p>After prior research, I have found lots of youtube videos and an official <strong>service manual</strong> (for <a href="http://h10032.www1.hp.com/ctg/Manual/c04653884">G4</a> and <a href="http://h10032.www1.hp.com/ctg/Manual/c06189775">G5</a> models), which was pretty decent and helped me a lot later. It contained a detailed reference on how to remove/install every component in the laptop, so investigate it first in case you need some assistance.</p><p>However, the problems arose almost immediately :) I was unable to remove the bottom cover: <strong>1 of the bolts failed to unscrew</strong>. To be more precise, it was screwing nice but didn’t come up. After 2–3 days of ineffective attempts, I’ve managed to find an ugly solution on one of the forums. I heated the bolt with the soldering iron and squeezed the bottom cover out without undoing the screw. The plastic around the bolt got soft, which helped me to lift the lid.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xez3dnfb51CFGhPFnTsTHQ.png" /><figcaption>Bottom cover: problematic screw (left) as opposed to a normal one (right)</figcaption></figure><p>Pretty ugly, right? After further investigation, I concluded that it was the right decision because the <strong>plastic on the palm rest around the corresponding nut was broken</strong>. Hence, the nut was rotating with the bolt while undoing it. One of my friends had the same problem with the same laptop. He managed to solve the issue by unscrewing the bolt with an electric screwdriver on high speed.</p><p><strong>Tips for unclipping the bottom cover:</strong></p><ul><li>do it with some kind of card (i.e. old credit card)</li><li>don’t put the card too far inside (otherwise, you are going to break the clip)</li><li>get the ODD out and start unclipping from there</li><li>you might want to use two cards at the corners of the cover</li><li>you can use a flat screwdriver for undoing hard clips on top (where the battery is located)</li></ul><h3>First Look at the Internals, Hardware Upgrade</h3><p>After the bottom cover removed and half plastic clips broken :), we can access the internals.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a0YhWNXZPcFrYnuw3Z8zwg.jpeg" /><figcaption>First look at the internals (the photo was taken while reassembling)</figcaption></figure><p>We can easily detect HDD (yellow), ODD(green) and a free RAM slot(red). In order to connect a second hard drive through an optical drive interface, I’ve bought an HDD caddy. However, since the drive connected via ODD line cannot be bootable and does not have high data throughput, we should <strong>replace HDD with SSD and put the HDD into the caddy</strong> (see this <a href="https://h30434.www3.hp.com/t5/Notebook-Hardware-and-Upgrade-Questions/add-second-HDD-over-existing-SDD/td-p/5937688">topic</a>). OS should be ported to the new SSD drive. You can use some programs to clone the disk image or create a recovery flash drive and reinstall the OS from it. I did the second option. The manual on HDD removal is pretty clear and straightforward. So is HDD caddy instruction.</p><p><strong>Tips:</strong></p><ul><li>lift the motherboard connector before disconnecting hard drive cable</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YIo0uA7LhLkAOtP-bX2J7w.jpeg" /><figcaption>All these black connectors on the motherboard should be lifted before removal</figcaption></figure><ul><li>pay attention to the arrows on metal plates on the sides of the HDD</li><li>make sure you fix the HDD with four screws after you installed it into the caddy</li><li><strong>do not open the lid without bottom cover installed and screwed</strong> (you risk to break the plastic around the hinges — learnt it the hard way)</li></ul><p>RAM removal/installation is also easy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PD0lSgp9kyAWdz-wMQ6JQw.png" /><figcaption>SSD model(left) and the upgraded internals (right)</figcaption></figure><p><strong>Results:</strong> Windows startup time reduced drastically, programs response time also dropped. The laptop became usable. Crystaldiskmark verified that SSD read/write speed capabilities were fully exploited. Additional RAM was detected by the system.</p><h3>Disassemble Methodology, Keyboard and Palmrest Replacement</h3><p>As I’ve mentioned earlier, the plastic on the palm rest was cracked. Moreover, I broke it on one of the hinges too. So it definitely needed replacement.</p><p>Apparently, all the electronics should be disassembled and transferred to a new palm rest. That brings up a question of <strong>how to store/sort the components</strong> in order not to get lost and be able to reassemble it. There is an urgency in some kind of methodology.</p><p>I’m going to share my approach, which consists of 5 simple steps:</p><ol><li><strong>Write down the name</strong> of the component.</li><li><strong>Take a picture</strong> of the component.</li><li><strong>Disassemble</strong> the component (refer to the manual if needed).</li><li>Put all the <strong>pieces</strong> (bolts, planks, etc.) <strong>into a package</strong> and label it (I used zip-locks for that).</li><li><strong>Take a picture</strong> of the place where the component was mounted.</li></ol><p>By doing so, you won’t be overwhelmed with different types of bolts, circuit boards, etc. Moreover, you will be able to assemble it back, because you have all the pictures needed and don’t hesitate about what pieces to use and where to put them. This might sound like overkill, but it’s always better to be overprepared, right? :)</p><p>You can start disassembling the components in the same order they are mentioned in the manual. Please do it one by one, don’t remove multiple parts at once. Here is what I’ve ended up with:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PHQxBKbSCEygO_B-B7KKrw.jpeg" /><figcaption>Laptop’s internals disassembled and sorted</figcaption></figure><p>Some <strong>tips</strong> I haven’t found on the manual:</p><ul><li><strong>Heat sink:</strong> apply a little bit of pressure to undo the screws. After all of them are gone, use a tiny bit of force to lift the sink (that’s because of old thermal paste)</li><li><strong>Fan:</strong> if you are failing to unplug the fan from the system board, it is ok. I had the same problem. Apply force to do it, use flat screwdrivers if needed (I ended up using two flat screwdrivers :)</li><li><strong>Speakers:</strong> check the wires for integrity after removal (mine were exposed in one place)</li><li>Make sure you have taken a picture of the display, wifi and power cables routing path.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h4CoOJS5rd6nbayd6IYe4w.jpeg" /><figcaption>Display, wifi and power cables routing path</figcaption></figure><p>When you are done, the only thing left is a bare palm rest with a keyboard and a metal plate that secures it in place. After some research, I’ve discovered that the keyboard is non-replaceable. That’s why I bought a new one. In order to fix the keyboard and a metal plate, you should melt small plastic ledges on the palm rest and a keyboard with a soldering iron and “glue” it to the metal plate. I must admit that I cheated here and had it done in a laptop repair shop (don’t put dislikes, everything else is done only by me :))</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MI3mfGePzqYR28rp3U9oLw.jpeg" /><figcaption>Metal plate, that secures keyboard in-place with melted plastic (black dots)</figcaption></figure><h3>Reassembling and Further Problems</h3><p>With the new palm rest and keyboard installed, it’s time to reassemble all the electronics and give it a try. With all the components sorted, that was a relatively easy task. I used <strong>Arctic MX-4</strong> thermal paste to be sure that the cooling system functions properly.</p><p><strong>Note:</strong></p><ol><li>Clean all the components with alcohol before reassembling (ports, old thermal paste, dust inside the fan, etc.)</li><li>Make sure you install the touchpad button board <strong>before</strong> the system board (otherwise it will be problematic to connect it to the touchpad board (green circuit))</li></ol><p>However, that was another problem standing by. The new <strong>bottom cover</strong> I bought <strong>lacked a hole for the VGA port</strong> (although it was described to be compatible with my laptop). It was too late to return, so I ended up making the hole manually using a drill and a file (the only advice here is extreme patience).</p><p>More problems were about to appear. After installing the new bottom cover, I discovered that the <strong>keyboard was not functioning correctly</strong>. Weird stuff was going on. When pressing the keys from the top row (QWERTY), middle row characters were appearing on the screen (ASDFGH). There was no clear correspondence between the keys, but I assumed that the keyboard cable was not properly aligned with a corresponding port on the system board. The issue was solved after reconnecting the keyboard. But the problems didn’t end there :)</p><h3>Salvaging the Old Touchpad</h3><p>After solving the keyboard issue, I noticed that the <strong>two-finger scroll was not working</strong>. I have tried many things with no success: reconnecting the touchpad button board, looking for the newest drivers and even reinstalling the OS. Nothing helped, though. Then I compared a new touchpad brand with the original one. It was different! I couldn’t even think that the palm rest can have a different touchpad for the same laptop model and that this can be a problem. I double-checked my order and didn’t find any laptop model mismatch. As I read later, the original touchpad model is written in BIOS, so no other touchpad should be installed on the laptop.</p><p>I have almost accepted defeat, as no touchpad disassembling instructions were provided in the manual. But then I bumped into this <a href="https://www.youtube.com/watch?v=IOj2c2UBMdo&amp;t=63s">video</a>. The authors claimed that the <strong>touchpad could be separated from the palm rest with no damage done to both</strong>. After that, both surfaces should be cleaned up from glue. The touchpad can then be installed on the other palm rest with double-sided tape. I decided to give it a try on the old palm rest.<br>The worst part of the whole process was to push the flat screwdriver in-between the touchpad and the palm rest plastic in order to start separating it from the glue. I ended up starting with a small chisel-like screw followed by a big flat screwdriver. Once it is under the touchpad, it should be easier. The only tip here is extreme patience: take your time and don’t rush or you are going to break the plastic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kkJv_MkDXMvfCvC_bAc53A.jpeg" /><figcaption>Salvaging the old touchpad: start with a chisel-like screw (blue) and continue with a big flat screwdriver (red)</figcaption></figure><p>I wanted to check if it still works, so I mounted it back on the old palm rest and moved the essential electronic components there. To my relief, it worked perfectly fine: all multi-finger gestures were appropriately recognised. After that, I removed the touchpad from the new palm rest and transferred the old touchpad there.</p><h3>Broken Screen and Hinges</h3><p>The laptop has started to gain a better appearance, but there was another problem: <strong>part of the display stopped working and went black</strong>. I detected that the issue occurs because of bad contacts inside the enclosure. It turns back to normal for a while after pushing on the plastic, but that’s just a temporary fix. Considering the fact that the plastic started to crack, I decided to disassemble the screen.</p><p>After a consultation with the manual, I found that the task is quite similar to what I’ve done before: a bunch of plastic clips around the bezel. However, I didn’t consider one thing: glue. It appeared to be no clips at the bottom (both on the manual and youtube videos), but that was not true. I damaged the screen with the screwdriver while trying to remove the bezel from the glue.</p><p>Note: <strong>there are clips at the bottom of the bezel too!</strong> Make sure you undo them first and then deal with the glue. The other parts of the screen were not hard to remove: a few bolts, 2 hinges and 2 cables (wifi antenna and a webcam).</p><p>To my surprise, <strong>both of the hinges were broken</strong> (that was the reason for the cracks on the plastic). So I definitely needed replacement parts: new hinges, display enclosure and a screen itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pd7dwo9B0DZ00wvuYk-5-Q.png" /><figcaption>Broken hinges (left, both at the same spot) and screen (right)</figcaption></figure><p>I had doubts on the last, however. FHD models were not a part of HP 250 G4 laptops, but G5 models were equipped with them. Since G5 is just a slightly better version of G4, I suspected that it is possible to upgrade to FHD display. After consultations in a few online shops, I’ve collected different opinions. The major concern was the display cable capability of transferring FHD signal instead of HD (single and dual-channel cables). After reading G4 and G5 manuals, I discovered that all G4 and G5 laptops use the same screen cable model. So I decided to give it a try and ordered an <strong>FHD matte IPS panel</strong>. To my relief, it worked perfectly! That’s a noticeable upgrade in terms of resolution and technology (brighter colors compared to an original TN screen).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7v1Daq1GZ3G1E3Bi9s0Ucw.jpeg" /><figcaption>Testing out the new IPS screen</figcaption></figure><h3>Final Assembly and Testing</h3><p>Not much work here. Just replace old components with new ones, route the wires properly and give it a test try without a bezel installed. However, my display enclosure came as a bare piece of plastic, so I had to make foil and foam rubber paddings by hand.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jY3xpNIRBxiLC9yyaFXgRw.jpeg" /><figcaption>Hand-crafted foil and foam rubber paddings</figcaption></figure><p><strong>Tips:</strong></p><ol><li>Make sure you <strong>route webcam and wifi cables properly</strong> (mine were not working because I routed them incorrectly and they were pressed by one of the hinges)</li><li>Make sure you <strong>test</strong> all the components <strong>before closing the screen bezel</strong> (webcam, microphone, wifi, screen)</li><li>Watch out for the bezel (I bet mine was not compatible with the display enclosure because I couldn’t do bottom clips. I rolled back to an original bezel and it worked just fine)</li><li><strong>Do screen bezel aesthetics at the very end</strong> (I transferred all rubber parts to the new screen bezel before recognizing that I need to stick with the original one)</li></ol><p>Now it is time to gather it all up. After installing the bezel and connecting the screen to the palm rest, I installed the bottom cover and transferred all rubber supports in there. The laptop renovating was finished after doing the final screw into the bottom cover and unsticking the HP logo at the backside of the screen.</p><p>The test run went fine. I watched the same core temperatures as before the upgrade (both cores were around 37 degrees in idle mode). GPU also worked fine and helped me to watch 1080p video without lags (make sure you install Radeon drivers, however). The response time was pretty low in general.</p><h3>Estimate</h3><p>I was interested in calculating how much did the whole build cost me (all prices are in local currency):</p><ol><li>SSD drive: 1969 UAH</li><li>display: 1670 UAH</li><li>new battery: 1146 UAH</li><li>palm rest: 1034 UAH</li><li>display enclosure: 916 UAH</li><li>RAM: 885 UAH</li><li>bottom cover: 605 UAH</li><li>hinges: 253 UAH</li><li>keyboard: 190 UAH</li><li>thermal paste: 179 UAH</li><li>HDD caddy: 167 UAH</li></ol><p>So it’s 9014 UAH or roughly 330 USD. Considering post office fees and work in a laptop repair shop it is <strong>380 USD</strong>.</p><p>Is it worth it? Who knows, maybe I should have bought a new laptop. But I can confirm that it was extremely fun and challenging. Moreover, it is cheaper than buying a new laptop with similar specs. I’ve gained lots of new experience and self-confidence and don’t regret it completely.</p><h3>Conclusions</h3><p>Here are my conclusions after all that I’ve been through:</p><ol><li>Try to <strong>estimate</strong> how much your laptop upgrade would cost <strong>beforehand</strong> and whether it’s worth it (what seemed like a simple replacement of a few components ended up in “rebuying” almost the whole laptop)</li><li>Make sure you <strong>know the exact model of your laptop</strong> (mine was advertised as G5 but is actually G4 :))</li><li>Consider buying a <strong>metal laptop</strong> instead of a plastic one if you want to use it on an everyday basis or move it from place to place frequently.</li><li>Make sure you order <strong>exactly the spare part you need</strong>, and it is compatible with your laptop (wrong touchpad, missing holes in a bottom cover, no foil on display enclosure, etc.). Check the part number or call the shop to clarify all the necessary details if needed.</li><li><strong>Don’t order the new palm rest with a touchpad</strong> if you can salvage the old one.</li><li>Use <strong>service manual</strong> as a primary source of information.</li><li>Do some <strong>aesthetics at the very end</strong> (rubber pieces, etc.).</li></ol><p>I hope some of you will find this post useful and won’t repeat my mistakes. Step out of your comfort zone, develop new skills and progress constantly!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7e7f5a6938f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>