<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Layman's Guide to Computing</title><link href="https://ngjunsiang.github.io/laymansguide/" rel="alternate"></link><link href="https://ngjunsiang.github.io/laymansguide/feeds/all.atom.xml" rel="self"></link><id>https://ngjunsiang.github.io/laymansguide/</id><updated>2026-08-31T08:00:00+08:00</updated><entry><title>Issue 182: Running a model, part 2</title><link href="https://ngjunsiang.github.io/laymansguide/issue182.html" rel="alternate"></link><published>2026-08-31T08:00:00+08:00</published><updated>2026-08-31T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-08-31:/laymansguide/issue182.html</id><summary type="html">&lt;p&gt;Open-weight models range in size from sub-1B to 100+B. A range of device options below &lt;span class="caps"&gt;SGD6&lt;/span&gt;,000 are already capable of running these models, ranging from the humble Raspberry Pi for running harness support to the Mac Studio M3 for running 70B&amp;nbsp;models.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Quantization trades parameter precision for a smaller memory footprint and faster inference, making many models feasible for running on user devices. Model capabilities depend on their parameter count and training data. Models with higher parameter counts can represent more patterns, while model capabilities are added by training them on well-labeled&amp;nbsp;data.&lt;/p&gt;
&lt;p&gt;Last issue, we discovered that there are quantized models that can actually run on laptops. (You can also run &lt;span class="caps"&gt;GPT&lt;/span&gt;-1 and &lt;span class="caps"&gt;GPT&lt;/span&gt;-2 on a laptop, but you would likely be disappointed in their performance today given the leaps-and-bounds improvement in &lt;span class="caps"&gt;AI&lt;/span&gt; capability that have happened since&amp;nbsp;2022.)&lt;/p&gt;
&lt;p&gt;Besides gemma-4-12b, what else can we&amp;nbsp;run?&lt;/p&gt;
&lt;h2&gt;Open-weight model&amp;nbsp;options&lt;/h2&gt;
&lt;p&gt;Open-weight enthusiasts have a number of well-known options available to them (sizes are&amp;nbsp;unquantized):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Google&amp;#8217;s Gemma 3 series, available in sizes 1B to&amp;nbsp;27B&lt;/li&gt;
&lt;li&gt;Google&amp;#8217;s Gemma 4 series, available in sizes 5B to&amp;nbsp;31B&lt;/li&gt;
&lt;li&gt;Microsoft&amp;#8217;s Phi series, available in sizes 2.7B to&amp;nbsp;14B&lt;/li&gt;
&lt;li&gt;Meta&amp;#8217;s Llama series, available in sizes 8B to&amp;nbsp;405B&lt;/li&gt;
&lt;li&gt;Alibaba&amp;#8217;s Qwen series, available in sizes 0.5B to&amp;nbsp;72B&lt;/li&gt;
&lt;li&gt;Deepseek&amp;#8217;s titular Deepseek series, available in sizes 16B to&amp;nbsp;72B&lt;/li&gt;
&lt;li&gt;Mistral&amp;#8217;s Mistral series, available in sizes 7B to&amp;nbsp;176B&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are also many lesser-known models, whose capabilities are still increasing every few&amp;nbsp;months.&lt;/p&gt;
&lt;p&gt;I won&amp;#8217;t give you a comprehensive low-down on what each model is good for,&amp;nbsp;because:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the models can be fine-tuned by those who know how, and may have variants that are better at specific task&amp;nbsp;categories,&lt;/li&gt;
&lt;li&gt;the models are updated every few months, and see new capabilities added through post-training (supervised&amp;nbsp;learning),&lt;/li&gt;
&lt;li&gt;the agent harness and runtime do play a part: some models are useful &amp;#8220;out-of-the-box&amp;#8221;, some work best within a particular harness or with a particular set of&amp;nbsp;tools&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Model&amp;nbsp;capabilities&lt;/h2&gt;
&lt;p&gt;In &lt;a href="https://ngjunsiang.github.io/laymansguide/issue181.html"&gt;Issue 181&lt;/a&gt; I mentioned that &lt;strong&gt;more parameters&lt;/strong&gt; lets the model represent more patterns in its weights, while &lt;strong&gt;better training data&lt;/strong&gt; determines the model&amp;#8217;s capabilities. Useful to know as a general pattern, but difficult to apply when deciding on a specific model to run. Should we just run the largest model that our device is capable of&amp;nbsp;running?&lt;/p&gt;
&lt;p&gt;As of June&amp;nbsp;2026:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;0.5B–3B&lt;/strong&gt; models can handle classification, extraction, summarization tasks and are generally good for single request-response&amp;nbsp;purposes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;7B–9B&lt;/strong&gt; models are useful assistants (in a harness) that can hold short conversations, handle basic Q&amp;amp;A, do simple coding or tool calls, and otherwise generally match &lt;span class="caps"&gt;GPT&lt;/span&gt;-3&amp;#8217;s&amp;nbsp;capabilities.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;12B–15B&lt;/strong&gt; models can follow instructions consistently, generate code that mostly works (and do some debugging if necessary), generate tool calls more reliably, making them capable tool-using&amp;nbsp;agents.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;27B–35B&lt;/strong&gt; models can handle most tasks, even across longer contexts: analyze documents and write reports, generate and debug code, execute requests involving multiple steps. With a well-designed harness and accurate task documentation, these become capable general-purpose&amp;nbsp;agents.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;70B&lt;/strong&gt; models can handle what previous tiers can do, but better: fewer hallucinations and mistakes, better answers, better general understanding, more consistent planning, and over longer context windows—smaller models sometimes see a sharp performance drop when the context window extends past a certain length. Some users report better reasoning performance as&amp;nbsp;well.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;100B+ Frontier models&lt;/strong&gt;—&lt;span class="caps"&gt;GPT&lt;/span&gt;-5, Claude Opus, Kimi K2.5, et al—can do all of the above, with state-of-the-art reasoning and thinking, knowledge, error recovery, ambiguity handling, and&amp;nbsp;more&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Specialized non-&lt;span class="caps"&gt;LLM&lt;/span&gt; models&amp;nbsp;include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;OpenAI Whisper (0.4B–1.5B)&lt;/strong&gt; for speech-to-text transcription, text-to-speech&amp;nbsp;generation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stable Diffusion (0.9B–8B)&lt;/strong&gt; for text-to-image&amp;nbsp;generation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;span class="caps"&gt;FLUX&lt;/span&gt;.1 (12B)&lt;/strong&gt; also for text-to-image&amp;nbsp;generation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;span class="caps"&gt;CLIP&lt;/span&gt; (0.4B)&lt;/strong&gt; for image-to-text&amp;nbsp;understanding&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stable Audio 3 (0.6B–2B)&lt;/strong&gt; for text-to-audio&amp;nbsp;generation&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Models are still improving through post-training (supervised learning) and distillation—a process by which small models are trained on output from larger, more capable models. A 9B model today already exhibits capabilities that &lt;span class="caps"&gt;GPT&lt;/span&gt;-3 (175B) was capable of in 2022. So you should expect a different set of capability tiers this time next&amp;nbsp;year.&lt;/p&gt;
&lt;h2&gt;Hardware&amp;nbsp;options&lt;/h2&gt;
&lt;p&gt;The sweet spot for &amp;#8220;value-for-money&amp;#8221; sits around &lt;strong&gt;12B–35B&lt;/strong&gt; for now. Smaller models are faster and use less memory. Speed decreases and memory use increases as model size&amp;nbsp;increases.&lt;/p&gt;
&lt;p&gt;With this in mind, these are some popular options for running models on-device (local deployment) as of June 2026 (prices are Singapore&amp;nbsp;retail):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Raspberry Pi&lt;/strong&gt; (8–&lt;span class="caps"&gt;16GB&lt;/span&gt; &lt;span class="caps"&gt;RAM&lt;/span&gt;): popular for tiny models (2B or smaller), used to generate document embeddings for search, &lt;span class="caps"&gt;OCR&lt;/span&gt; documents and clean up the OCRed text, etc. These form the support system for the agent harness, and usually are not used directly for the agent&amp;nbsp;models.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mini-PCs&lt;/strong&gt; with a sufficiently capable &lt;span class="caps"&gt;CPU&lt;/span&gt;, no dedicated &lt;span class="caps"&gt;GPU&lt;/span&gt; are a decent budget option.&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;span class="caps"&gt;AMD&lt;/span&gt; Ryzen &lt;span class="caps"&gt;AI&lt;/span&gt; 300 CPUs, 12 &lt;span class="caps"&gt;CPU&lt;/span&gt; cores, 8–12 &lt;span class="caps"&gt;GPU&lt;/span&gt; compute units &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; &lt;span class="caps"&gt;64GB&lt;/span&gt; &lt;span class="caps"&gt;RAM&lt;/span&gt;&lt;/strong&gt;: this can run 7B–13B models capably (if slowly), and 34B quantized models at a crawl. [~&lt;span class="caps"&gt;SGD2&lt;/span&gt;,000]&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;span class="caps"&gt;AMD&lt;/span&gt; Ryzen &lt;span class="caps"&gt;AI&lt;/span&gt; &lt;span class="caps"&gt;MAX&lt;/span&gt;+ (Strix Halo) CPUs, 16 &lt;span class="caps"&gt;CPU&lt;/span&gt; cores, 40–48 &lt;span class="caps"&gt;GPU&lt;/span&gt; compute units &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; &lt;span class="caps"&gt;256GB&lt;/span&gt; &lt;span class="caps"&gt;RAM&lt;/span&gt;&lt;/strong&gt;: this bundles a much more capable integrated &lt;span class="caps"&gt;GPU&lt;/span&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue123.html"&gt;Issue 123&lt;/a&gt;) and can run 34B models capably, 70B models at a crawl. [~&lt;span class="caps"&gt;SGD4&lt;/span&gt;,800]&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mac Mini M4, 12 &lt;span class="caps"&gt;CPU&lt;/span&gt; cores, 10 &lt;span class="caps"&gt;GPU&lt;/span&gt; compute units &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; &lt;span class="caps"&gt;24GB&lt;/span&gt; &lt;span class="caps"&gt;RAM&lt;/span&gt;&lt;/strong&gt;: In a similar category as the Ryzen &lt;span class="caps"&gt;AI&lt;/span&gt; 300. [&lt;span class="caps"&gt;SGD1&lt;/span&gt;,299]&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mac Mini M4 Pro, 14 &lt;span class="caps"&gt;CPU&lt;/span&gt; cores, 20 &lt;span class="caps"&gt;GPU&lt;/span&gt; compute units &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; &lt;span class="caps"&gt;48GB&lt;/span&gt; &lt;span class="caps"&gt;RAM&lt;/span&gt;&lt;/strong&gt;: In a similar category as the Ryzen &lt;span class="caps"&gt;AI&lt;/span&gt; &lt;span class="caps"&gt;MAX&lt;/span&gt;+. [&lt;span class="caps"&gt;SGD2&lt;/span&gt;,659]&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mac Studio M3 Ultra, 28 &lt;span class="caps"&gt;CPU&lt;/span&gt; cores, 60 &lt;span class="caps"&gt;GPU&lt;/span&gt; compute units &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; &lt;span class="caps"&gt;96GB&lt;/span&gt; &lt;span class="caps"&gt;RAM&lt;/span&gt;&lt;/strong&gt;: With the highest memory bandwidth of all the units in this category, this can run everything mentioned above, and even run 70B models decently well. That&amp;#8217;s what most folks would be buying this for.&lt;br /&gt;
A higher-end 32 &lt;span class="caps"&gt;CPU&lt;/span&gt; core, 80 &lt;span class="caps"&gt;GPU&lt;/span&gt; compute unit configuration exists if you add &lt;span class="caps"&gt;SGD2&lt;/span&gt;,025—doesn&amp;#8217;t add new capabilities, makes everything a little faster. [&lt;span class="caps"&gt;SGD5&lt;/span&gt;,199]&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Full PCs&lt;/strong&gt; with a capable &lt;span class="caps"&gt;CPU&lt;/span&gt; &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; dedicated &lt;span class="caps"&gt;GPU&lt;/span&gt;&lt;ul&gt;
&lt;li&gt;Many options exist here, none below &lt;span class="caps"&gt;SGD6&lt;/span&gt;,000, most above &lt;span class="caps"&gt;SGD10&lt;/span&gt;,000. Dedicated GPUs capable of running &lt;span class="caps"&gt;AI&lt;/span&gt; models already have prices in the&amp;nbsp;thousands.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you already have an existing laptop/&lt;span class="caps"&gt;PC&lt;/span&gt; and want to know how it will manage different model sizes, you can ask ChatGPT or Claude; they are pretty up-to-date with hardware capabilities and can give you an estimate. Alternatively, try to download and run the models and see for yourself—ground truth doesn&amp;#8217;t care about your&amp;nbsp;estimates.&lt;/p&gt;
&lt;h2&gt;Cloud&amp;nbsp;options&lt;/h2&gt;
&lt;p&gt;Wow that&amp;#8217;s a lot of zeros. Besides, owning hardware comes with its own maintenance needs and headaches. Enter the cloud, i.e.&amp;nbsp;pay-per-use.&lt;/p&gt;
&lt;p&gt;If you don&amp;#8217;t want to have to manage the hardware that runs these models, don&amp;#8217;t plan to be running a model long-term, or want to run a model larger than what your hardware can handle, these are the current most user-friendly&amp;nbsp;options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://huggingface.co/models?inference_provider=hf-inference"&gt;HuggingFace&lt;/a&gt; not only catalogues model weights, it also automates inference hosting (provided by &lt;span class="caps"&gt;AWS&lt;/span&gt; or Google Cloud underneath). Caveat: not all models are supported; you need a model that lists &amp;#8220;&lt;span class="caps"&gt;HF&lt;/span&gt; Inference &lt;span class="caps"&gt;API&lt;/span&gt;&amp;#8221; as an Inference Provider. The HuggingFace link in this bullet point links you to models that do. On the model card page, click &lt;strong&gt;Deploy &amp;gt; &lt;span class="caps"&gt;HF&lt;/span&gt; Inference&amp;nbsp;Endpoints&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://replicate.com/explore"&gt;Replicate&lt;/a&gt; provides an even simpler interface, but for a smaller catalogue of models. Try out the models directly on the model card page, or create an account for deployment&amp;nbsp;options.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://fireworks.ai/models"&gt;Fireworks &lt;span class="caps"&gt;AI&lt;/span&gt;&lt;/a&gt; is where you go once you&amp;#8217;ve decided on a (supported) model and want reliable hosting. Browse their model list and click &lt;strong&gt;Try In Playground&lt;/strong&gt; or &lt;strong&gt;Deploy On Demand&lt;/strong&gt; (requires&amp;nbsp;registration).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are other options that require more technical expertise to use, but if you reach that point you shouldn&amp;#8217;t be relying on a layman&amp;#8217;s guide anymore&amp;nbsp;:)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Open-weight models range in size from sub-1B to 100+B. A range of device options below &lt;span class="caps"&gt;SGD6&lt;/span&gt;,000 are already capable of running these models, ranging from the humble Raspberry Pi for running harness support to the Mac Studio M3 for running 70B models. For larger models, or short-term workloads, cloud options for deploying and running open-weight models also&amp;nbsp;exist.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;This is the most tentative issue for this season, and probably for the entire newsletter so far. I try not to write issues that I will have to retroactively edit as the frontier shifts, but I&amp;#8217;ll make this an exception: I think expounding on available open-weight models illustrates how the ecosystem is similar to open-source software, that allows the (sufficiently educated) public to experiment and provide feedback, how advances in &lt;span class="caps"&gt;AI&lt;/span&gt; over the past 3–4 years have made them feasible to run on consumer-class devices, and how cloud infrastructure has made larger models accessible to those who don&amp;#8217;t own sufficiently powerful&amp;nbsp;hardware.&lt;/p&gt;
&lt;h2&gt;The Layman&amp;#8217;s Guide to Computing&amp;nbsp;archive&lt;/h2&gt;
&lt;p&gt;Buttondown still does not have a very browseable archive, so I&amp;#8217;ve made the newsletter content available on a static site. You can browse past seasons more easily at &lt;a href="https://ngjunsiang.github.io/laymansguide/categories"&gt;https://ngjunsiang.github.io/laymansguide/categories&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I may add more seasons in future, as computing technology stabilizes enough for me to write about them in a static newsletter. If you&amp;#8217;d like to receive future issues, do subscribe&amp;nbsp;below:&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 181: Quantization</title><link href="https://ngjunsiang.github.io/laymansguide/issue181.html" rel="alternate"></link><published>2026-08-24T08:00:00+08:00</published><updated>2026-08-24T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-08-24:/laymansguide/issue181.html</id><summary type="html">&lt;p&gt;Quantization trades parameter precision for a smaller memory footprint and faster inference, making many models feasible for running on user devices. Model capabilities depend on their parameter count and training data. Models with higher parameter counts can represent more patterns, while model capabilities are added by training them on well-labeled&amp;nbsp;data.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Proprietary models do not have their weights published publicly, while open-weight models do. Various runtimes are available for download, and can run models that have a compatible file format. But models are extremely compute- and memory-intensive, requiring extremely high-end hardware and capacious memory to&amp;nbsp;run.&lt;/p&gt;
&lt;p&gt;Great, so a 12B model takes up &lt;span class="caps"&gt;24GB&lt;/span&gt; of disk space, uses &lt;span class="caps"&gt;24GB&lt;/span&gt; of &lt;span class="caps"&gt;RAM&lt;/span&gt;, and up to &lt;span class="caps"&gt;96GB&lt;/span&gt; for the &lt;span class="caps"&gt;KV&lt;/span&gt; cache (model&amp;#8217;s calculated representation of input tokens). That&amp;#8217;s out of reach for most consumers without &lt;span class="caps"&gt;AI&lt;/span&gt;-grade GPUs, which currently cost tens of thousands per&amp;nbsp;unit.&lt;/p&gt;
&lt;p&gt;Enter&amp;nbsp;quantization.&lt;/p&gt;
&lt;h2&gt;Parameter&amp;nbsp;representation&lt;/h2&gt;
&lt;p&gt;Models are typically trained with full precision, allowing them to store each parameter using 16 bits (2 bytes). This is necessary because the training process results in multiple adjustments to the weights. If the intermediate values are not stored with full precision, subsequent adjustments to those values are not accurately represented, and may result in inaccurate training&amp;nbsp;results.&lt;/p&gt;
&lt;p&gt;However, once the model is trained and its weights released, they are effectively &amp;#8220;frozen&amp;#8221;: the weights do not change as the model is used for inference (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue173.html"&gt;Issue 173&lt;/a&gt;).&lt;/p&gt;
&lt;h2&gt;Quantizing&amp;nbsp;parameters&lt;/h2&gt;
&lt;p&gt;Can we reduce the model size and memory footprint by reducing the precision? Yes. Experiments have shown that models lose some accuracy as their parameters are quantized: represented using 8 bits (twofold reduction), or even 4 bits (fourfold reduction!). Below that range, running the model at 2 bits often results in unacceptable&amp;nbsp;performance.&lt;/p&gt;
&lt;p&gt;This inaccuracy shows up in models not following instructions as well, potentially making mistakes more noticeably, especially on complex tasks, or being less accurate with tool call syntax. However, compared to the alternative of not running the model at all, this is usually an acceptable tradeoff for users running the model on their own&amp;nbsp;computers.&lt;/p&gt;
&lt;h2&gt;Running a quantized&amp;nbsp;model&lt;/h2&gt;
&lt;p&gt;Okay, let&amp;#8217;s run those numbers on a quantized Gemma 4 12B model. We don&amp;#8217;t even need to do the quantization ourselves usually: other enthusiasts have already done it, &lt;a href="https://huggingface.co/Brunobkr/OFFELLIA_Q4_0_gemma-4-12B-it.gguf"&gt;providing the models on HuggingFace as well&lt;/a&gt; (they can be identified through the &amp;#8220;Q4&amp;#8221; in the model naming scheme; 8-bit quantized models are labelled&amp;nbsp;&amp;#8220;Q8&amp;#8221;).&lt;/p&gt;
&lt;p&gt;We already see immediate benefits: the 4-bit quantized model weights are only &lt;span class="caps"&gt;7GB&lt;/span&gt;, a stark contrast to the &lt;span class="caps"&gt;24GB&lt;/span&gt; of full-precision&amp;nbsp;weights.&lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;KV&lt;/span&gt; cache requirement now drops to ~&lt;span class="caps"&gt;6GB&lt;/span&gt; for 32K tokens, and ~&lt;span class="caps"&gt;50GB&lt;/span&gt; for 256K tokens. &lt;em&gt;Very&lt;/em&gt; uncomfortable for a Macbook, which means we would have to limit ourselves to a 128K or even 64K token context length. Annoying, but not&amp;nbsp;show-stopping.&lt;/p&gt;
&lt;p&gt;The inference speed now increases to ~60 tokens/sec, about as responsive as ChatGPT or other&amp;nbsp;chatbots!&lt;/p&gt;
&lt;h2&gt;What do we gain from larger&amp;nbsp;models?&lt;/h2&gt;
&lt;p&gt;Unlike programs or data files, which store data as-is (perhaps compressing them for a smaller filesize), models &lt;strong&gt;represent&lt;/strong&gt; information: the training process produces a highly compressed set of numbers that are able to approximately reproduce the training contents (not 100% accurately, but quite close), and more importantly generate tokens following the same pattern for inputs that it was not trained&amp;nbsp;on.&lt;/p&gt;
&lt;p&gt;What if we try to break the laws of physics, taking &lt;span class="caps"&gt;GPT&lt;/span&gt; or Claude&amp;#8217;s training corpus, and training it into a 1B model? What&amp;nbsp;happens?&lt;/p&gt;
&lt;p&gt;1B parameters means the model only has 1 billion numbers to try to represent everything. If the training data is repetitive and largely similar, 1B might even be sufficient since there just isn&amp;#8217;t that much variation in the&amp;nbsp;data.&lt;/p&gt;
&lt;p&gt;But if the data is highly varied, the model might not be able to adjust the weights to represent everything. It will end up storing one additional data point at the expense of worse representation for other data points. This might show up as a plateau in benchmark scores: the model can&amp;#8217;t improve further. Or it might show up as the model not &amp;#8220;remembering&amp;#8221; data that shows up less&amp;nbsp;frequently.&lt;/p&gt;
&lt;p&gt;What do frontier models, often with parameter counts running into trillions, gain? With so many parameters, they can represent more patterns: more thinking scaffolds and reasoning frameworks, more sentence/paragraph patterns from more books and articles, etc. And not just more patterns, but higher-order patterns: writing styles, writing intents, idea development, longform writing structure,&amp;nbsp;etc.&lt;/p&gt;
&lt;p&gt;Google&amp;#8217;s Gemma 4 12B model will end up not being able to represent everything. Our running model might give less nuanced answers, consider fewer perspectives in its answer, and otherwise give worse&amp;nbsp;answers.&lt;/p&gt;
&lt;p&gt;But hey, it runs! Give it a spin, see what you can do with 12B&amp;nbsp;parameters.&lt;/p&gt;
&lt;h2&gt;Model&amp;nbsp;capabilities&lt;/h2&gt;
&lt;p&gt;Even frontier models with poor training data will disappoint. 1 trillion parameters won&amp;#8217;t necessarily make a model much smarter if the training data is&amp;nbsp;poor.&lt;/p&gt;
&lt;p&gt;Most new capabilities are added through additional training, usually supervised learning. If we can&amp;#8217;t train the underlying model, we might be able to create skill files explaining how to do something, let the harness read it and add it into the input context, and lean on the model&amp;#8217;s pattern-following capabilities to tackle the&amp;nbsp;task.&lt;/p&gt;
&lt;p&gt;Either way, if you have the hardware to support it and manage to get a local agent running, try it with different questions and tasks to get a feel for what it can and cannot handle. That beats any amount of reading on what these models are supposed to be able to&amp;nbsp;do.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Quantization trades parameter precision for a smaller memory footprint and faster inference, making many models feasible for running on user devices. Model capabilities depend on their parameter count and training data. Models with higher parameter counts can represent more patterns, while model capabilities are added by training them on well-labeled&amp;nbsp;data.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;12 issues in, that&amp;#8217;s a wrap! At this point I think what I&amp;#8217;ve written is what&amp;#8217;s unlikely to change in the next couple of years, and still useful for layfolks to know about the ongoing &lt;span class="caps"&gt;AI&lt;/span&gt; development. Anything newer is still in active&amp;nbsp;development.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue182.html"&gt;Issue 182: Running a model, part&amp;nbsp;2&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the last issue, I&amp;#8217;ll explore other options for running a model on your device (called &lt;strong&gt;local deployment&lt;/strong&gt; in parlance): running smaller models, and other feasible hardware&amp;nbsp;options.&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 180: Running a model</title><link href="https://ngjunsiang.github.io/laymansguide/issue180.html" rel="alternate"></link><published>2026-08-17T08:00:00+08:00</published><updated>2026-08-17T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-08-17:/laymansguide/issue180.html</id><summary type="html">&lt;p&gt;Proprietary models do not have their weights published publicly, while open-weight models do. Various runtimes are available for download, and can run models that have a compatible file format. But models are extremely compute- and memory-intensive, requiring extremely high-end hardware and capacious memory to&amp;nbsp;run.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Agents are software applications that comprise a harness, a runtime, and a model (typically accessed through an &lt;span class="caps"&gt;API&lt;/span&gt; instead of directly executed on the computer). They enable a user to type in a request or send it by other means and thus instruct the agent to carry out a task on the computer until completion. The capabilities of agents are limited by the tools available to&amp;nbsp;them.&lt;/p&gt;
&lt;p&gt;As of June 2026, OpenAI and Anthropic charge about $20/mth for their Pro/Plus plan, and about $200/mth for their Max plan. For those of us who like to stay on free tiers, it can be pretty annoying to hit the dreaded &amp;#8220;You have reached the limit for Free plan&amp;#8221;, but what can we do short of shelling out for a higher&amp;nbsp;tier?&lt;/p&gt;
&lt;p&gt;Wait—if a language model is a bunch of numbers, and a runtime is just a program, why can&amp;#8217;t I run it on my own computer&amp;nbsp;instead?&lt;/p&gt;
&lt;h2&gt;Proprietary models and open-weight&amp;nbsp;models&lt;/h2&gt;
&lt;p&gt;For starters, you can&amp;#8217;t download the &lt;span class="caps"&gt;GPT&lt;/span&gt;-5 or Claude models. They are proprietary models, and their weights (the file containing the model&amp;#8217;s parameters) are a guarded trade secret; a leak of the weights would be disastrous for OpenAI or&amp;nbsp;Anthropic.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Okay, fine&lt;/em&gt; you say, &lt;em&gt;then let&amp;#8217;s run something I can actually download.&lt;/em&gt; As of 2026, that typically means you would go to &lt;a href="https://huggingface.co/"&gt;HuggingFace&lt;/a&gt; (yes, that is their actual name), currently the world&amp;#8217;s largest platform for hosting open-weight models. An open-weight model, analogous to open-source software, means the model&amp;#8217;s weights are publicly available and you can download&amp;nbsp;them.&lt;/p&gt;
&lt;h2&gt;The parts: downloading&amp;nbsp;weights&lt;/h2&gt;
&lt;p&gt;Let&amp;#8217;s download the currently top-trending model, Google&amp;#8217;s &lt;a href="https://huggingface.co/google/gemma-4-12B-it"&gt;&lt;code&gt;gemma-4-12B-it&lt;/code&gt;&lt;/a&gt;. The &lt;a href="https://huggingface.co/google/gemma-4-12B-it"&gt;model card&lt;/a&gt; says that this is a multimodal model (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue177.html"&gt;Issue 177&lt;/a&gt;) with 11.95 billion (12B) parameters (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue170.html"&gt;Issue 170&lt;/a&gt;). It has a context length of 256K tokens (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue172.html"&gt;Issue 172&lt;/a&gt;)—important when deciding what kind of tasks it can plausibly take on, since the context length dictates what the total output length (including the input tokens) cannot&amp;nbsp;exceed.&lt;/p&gt;
&lt;p&gt;Under &lt;a href="https://huggingface.co/google/gemma-4-12B-it/tree/main"&gt;Files and versions&lt;/a&gt;, we see a whole bunch of files, most of them metadata, configuration information, and other data (such as the token list). The model weights are easy to tell: they are by far the largest file of the collection, weighing in at 23.&lt;span class="caps"&gt;9GB&lt;/span&gt;. We can calculate this: 11.95 billion parameters, with each parameter taking up 16 bits (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue040.html"&gt;Issue 40&lt;/a&gt;), means 2 bytes per parameter, and thus 23.9 billion bytes for all the parameters. 23.&lt;span class="caps"&gt;9GB&lt;/span&gt;.&lt;/p&gt;
&lt;h2&gt;The&amp;nbsp;runtime&lt;/h2&gt;
&lt;p&gt;You have a few options here, listed from easiest to most&amp;nbsp;difficult:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="https://lmstudio.ai/"&gt;&lt;span class="caps"&gt;LM&lt;/span&gt; Studio&lt;/a&gt; – Comes with a graphical user interface (&lt;span class="caps"&gt;GUI&lt;/span&gt;), so click to load the model and you get a chat interface. Great for getting started &lt;span class="caps"&gt;ASAP&lt;/span&gt;, not great if you actually eventually want to use it as an&amp;nbsp;agent.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ollama.com/"&gt;Ollama&lt;/a&gt; – A commandline program, requiring some terminal chops. Sets up an &lt;span class="caps"&gt;API&lt;/span&gt; server that you can use with many other&amp;nbsp;programs.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/huggingface/transformers"&gt;Hugging Face Transformers&lt;/a&gt; – A Python library for working with models, which means it&amp;#8217;s programmers-only. Great if you are building or customizing your own agent harness, but definitely not ready-to-run&amp;nbsp;as-is.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/ggml-org/llama.cpp"&gt;llama.cpp&lt;/a&gt; – The most low-level, close-to-the-metal option. Gives you a commandline program for using the model, but you have to manage all other technical detail on your own. Not for the&amp;nbsp;faint-hearted.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/vllm-project/vllm"&gt;vLLM&lt;/a&gt; – A &lt;span class="caps"&gt;GPU&lt;/span&gt;-only library for serving models over an &lt;span class="caps"&gt;API&lt;/span&gt;. Presumably we do not have four thousand bucks to spend on an entry-level &lt;span class="caps"&gt;GPU&lt;/span&gt; for models, such as the &lt;span class="caps"&gt;RTX&lt;/span&gt; 4090 with &lt;span class="caps"&gt;24GB&lt;/span&gt; of &lt;span class="caps"&gt;GPU&lt;/span&gt; memory, and are running the model on a &lt;span class="caps"&gt;CPU&lt;/span&gt;, so this option is automatically disqualified for&amp;nbsp;us.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Hardware&amp;nbsp;requirements&lt;/h2&gt;
&lt;p&gt;Great. So we&amp;#8217;ve downloaded and installed &lt;span class="caps"&gt;LM&lt;/span&gt; Studio, launched it, and then selected&amp;nbsp;our &lt;code&gt;gemma-4-12B-it&lt;/code&gt; model for&amp;nbsp;loading.&lt;/p&gt;
&lt;p&gt;&lt;img alt="A screenshot of LM Studio" src="https://ngjunsiang.github.io/laymansguide/lm-studio.png" /&gt;&lt;br /&gt;
&lt;em&gt;A screenshot of &lt;span class="caps"&gt;LM&lt;/span&gt; Studio&lt;/em&gt;&lt;br /&gt;
Source: &lt;a href="https://lmstudio.ai/"&gt;&lt;span class="caps"&gt;LM&lt;/span&gt;&amp;nbsp;Studio&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The first thing that would probably happen is your system will complain about insufficient memory and stop. You see, to run this model, we would need to read the model weights (23.&lt;span class="caps"&gt;9GB&lt;/span&gt;) into memory, immediately using up &lt;span class="caps"&gt;24GB&lt;/span&gt; of memory. Even assuming no other apps are running, we still need more memory for the following:
- operating system overhead (~1-&lt;span class="caps"&gt;2GB&lt;/span&gt;)
- memory used by the runtime (1-&lt;span class="caps"&gt;3GB&lt;/span&gt;)&lt;/p&gt;
&lt;p&gt;Oh? It didn&amp;#8217;t crash for you? I see, you had the Macbook Pro with &lt;span class="caps"&gt;64GB&lt;/span&gt; memory, or something in that weight class. Great, let&amp;#8217;s start prompting your model then. It won&amp;#8217;t work as quickly as ChatGPT, but it should manage a comfortable ~20–30 tokens/sec, slightly slower than reading speed but&amp;nbsp;useable.&lt;/p&gt;
&lt;p&gt;Unfortunately, as you ask more and more questions within the same session, it will run more and more slowly, and eventually it will crash. You see, the model generates a representation of the entire input, called the &lt;strong&gt;&lt;span class="caps"&gt;KV&lt;/span&gt; cache&lt;/strong&gt;, which stores its computed values for how each token in the input relates to other tokens in the input. This is estimated to take up ~&lt;span class="caps"&gt;12GB&lt;/span&gt; for 32K tokens, so ~&lt;span class="caps"&gt;96GB&lt;/span&gt; if using the full 256K context&amp;nbsp;length.&lt;/p&gt;
&lt;p&gt;Yeah, this isn&amp;#8217;t for the&amp;nbsp;faint-hearted.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Proprietary models do not have their weights published publicly, while open-weight models do. Various runtimes are available for download, and can run models that have a compatible file format. But models are extremely compute- and memory-intensive, requiring extremely high-end hardware and capacious memory to&amp;nbsp;run.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;This is the pessimistic view. Next issue, we look at some optimizations that are available even to newcomers to enable models to run faster and with a smaller memory&amp;nbsp;footprint.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue181.html"&gt;Issue 181:&amp;nbsp;Quantization&lt;/a&gt;&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 179: Agents</title><link href="https://ngjunsiang.github.io/laymansguide/issue179.html" rel="alternate"></link><published>2026-08-10T08:00:00+08:00</published><updated>2026-08-10T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-08-10:/laymansguide/issue179.html</id><summary type="html">&lt;p&gt;Agents are software applications that comprise a harness, a runtime, and a model (typically accessed through an &lt;span class="caps"&gt;API&lt;/span&gt; instead of directly executed on the computer). They enable a user to type in a request or send it by other means and thus instruct the agent to carry out a task on the computer until completion. The capabilities of agents are limited by the tools available to&amp;nbsp;them.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Thinking/reasoning models are those that have been trained on examples of how to think about different problems in different domains, or plan and execute complex tasks. They often use tools to aid them in goal tracking and updating. The full thinking trace from the model may be removed or hidden to present a more legible response to the&amp;nbsp;user.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s review the ingredients we have so&amp;nbsp;far:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A large language model (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue170.html"&gt;Issue 170&lt;/a&gt;) or multimodal model (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue177.html"&gt;Issue 177&lt;/a&gt;): a next-token predictor that takes input tokens and keeps generating output tokens which feed back to the&amp;nbsp;input&lt;/li&gt;
&lt;li&gt;Training data, which the model is trained on to pick up general patterns through unsupervised learning (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue171.html"&gt;Issue 171&lt;/a&gt;), and then steered to avoid harmful output and generate useful output through the use of labelled training data through supervised learning (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue174.html"&gt;Issue 174&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;A runtime (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue175.html"&gt;Issue 175&lt;/a&gt;), which handles multiple responsibilities:&lt;ul&gt;
&lt;li&gt;parsing the model output to block it if found to be&amp;nbsp;harmful&lt;/li&gt;
&lt;li&gt;formatting the text for display to the&amp;nbsp;user&lt;/li&gt;
&lt;li&gt;separating and executing tool calls (typically in an isolated container), and injecting the results back into the input (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue175.html"&gt;Issue 175&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;processing thinking tokens, removing or hiding them (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue178.html"&gt;Issue 178&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Other optional runtime extensions, such as those that add retrieval-augmented generation (&lt;span class="caps"&gt;RAG&lt;/span&gt;) capabilities (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue176.html"&gt;Issue 176&lt;/a&gt;), or add information that the model remembered about the signed-in&amp;nbsp;user&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;What does an agent&amp;nbsp;do?&lt;/p&gt;
&lt;h2&gt;&lt;span class="caps"&gt;AI&lt;/span&gt;&amp;nbsp;Agents&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;agent(n.)&lt;br /&gt;
late 15c., &amp;#8220;one who acts,&amp;#8221; from Latin &lt;em&gt;agentem&lt;/em&gt; (nominative &lt;em&gt;agens&lt;/em&gt;) &amp;#8220;effective, powerful,&amp;#8221; present participle of &lt;em&gt;agere&lt;/em&gt; &amp;#8220;to set in motion, drive forward; to do, perform; keep in movement&amp;#8221; (from &lt;span class="caps"&gt;PIE&lt;/span&gt; root &lt;strong&gt;*ag-&lt;/strong&gt; &amp;#8220;to drive, draw out or forth,&amp;nbsp;move&amp;#8221;).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The term &amp;#8220;agent&amp;#8221; means &amp;#8220;one who acts&amp;#8221;. So agents are software applications, comprising a trained model and a runtime. We can broadly think of the model as the &amp;#8220;brains&amp;#8221; of the partnership, and the runtime as the&amp;nbsp;&amp;#8220;body&amp;#8221;.&lt;/p&gt;
&lt;p&gt;Because agents need a computer (physical or virtual) to &amp;#8220;act&amp;#8221;, these software applications are typically installed on a computer, although they may also include a web interface to allow users to control them&amp;nbsp;remotely.&lt;/p&gt;
&lt;p&gt;The model has remained conceptually similar as I went from Issue 170 to here, but the runtime is picking up more and more responsibilities. So as not to muddy the terms, I&amp;#8217;ll keep the runtime focused on the model: processing the output, executing tool calls and injecting results, re-invoking the model if it has not reached a stop token, and any &lt;span class="caps"&gt;RAG&lt;/span&gt; if implemented. Everything else that we are adding today, that makes the agent an effective partner and piece of software, I&amp;#8217;ll explain under the label &lt;strong&gt;harness&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;The&amp;nbsp;model&lt;/h2&gt;
&lt;p&gt;Some harnesses make it easy to swap out the underlying model, allowing the model to run the agent harness with different models. Many model providers have standardized on OpenAI&amp;#8217;s &lt;span class="caps"&gt;API&lt;/span&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue004.html"&gt;Issue 4&lt;/a&gt;) so as to make their models easily accessible to&amp;nbsp;programmers.&lt;/p&gt;
&lt;p&gt;While state-of-the-art models are capable enough to not require a more specialized version for agentic use, the agent harness usually provides a special system prompt for this purpose. This special prompt includes information on the use context, on the tools available to the model, and other pertinent information to guide the model and keep it on&amp;nbsp;task.&lt;/p&gt;
&lt;h2&gt;The&amp;nbsp;runtime&lt;/h2&gt;
&lt;p&gt;A runtime used within a harness needs to include additional features: the ability to pause or stop the model, to understand access control configuration (which tool calls require user approval) and route matching tool calls to the user for permission grants, and introspectability: allowing the harness program to check the state of the runtime and&amp;nbsp;model.&lt;/p&gt;
&lt;h2&gt;The&amp;nbsp;harness&lt;/h2&gt;
&lt;p&gt;When a user uses agentic software, the harness is what they see. That means the harness handles typical software&amp;nbsp;responsibilities:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;it handles installation and initial setup, allowing the user to select a directory that the agent will begin working&amp;nbsp;from&lt;/li&gt;
&lt;li&gt;it handles extensions/plugins that the user may wish to install, making the tools/MCPs (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue175.html"&gt;Issue 175&lt;/a&gt;) available to the&amp;nbsp;runtime&lt;/li&gt;
&lt;li&gt;it handles file uploads (and any necessary format conversion or resizing), request customisation (e.g. enabling extended thinking), other request-related&amp;nbsp;settings&lt;/li&gt;
&lt;li&gt;it handles the model output through the runtime, displaying to the user tool calls and their results, any visible thinking traces, and any permission requests which come from the runtime (remember that the model remains unaware of these). If the &lt;span class="caps"&gt;API&lt;/span&gt; supports it, the harness streams these to the user, allowing them to see tokens as the model outputs them, without having to wait for the model to finish the entire&amp;nbsp;response&lt;/li&gt;
&lt;li&gt;it provides an interrupt mechanism for the user to halt the runtime if the model is going off-track, or to queue up more messages for the runtime to inject into the request at an appropriate&amp;nbsp;juncture&lt;/li&gt;
&lt;li&gt;some harnesses may support agent memory features, giving the agent tools to write information to its internal memory, and retrieve the information when&amp;nbsp;required&lt;/li&gt;
&lt;li&gt;harnesses for continuously running agents may include features for setting the wake-up interval of the agent, e.g. invoking the agent every 30 seconds with standard instructions to check for outstanding tasks and complete&amp;nbsp;them&lt;/li&gt;
&lt;li&gt;harnesses that integrate with external services will include features for receiving requests via email, WhatsApp, Telegram, or other channels, passing them to the agent and returning the response when it is&amp;nbsp;ready.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;What an agent&amp;nbsp;does&lt;/h2&gt;
&lt;p&gt;&amp;#8230; I don&amp;#8217;t know what to say here. By itself, a model can do nothing besides generate text. When embedded in a harness+runtime, what it can do is limited by the tools it has available—remember that the model relies on the runtime executing its tool calls to have any effect on the&amp;nbsp;world.&lt;/p&gt;
&lt;p&gt;With simple toolsets (primarily a commandline tool), the agent can&amp;nbsp;plausibly:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;read, edit, and delete text files on the&amp;nbsp;computer&lt;/li&gt;
&lt;li&gt;search through files on the&amp;nbsp;computer&lt;/li&gt;
&lt;li&gt;check the computer&amp;#8217;s stats, such as memory usage, free space on disk, &lt;span class="caps"&gt;CPU&lt;/span&gt;&amp;nbsp;usage&lt;/li&gt;
&lt;li&gt;troubleshoot or diagnose computer&amp;nbsp;issues&lt;/li&gt;
&lt;li&gt;perform a web search or retrieve a web&amp;nbsp;page&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If given the appropriate tools and permissions from the user, the agent can&amp;nbsp;also:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;install or uninstall software on the computer (through the&amp;nbsp;commandline)&lt;/li&gt;
&lt;li&gt;download source code, compile it, install it, and run&amp;nbsp;it&lt;/li&gt;
&lt;li&gt;run a server on the computer, handle web requests, return&amp;nbsp;responses&lt;/li&gt;
&lt;li&gt;read, write, and test&amp;nbsp;code&lt;/li&gt;
&lt;li&gt;push code to a code&amp;nbsp;repository&lt;/li&gt;
&lt;li&gt;add bug reports or issues to a task board, or read existing ones from&amp;nbsp;it&lt;/li&gt;
&lt;li&gt;send requests to an &lt;span class="caps"&gt;API&lt;/span&gt; (if authenticated by the user), and thus execute any supported action through the APIs of Google Drive, Dropbox, Notion, and other services (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue006.html"&gt;Issue 6&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With more advanced tools or &lt;span class="caps"&gt;MCP&lt;/span&gt; servers that handle the complex details, an agent can&amp;nbsp;even:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;be registered as a plugin in Adobe or Microsoft Office software, reading and editing&amp;nbsp;documents&lt;/li&gt;
&lt;li&gt;work with &lt;span class="caps"&gt;PDF&lt;/span&gt;&amp;nbsp;files&lt;/li&gt;
&lt;li&gt;fix&amp;nbsp;bugs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When provided with detailed explanations of how to perform complex tasks (typically through a skill file that the agent can read), the agent can&amp;nbsp;plausibly:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;analyze large&amp;nbsp;datasets&lt;/li&gt;
&lt;li&gt;follow company&amp;nbsp;workflows&lt;/li&gt;
&lt;li&gt;scan software or APIs for&amp;nbsp;vulnerabilities&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&amp;#8230; Why haven&amp;#8217;t they taken over the world&amp;nbsp;yet?&lt;/h2&gt;
&lt;p&gt;Because most people aren&amp;#8217;t using&amp;nbsp;them!&lt;/p&gt;
&lt;p&gt;&amp;#8230; Just kidding, there are other reasons too. For&amp;nbsp;example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Most complex tasks aren&amp;#8217;t described in skill files that are agent-readable, or are not well&amp;nbsp;described&lt;/li&gt;
&lt;li&gt;Many of the advanced tools or &lt;span class="caps"&gt;MCP&lt;/span&gt; servers that are needed don&amp;#8217;t exist, e.g. those for editing &lt;span class="caps"&gt;PDF&lt;/span&gt; files reliably. If they exist they aren&amp;#8217;t always&amp;nbsp;reliable&lt;/li&gt;
&lt;li&gt;The really effective tools might be hyper-customized for the tool author and not as useful for&amp;nbsp;others&lt;/li&gt;
&lt;li&gt;Most users are used to doing things themselves, and don&amp;#8217;t have enough experience with an agent harness to be accustomed to instructing&amp;nbsp;one&lt;/li&gt;
&lt;li&gt;Users might not know that it is possible to do something, and have not considered asking an agent to do&amp;nbsp;it&lt;/li&gt;
&lt;li&gt;Agent models still have limited context windows (even a context window of 1 million tokens can fill up quickly with a sufficiently complex task), and ways to enable a model to keep relevant task details in context while removing irrelevant details are still being&amp;nbsp;studied&lt;/li&gt;
&lt;li&gt;The model might not have been trained on a particular task, and its general reasoning capabilities might not be sufficient to carry out the task&amp;nbsp;effectively&lt;/li&gt;
&lt;li&gt;Agent harnesses tend to run in the commandline, or be designed primarily for programmer use, thus scaring layfolks&amp;nbsp;away&lt;/li&gt;
&lt;li&gt;&amp;#8230;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Agent capabilities tend to be emergent. That means researchers and frontier labs can train a model to carry out tasks A, B, and C, and a user giving the agent a different kind of task &lt;em&gt;discovers&lt;/em&gt; that it is also effective at task D but not task&amp;nbsp;E.&lt;/p&gt;
&lt;p&gt;Generally, a question can &amp;#8220;can an agent do F?&amp;#8221; can&amp;#8217;t be answered definitively prior to actually asking the model to do F. And even if one person fails to get the agent to execute the task successfully, another person might succeed, because they asked differently, because they are familiar with the terminology required to instruct the agent, or for some other&amp;nbsp;reason.&lt;/p&gt;
&lt;p&gt;All of this is still ongoing research work: agents only really took off in 2025, when &lt;a href="https://www.anthropic.com/news/claude-3-7-sonnet"&gt;Anthropic released Claude Code&lt;/a&gt; which became the first generally capable agent. Since then, every day users are discovering new things that it can do. The things that it can&amp;#8217;t, Anthropic and other frontier labs are still training it to be able to do&amp;nbsp;them.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Agents are software applications that comprise a harness, a runtime, and a model (typically accessed through an &lt;span class="caps"&gt;API&lt;/span&gt; instead of directly executed on the computer). They enable a user to type in a request or send it by other means and thus instruct the agent to carry out a task on the computer until completion. The capabilities of agents are limited by the tools available to&amp;nbsp;them.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;You now have a pretty good idea of all the pieces involved in getting an &lt;span class="caps"&gt;AI&lt;/span&gt; agent to do things. The part I can&amp;#8217;t authoritatively tell you about is what they can or can&amp;#8217;t do, because that is still changing every week as frontier labs continue to train more capable models and agent harnesses continue to add more tools and&amp;nbsp;features.&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;re curious, consider trying them out. You could search for an online guide, or let ChatGPT/Claude help get you&amp;nbsp;started.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;In ten issues, I&amp;#8217;ve walked you through the key concepts that help you understand what &lt;span class="caps"&gt;AI&lt;/span&gt; agents do. With three issues left to go, what else should I&amp;nbsp;cover?&lt;/p&gt;
&lt;p&gt;Some questions I&amp;#8217;m anticipating, or have fielded some variant&amp;nbsp;of:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Can I run my own &lt;span class="caps"&gt;AI&lt;/span&gt;&amp;nbsp;model?&lt;/li&gt;
&lt;li&gt;Why can&amp;#8217;t the &lt;span class="caps"&gt;AI&lt;/span&gt; do&amp;nbsp;&amp;lt;thing&amp;gt;?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Question 2 has a boring answer and an interesting one. The boring answer is &amp;#8220;because it hasn&amp;#8217;t been trained yet&amp;#8221;. The interesting answer is &amp;#8230; not really suitable for a newletter titled Layman&amp;#8217;s Guide to &lt;em&gt;Computing&lt;/em&gt;, because it&amp;#8217;ll be rooted in philosophy and cognitive science. In a different publication&amp;nbsp;perhaps.&lt;/p&gt;
&lt;p&gt;So let&amp;#8217;s tackle question 1, which will draw on computing concepts I&amp;#8217;ve covered in earlier issues and give you an idea of the kind of compute and memory capacity needed to run a&amp;nbsp;model.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue180.html"&gt;Issue 180: Running a&amp;nbsp;model&lt;/a&gt;&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 178: Model thinking and reasoning</title><link href="https://ngjunsiang.github.io/laymansguide/issue178.html" rel="alternate"></link><published>2026-08-03T08:00:00+08:00</published><updated>2026-08-03T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-08-03:/laymansguide/issue178.html</id><summary type="html">&lt;p&gt;Thinking/reasoning models are those that have been trained on examples of how to think about different problems in different domains, or plan and execute complex tasks. They often use tools to aid them in goal tracking and updating. The full thinking trace from the model may be removed or hidden to present a more legible response to the&amp;nbsp;user.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Multimodal models represent text, image, and audio tokens alongside each other in their embedding space. The model uses the input tokens, regardless of type, to calculate the next output token. Multimodal models typically only output text tokens in their response, delegating to more specialized models for image and audio generation if&amp;nbsp;necessary.&lt;/p&gt;
&lt;p&gt;In this issue we fill in the last piece of the puzzle needed to &amp;#8220;unlock untold economic value&amp;#8221;, if the &lt;span class="caps"&gt;AI&lt;/span&gt; labs are to be believed. Let&amp;#8217;s talk about how models&amp;nbsp;&amp;#8220;think&amp;#8221;.&lt;/p&gt;
&lt;h2&gt;Making thinking&amp;nbsp;happen&lt;/h2&gt;
&lt;p&gt;You&amp;#8217;re in a lesson. The teacher asks a question, something innocuous really: &amp;#8220;What&amp;#8217;s the value of X?&amp;#8221; All eyes are on you. You reply with the first answer off the top of your head. Wrongly, it turns&amp;nbsp;out.&lt;/p&gt;
&lt;p&gt;Your teacher could mock you at this point, but if they decide to get you to think harder instead, what do they&amp;nbsp;say?&lt;/p&gt;
&lt;p&gt;As it happens, this trick works on LLMs too. The ways we try to get people to think harder appear to be well-represented in books, on the internet, and in other media that the models are trained&amp;nbsp;on.&lt;/p&gt;
&lt;p&gt;What this means is that you add any of the&amp;nbsp;following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="dquo"&gt;&amp;#8220;&lt;/span&gt;think step by&amp;nbsp;step.&amp;#8221;&lt;/li&gt;
&lt;li&gt;&lt;span class="dquo"&gt;&amp;#8220;&lt;/span&gt;think&amp;nbsp;carefully.&amp;#8221;&lt;/li&gt;
&lt;li&gt;&lt;span class="dquo"&gt;&amp;#8220;&lt;/span&gt;check your assumptions before you&amp;nbsp;answer.&amp;#8221;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And it influences the model&amp;#8217;s next token. It begins to output phrases&amp;nbsp;like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="dquo"&gt;&amp;#8220;&lt;/span&gt;Let&amp;#8217;s break this&amp;nbsp;down.&amp;#8221;&lt;/li&gt;
&lt;li&gt;&lt;span class="dquo"&gt;&amp;#8220;&lt;/span&gt;First, let&amp;#8217;s identify what&amp;#8217;s being&amp;nbsp;asked.&amp;#8221;&lt;/li&gt;
&lt;li&gt;&lt;span class="dquo"&gt;&amp;#8220;&lt;/span&gt;One way to approach this&amp;nbsp;is&amp;#8230;&amp;#8221;&lt;/li&gt;
&lt;li&gt;&lt;span class="dquo"&gt;&amp;#8220;&lt;/span&gt;Before answering, let&amp;#8217;s&amp;nbsp;consider&amp;#8230;&amp;#8221;&lt;/li&gt;
&lt;li&gt;&lt;span class="dquo"&gt;&amp;#8220;&lt;/span&gt;Let&amp;#8217;s work through the problem&amp;nbsp;systematically.&amp;#8221;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It begins to imitate the patterns of careful thinking that it picked up during training. Surprisingly (or perhaps unsurprisingly), this improves the model&amp;#8217;s answer in many cases! It generates a much longer answer, taking more time and using more compute in the process—this is what &lt;span class="caps"&gt;AI&lt;/span&gt; folks call &amp;#8220;spending compute for intelligence&amp;#8221;. If you don&amp;#8217;t have a large &lt;span class="caps"&gt;LLM&lt;/span&gt;, you can have a smaller &lt;span class="caps"&gt;LLM&lt;/span&gt; &amp;#8220;think harder&amp;#8221; and come up with a better&amp;nbsp;answer.&lt;/p&gt;
&lt;h2&gt;Where thinking breaks down: insufficient&amp;nbsp;examples&lt;/h2&gt;
&lt;p&gt;When this trick was first discovered, early adopters experimented with different prompt patterns, trying to get models to generate longer responses that led to better answers. But thinking doesn&amp;#8217;t always succeed. We&amp;#8217;ve all had the experience of trying to think through some difficult math problem, writing lots of working that ultimately led&amp;nbsp;nowhere.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;GPT&lt;/span&gt;-3 may have been trained on a really large dataset, but most webpages and books are not showcases of how to solve difficult problems through clear&amp;nbsp;thinking.&lt;/p&gt;
&lt;p&gt;So it&amp;#8217;s back to supervised learning again. Look for examples of how to solve difficult problems. Recruit experts and have them write down their chain of thought for different kinds of problems. Then train the model on this labelled data, so that it doesn&amp;#8217;t require users to be clever with prompts to extract this thinking. Train the model to differentiate between requests for a quick answer, and requests requiring deeper&amp;nbsp;thinking.&lt;/p&gt;
&lt;h2&gt;Thinking vs.&amp;nbsp;planning&lt;/h2&gt;
&lt;p&gt;A model that is able to think longer and in a more disciplined way to produce a better answer is able to tackle harder questions. These are the models that were solving olympiad questions that humans struggled to&amp;nbsp;solve.&lt;/p&gt;
&lt;p&gt;But this isn&amp;#8217;t enough for another kind of challenge: long-horizon tasks that involve multiple tool calls, putting together information and feedback from multiple sources, maintaining task coherence and a consistent goal orientation throughout the process, and finally producing output in the correct&amp;nbsp;format.&lt;/p&gt;
&lt;p&gt;For example, filing tax returns involves digging through a large number of financial documents, remaining aware of legal requirements for filing, extracting relevant information, and putting it together following those requirements. None of the steps along the way involve extreme intelligence or genius insight, it&amp;#8217;s just a lot of tedious steps and details to keep track of. Along the way, detours and failed tool calls threaten to derail the model; it can get stuck researching an edge case rule, debugging a failing tool call, or get distracted by other&amp;nbsp;things.&lt;/p&gt;
&lt;p&gt;This requires the model to &lt;em&gt;plan&lt;/em&gt;. It has to take an end-goal, break it down into phases and steps, think about immediate steps, execute them and observe the result, decide next steps, repeat, &amp;#8230;. Along the way, it has to keep track of goals and sub-goals (usually aided by task management tools), be able to tell when they are met and check them off the&amp;nbsp;list.&lt;/p&gt;
&lt;p&gt;Books and websites seldom contain detailed worked examples of how to do this, so the model has to be trained with labelled data (again!), given examples of planning steps through supervised learning until it is able to reproduce them&amp;nbsp;reliably.&lt;/p&gt;
&lt;h2&gt;Hidden vs visible&amp;nbsp;thinking&lt;/h2&gt;
&lt;p&gt;Frontier labs found that showing the full thinking process to users isn&amp;#8217;t always beneficial. For example, the full thinking trace—tokens that constitute the analysis and are not part of the final answer—could be really lengthy. Users tend not to like that; they want to see the key steps for a quick check, and then the final&amp;nbsp;answer.&lt;/p&gt;
&lt;p&gt;Perhaps the full thinking trace includes mistakes the model made and corrected later, erroneous tool calls that it subsequently fixed, search tool calls which the user does not need to see the full contents of, etc. In other cases, frontier labs may have found ways for the model to output a more efficient form of thinking with tokens that is not&amp;nbsp;human-readable.&lt;/p&gt;
&lt;p&gt;This means one more step in the runtime: detecting and processing thinking tokens. If the model is trained to demarcate thinking tokens with a special start and end sequence,&amp;nbsp;e.g. &lt;code&gt;&amp;lt;thinking&amp;gt;...&amp;lt;/thinking&amp;gt;&lt;/code&gt;, the runtime may look for&amp;nbsp;it.&lt;/p&gt;
&lt;p&gt;Once detected, this hidden thinking may be removed, summarized (with a different model), or collapsed to take up less space in the user&amp;nbsp;interface.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Thinking/reasoning models are those that have been trained on examples of how to think about different problems in different domains, or plan and execute complex tasks. They often use tools to aid them in goal tracking and updating. The full thinking trace from the model may be removed or hidden to present a more legible response to the&amp;nbsp;user.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;This really is the primary concept behind thinking/reasoning models: more supervised training to output a sequence of tokens that lead the model to a useful&amp;nbsp;answer.&lt;/p&gt;
&lt;p&gt;If this sounds simple, that&amp;#8217;s because most of the magic is in the model training: crafting and labelling training examples, and then training the model on them, is a much more complicated process than it sounds, and I&amp;#8217;m excluding it from this issue because it is very technical and not suited for a newsletter named Layman&amp;#8217;s&amp;nbsp;Guide.&lt;/p&gt;
&lt;p&gt;Now you know what a model is doing when you activate a feature named &amp;#8220;Extended Thinking&amp;#8221;, or switch to a model that is described as a thinking/reasoning&amp;nbsp;model.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue179.html"&gt;Issue 179:&amp;nbsp;Agents&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Finally we can talk about this term, &amp;#8220;agents&amp;#8221;, and what differentiates them from a model. If you&amp;#8217;ve heard this term before and wondered what goes into one, subscribe to be notified when I lay it bare&amp;nbsp;;)&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 177: Multimodal models</title><link href="https://ngjunsiang.github.io/laymansguide/issue177.html" rel="alternate"></link><published>2026-07-27T08:00:00+08:00</published><updated>2026-07-27T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-07-27:/laymansguide/issue177.html</id><summary type="html">&lt;p&gt;Multimodal models represent text, image, and audio tokens alongside each other in their embedding space. The model uses the input tokens, regardless of type, to calculate the next output token. Multimodal models typically only output text tokens in their response, delegating to more specialized models for image and audio generation if&amp;nbsp;necessary.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; In retrieval-augmented generation (&lt;span class="caps"&gt;RAG&lt;/span&gt;), the runtime performs a search with the user&amp;#8217;s request to retrieve relevant chunks from a set of documents from a knowledge base. The chunks may be further re-ranked by the runtime before finally being included in the &lt;span class="caps"&gt;LLM&lt;/span&gt;&amp;#8217;s input. One alternative to &lt;span class="caps"&gt;RAG&lt;/span&gt;, where information lookup happens outside of &lt;span class="caps"&gt;LLM&lt;/span&gt; generation, is to provide the &lt;span class="caps"&gt;LLM&lt;/span&gt; with search tools instead, and rely on its judgement to use them&amp;nbsp;well.&lt;/p&gt;
&lt;p&gt;Multimodal models. Try saying that three times quickly. It&amp;#8217;s quite a mouthful, but if you&amp;#8217;ve managed to keep up so far, it&amp;#8217;s really not complicated, so I don&amp;#8217;t expect this to be a long&amp;nbsp;issue.&lt;/p&gt;
&lt;h2&gt;Multimodal&amp;nbsp;models&lt;/h2&gt;
&lt;p&gt;While a large language model works only with text tokens, a &lt;strong&gt;multimodal model&lt;/strong&gt; can work with other types of tokens as well. We&amp;#8217;ve previously covered what text tokens are and how LLMs use them (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue172.html"&gt;Issue 172&lt;/a&gt;), so let&amp;#8217;s focus on image and audio&amp;nbsp;tokens.&lt;/p&gt;
&lt;p&gt;The approach is similar, really: text gets broken up into common repeating patterns. Image and audio likewise gets broken up into common repeating patterns. Each common repeating pattern is represented by a number, or set of numbers, and located in an embedding space (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue172.html"&gt;Issue 172&lt;/a&gt;).&lt;/p&gt;
&lt;h2&gt;Image&amp;nbsp;tokens&lt;/h2&gt;
&lt;p&gt;There are a variety of approaches for tokenizing images. A common way to do this is to break it up into 16×16-pixel patches. Each pixel has three values representing red+green+blue (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue043.html"&gt;Issues 43&lt;/a&gt; &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue044.html"&gt;44&lt;/a&gt;), so each patch is a sequence of 16×16×3 = 768&amp;nbsp;values.&lt;/p&gt;
&lt;p&gt;Each unique combination of 768 values constitutes an image token. During training, these image tokens appear alongside other tokens (text, image, audio), and the model adjusts its embedding parameters to locate semantically similar tokens in close&amp;nbsp;proximity.&lt;/p&gt;
&lt;p&gt;During inference (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue173.html"&gt;Issue 173&lt;/a&gt;), hidden layers represent more abstract patterns that the model identifies: lower layers may encode information about edges, while higher layers capture information about shapes, textures, and even&amp;nbsp;objects.&lt;/p&gt;
&lt;h2&gt;Audio&amp;nbsp;tokens&lt;/h2&gt;
&lt;p&gt;While intuitively it seems natural to chunk audio into 1-second or even sub-second samples, in reality 1 second of audio contains 44,100 samples (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue045.html"&gt;Issue 45&lt;/a&gt;) which is still far too&amp;nbsp;large.&lt;/p&gt;
&lt;p&gt;Instead, audio is usually converted from waveform representation (amplitude vs time) into spectrum representation (frequency vs amplitude at a snapshot in time). The spectrogram gets split into shorter windows of a few milliseconds each (a few thousand samples per window). The values of each frequency in that window then naturally form an audio token, which appear alongside other tokens in training and get represented in embedding space the same way as other&amp;nbsp;tokens.&lt;/p&gt;
&lt;h2&gt;Multimodal models need supervised&amp;nbsp;training&lt;/h2&gt;
&lt;p&gt;Supervised learning plays a big part here. Images, audio, and text seldom appear together in unlabelled training data (except in video), so associating images and audio with text relies heavily on manual labelling. This is why multimodal models took so long to&amp;nbsp;emerge.&lt;/p&gt;
&lt;p&gt;During inference, all tokens regardless of type are represented as embeddings, and the model uses the input tokens to calculate the output&amp;nbsp;token.&lt;/p&gt;
&lt;h2&gt;Multimodal models vs image/audio generation&amp;nbsp;models&lt;/h2&gt;
&lt;p&gt;An app like ChatGPT can take user-uploaded image files, reference them in their response to the user, and then generate an image, or even convert the response from text to audio. But this seamlessness is an illusion; at the backend, these do not use the same&amp;nbsp;model.&lt;/p&gt;
&lt;p&gt;Multimodal models can take input tokens of multiple types, but typically only generate text in response; users do not expect image patches or audio snippets in the response, and would not know how to interpret&amp;nbsp;them.&lt;/p&gt;
&lt;p&gt;Instead, image and audio generation use different kinds of (non-Transformer) models, which might be worth exploring briefly in a future issue, but not this&amp;nbsp;one.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Multimodal models represent text, image, and audio tokens alongside each other in their embedding space. The model uses the input tokens, regardless of type, to calculate the next output token. Multimodal models typically only output text tokens in their response, delegating to more specialized models for image and audio generation if&amp;nbsp;necessary.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;There you go. Multimodal models demystified: once you figure out how to tokenize something alongside text, and give the model lots of labelled data to associate it with text tokens during training, you can create another modality for your model. This sentence hides months of complexity that &lt;span class="caps"&gt;AI&lt;/span&gt; labs tackle, because that&amp;#8217;s what you&amp;#8217;re reading Layman&amp;#8217;s Guide for, isn&amp;#8217;t&amp;nbsp;it?&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue178.html"&gt;Issue 178: Model thinking and&amp;nbsp;reasoning&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We&amp;#8217;ve covered retrieval-augmented generation (&lt;span class="caps"&gt;RAG&lt;/span&gt;), and now we&amp;#8217;ve covered multimodal models. Text, images, audio: Check check checked. Tools? You&amp;nbsp;bet.&lt;/p&gt;
&lt;p&gt;We&amp;#8217;ve got almost all the ingredients to assemble an &lt;span class="caps"&gt;AI&lt;/span&gt; to scare the economic labor pool, but we are still lacking one final piece of the puzzle: how do LLMs&amp;nbsp;&amp;#8220;think&amp;#8221;?&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 176: Retrieval-Augmented Generation (RAG)</title><link href="https://ngjunsiang.github.io/laymansguide/issue176.html" rel="alternate"></link><published>2026-07-20T08:00:00+08:00</published><updated>2026-07-20T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-07-20:/laymansguide/issue176.html</id><summary type="html">&lt;p&gt;In retrieval-augmented generation (&lt;span class="caps"&gt;RAG&lt;/span&gt;), the runtime performs a search with the user&amp;#8217;s request to retrieve relevant chunks from a set of documents from a knowledge base. The chunks may be further re-ranked by the runtime before finally being included in the &lt;span class="caps"&gt;LLM&lt;/span&gt;&amp;#8217;s input. One alternative to &lt;span class="caps"&gt;RAG&lt;/span&gt;, where information lookup happens outside of &lt;span class="caps"&gt;LLM&lt;/span&gt; generation, is to provide the &lt;span class="caps"&gt;LLM&lt;/span&gt; with search tools instead, and rely on its judgement to use them&amp;nbsp;well.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; LLMs can be trained to make tool calls, using the same training data used to train code assistants. The tool specifications are injected into the system prompt that is passed to the model, along with guidance on when to use a tool. Tool calls generated by the model are interpreted by a runtime that detects and executes them, then passes the results of the tool call back to the &lt;span class="caps"&gt;LLM&lt;/span&gt; in the next&amp;nbsp;input.&lt;/p&gt;
&lt;p&gt;We mentioned hallucination—mentioning non-existent publications, stories, or facts as though they were real—as one of the pitfalls of &lt;span class="caps"&gt;GPT&lt;/span&gt;-3, and mentioned how reinforcement learning with human feedback (&lt;span class="caps"&gt;RLHF&lt;/span&gt;) helps to combat some of these tendencies in general&amp;nbsp;use.&lt;/p&gt;
&lt;p&gt;These days, ChatGPT, Claude, and other chatbots also allow you to upload documents. The runtime supporting these chatbots helps to extract text from the documents with supporting context and include them in the system prompt, allowing the chatbot to answer from the document&amp;#8217;s contents to combat&amp;nbsp;hallucination.&lt;/p&gt;
&lt;p&gt;In some cases, the document may be too large. In other cases, a company may have a large set of documents the &lt;span class="caps"&gt;LLM&lt;/span&gt; should answer from, but they are too large to all be included in the system&amp;nbsp;prompt.&lt;/p&gt;
&lt;p&gt;In such cases, &lt;strong&gt;retrieval-augmented generation&lt;/strong&gt; (&lt;span class="caps"&gt;RAG&lt;/span&gt;) provides an alternative way to inject relevant information into the &lt;span class="caps"&gt;LLM&lt;/span&gt;&amp;#8217;s system&amp;nbsp;prompt.&lt;/p&gt;
&lt;h2&gt;Retrieval-Augmented Generation (&lt;span class="caps"&gt;RAG&lt;/span&gt;)&lt;/h2&gt;
&lt;p&gt;Like other &lt;span class="caps"&gt;LLM&lt;/span&gt; capabilities, this one comes from the runtime. The &lt;span class="caps"&gt;LLM&lt;/span&gt; plays no part in this and has no control over the&amp;nbsp;process.&lt;/p&gt;
&lt;p&gt;The source documents are chunked, and each chunk analyzed to create an embedding. Parts of the document that are closely related have embeddings located more&amp;nbsp;closely.&lt;/p&gt;
&lt;p&gt;Before the user&amp;#8217;s input is passed to the &lt;span class="caps"&gt;LLM&lt;/span&gt;, it is parsed by the runtime and analyzed into an embedding. This embedding is used to retrieve relevant parts of documents; other information may be used to determine relevant portions as&amp;nbsp;well.&lt;/p&gt;
&lt;p&gt;Instead of embedding entire documents, only these relevant portions are included in the system prompt for the &lt;span class="caps"&gt;LLM&lt;/span&gt; to answer the user&amp;#8217;s query. In more advanced implementations, the chunks may be further re-ranked by importance and other&amp;nbsp;criteria.&lt;/p&gt;
&lt;p&gt;All of this happens in the runtime, beyond the &lt;span class="caps"&gt;LLM&lt;/span&gt;&amp;#8217;s token generation&amp;nbsp;loop.&lt;/p&gt;
&lt;h2&gt;Limitations&lt;/h2&gt;
&lt;p&gt;When it works well, it works really well: the &lt;span class="caps"&gt;LLM&lt;/span&gt; doesn&amp;#8217;t hallucinate, quotes from the source, and if the source is well-tagged, it can even cite from the correct page and&amp;nbsp;paragraph.&lt;/p&gt;
&lt;p&gt;But there are ways it can make mistakes too. If no matching documents are found and the &lt;span class="caps"&gt;LLM&lt;/span&gt; isn&amp;#8217;t aware, it may hallucinate unless the runtime handles this well. On the opposite end of the spectrum, it may find too many results and not know how to select the most relevant ones. The documents themselves may be contradictory, incomplete, or require too much unwritten context. And lastly, it may miss important nuance found elsewhere in the document, or in other documents, that did not surface in the embedding&amp;nbsp;search.&lt;/p&gt;
&lt;h2&gt;Alternatives&lt;/h2&gt;
&lt;p&gt;Still, in cases where you can&amp;#8217;t fit entire source documents in the &lt;span class="caps"&gt;LLM&lt;/span&gt; context, what other alternatives do you&amp;nbsp;have?&lt;/p&gt;
&lt;p&gt;Then it&amp;#8217;s back to a set of tools for your &lt;span class="caps"&gt;LLM&lt;/span&gt; to use for searching the company knowledge base, read documents, and manually extract relevant portions. Naturally, your &lt;span class="caps"&gt;LLM&lt;/span&gt; will need to be trained on a dataset of positive examples of tool usage (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue175.html"&gt;Issue 175&lt;/a&gt;). In contrast to &lt;span class="caps"&gt;RAG&lt;/span&gt;, where retrieval is automatic and built into the runtime, here you are relying on the &lt;span class="caps"&gt;LLM&lt;/span&gt;&amp;#8217;s judgement of which tool to use, and when to use&amp;nbsp;it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; In retrieval-augmented generation (&lt;span class="caps"&gt;RAG&lt;/span&gt;), the runtime performs a search with the user&amp;#8217;s request to retrieve relevant chunks from a set of documents from a knowledge base. The chunks may be further re-ranked by the runtime before finally being included in the &lt;span class="caps"&gt;LLM&lt;/span&gt;&amp;#8217;s input. One alternative to &lt;span class="caps"&gt;RAG&lt;/span&gt;, where information lookup happens outside of &lt;span class="caps"&gt;LLM&lt;/span&gt; generation, is to provide the &lt;span class="caps"&gt;LLM&lt;/span&gt; with search tools instead, and rely on its judgement to use them&amp;nbsp;well.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Okay, that&amp;#8217;s &lt;span class="caps"&gt;RAG&lt;/span&gt; de-mystified. It&amp;#8217;s a program that runs a search on the user&amp;#8217;s request and injects relevant chunks from the knowledge base into the &lt;span class="caps"&gt;LLM&lt;/span&gt;&amp;#8217;s input, beyond the &lt;span class="caps"&gt;LLM&lt;/span&gt;&amp;#8217;s control. Now you can speak about &lt;span class="caps"&gt;RAG&lt;/span&gt; a little more&amp;nbsp;informatively.&lt;/p&gt;
&lt;p&gt;I avoided discussing &lt;span class="caps"&gt;RAG&lt;/span&gt;&amp;#8217;s performance, because results vary. For every detractor you can also find a supporter! Is it going to work well for you? You probably have to try it yourself, or find a consultant who can better advise&amp;nbsp;you.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue177.html"&gt;Issue 177: Multimodal&amp;nbsp;models&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Many chatbot models accept image and even audio alongside text. How does this work? De-mystifying in the next issue, so stay&amp;nbsp;tuned!&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 175: LLM tools</title><link href="https://ngjunsiang.github.io/laymansguide/issue175.html" rel="alternate"></link><published>2026-07-13T08:00:00+08:00</published><updated>2026-07-13T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-07-13:/laymansguide/issue175.html</id><summary type="html">&lt;p&gt;LLMs can be trained to make tool calls, using the same training data used to train code assistants. The tool specifications are injected into the system prompt that is passed to the model, along with guidance on when to use a tool. Tool calls generated by the model are interpreted by a runtime that detects and executes them, then passes the results of the tool call back to the &lt;span class="caps"&gt;LLM&lt;/span&gt; in the next&amp;nbsp;input.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Through reinforcement learning with human feedback (&lt;span class="caps"&gt;RLHF&lt;/span&gt;), the &lt;span class="caps"&gt;LLM&lt;/span&gt; is trained on labelled data until it can reliably follow instructions, avoid harmful output, and follow other desired behavior. A system prompt provides guidelines for output. The user&amp;#8217;s prompt is inserted into a templated prompt and passed to the &lt;span class="caps"&gt;LLM&lt;/span&gt;, which generates text in a markup format that a display system can understand. A chat interface wraps the entire system to create the illusion of a responsive&amp;nbsp;chatbot.&lt;/p&gt;
&lt;p&gt;A chatbot is fun to use for a while, but if all it could do was talk we wouldn&amp;#8217;t use it for very long. For starters, it would hallucinate a lot, or give outdated information, because it couldn&amp;#8217;t access the internet or do a web search. What would it take for models to be able to use computers to do&amp;nbsp;that?&lt;/p&gt;
&lt;p&gt;While this problem was being actively worked on, LLMs were also being trained to generate programming code. It turned out that code, being text-based, was fertile training ground for LLMs. They were improving at it too; while early versions still failed at producing large yet coherent programs, many were able to generate boilerplate code with correct syntax&amp;nbsp;already.&lt;/p&gt;
&lt;h2&gt;LLMs as tool-using&amp;nbsp;models&lt;/h2&gt;
&lt;p&gt;For a &lt;span class="caps"&gt;LLM&lt;/span&gt; to use a tool, it needs to be trained&amp;nbsp;to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;state the tool to&amp;nbsp;use&lt;/li&gt;
&lt;li&gt;pass the appropriate&amp;nbsp;options&lt;/li&gt;
&lt;li&gt;interpret the result, when passed back to the &lt;span class="caps"&gt;LLM&lt;/span&gt; (in the next&amp;nbsp;request)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;for example, to use a weather tool, the &lt;span class="caps"&gt;LLM&lt;/span&gt; needs to be able&amp;nbsp;to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;say &amp;#8220;use the weather&amp;nbsp;tool&amp;#8221;&lt;/li&gt;
&lt;li&gt;pass options: &amp;#8220;location: Singapore, &lt;span class="caps"&gt;SG&lt;/span&gt;, show me the temperature and humidity as&amp;nbsp;well&amp;#8221;&lt;/li&gt;
&lt;li&gt;interpret the result: mostly self-explanatory, but e.g. it may need to understand if the location provided in the output may be the nearest known location and not the user&amp;#8217;s actual&amp;nbsp;location&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It turns out that the first two problems are already known and solved: the first text-based interfaces were invented in the 1970s after all, and programmers have always needed a way to invoke programs through a text-based interface. They already had one, in the form of the command line (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue015.html"&gt;Issue 15&lt;/a&gt;) and the rich syntax that was already built around it. And they had another, in the form of the function call syntax that almost all programming languages had standardized on,&amp;nbsp;like &lt;code&gt;check_weather(location="Singapore, SG", show_temperature=True, show_humidity=True)&lt;/code&gt;. And training data already existed for both of these, in the form of open-source code readily available online in code repositories (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue019.html"&gt;Issue 19&lt;/a&gt;).&lt;/p&gt;
&lt;h2&gt;The structure of a tool-using &lt;span class="caps"&gt;LLM&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;For a &lt;span class="caps"&gt;LLM&lt;/span&gt; to be able to output tool calls, you&amp;nbsp;need:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;tool specifications, usually injected through the system prompt, telling the &lt;span class="caps"&gt;LLM&lt;/span&gt; the available tools and their&amp;nbsp;options&lt;/li&gt;
&lt;li&gt;guidance on when to use each tool, typically through further instructions in the system prompt, through &lt;span class="caps"&gt;RLHF&lt;/span&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue174.html"&gt;Issue 174&lt;/a&gt;), or&amp;nbsp;both&lt;/li&gt;
&lt;li&gt;familiarity with the tool call syntax used, typically trained into the model through &lt;span class="caps"&gt;RLHF&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the system prompt, you would&amp;nbsp;include:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;[...]&lt;/span&gt;

&lt;span class="c1"&gt;## Tools available&lt;/span&gt;

&lt;span class="na"&gt;- `check_weather(location&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;text, show_temperature: boolean, show_humidity: boolean)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="na"&gt;Check the weather at the given location. Example&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Singapore, SG&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="na"&gt;Pass show_temperature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;True and show_humidity=True if temperature and humidity are required in the output&lt;/span&gt;
&lt;span class="na"&gt;- ...&lt;/span&gt;
&lt;span class="na"&gt;- ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Providing a rich set of tools without using up too many tokens is a tricky design balance that requires regular tweaking. In any case, the model is then trained to output the tool calls in a specially marked section of their&amp;nbsp;output.&lt;/p&gt;
&lt;h2&gt;Invoking the&amp;nbsp;tools&lt;/h2&gt;
&lt;p&gt;At the point when the model outputs the stop token and the program stops using it to calculate more output tokens, its involvement stops. The program interprets the model&amp;#8217;s output, separating the tool calls out, and passes them to another&amp;nbsp;system.&lt;/p&gt;
&lt;p&gt;You see, tool calls can be pretty dangerous, especially if they enable the model to carry out destructive actions. A shell command&amp;nbsp;like &lt;code&gt;rm -rf /&lt;/code&gt; on Linux or Mac could delete the entire operating system, or important subdirectories.&amp;nbsp;A &lt;code&gt;delete_database&lt;/code&gt; tool could do what it says, but with the wrong target specified. So it&amp;#8217;s common to have a system that examines the tool call and attempts to determine if it is safe. In a code assistant, this tool call might be shown to the user for explicit approval. In a web-based chatbot like ChatGPT, tool safety is usually handled by another system&amp;nbsp;instead.&lt;/p&gt;
&lt;p&gt;Once validated, the tool needs to be &lt;em&gt;executed&lt;/em&gt; on a computer system. This computer system needs to have the necessary programs installed. It should also be isolated against potentially destructive actions. We&amp;#8217;ve covered how containerization (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue149.html"&gt;Issue 149&lt;/a&gt;) enables this to be done; an isolated container for each session where&amp;nbsp;necessary.&lt;/p&gt;
&lt;p&gt;Finally, the result of the tool call, whether success or failure, is captured and then added to the token sequence which is fed back into the &lt;span class="caps"&gt;LLM&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;This all sounds pretty neat, but with one caveat: only the chatbot provider (OpenAI for ChatGPT, or Anthropic for Claude) can pass these tools to the &lt;span class="caps"&gt;LLM&lt;/span&gt;. Third-party integrations, such as with GitHub or Google Drive, would be tricky for OpenAI/Anthropic to design on their own, yet unsafe for external parties to inject into the system&amp;nbsp;prompt.&lt;/p&gt;
&lt;h2&gt;Integrating third-party&amp;nbsp;tools&lt;/h2&gt;
&lt;p&gt;So in Nov 2024, Anthropic proposed another standard: the &lt;a href="https://modelcontextprotocol.io/docs/getting-started/intro"&gt;Model Context Protocol&lt;/a&gt;, a way for external parties to specify a set of tools that work together to enable access to other web-based or software-based&amp;nbsp;systems.&lt;/p&gt;
&lt;p&gt;When the user registers a &lt;span class="caps"&gt;MCP&lt;/span&gt; server through a graphical or text-based interface, the system reads the tool specifications from the &lt;span class="caps"&gt;MCP&lt;/span&gt; server, injects them into the system prompt, and from there they work like other tools accessible to the &lt;span class="caps"&gt;LLM&lt;/span&gt;.&lt;/p&gt;
&lt;h2&gt;The&amp;nbsp;runtime&lt;/h2&gt;
&lt;p&gt;Notice that none of this is mediated or controlled by the &lt;span class="caps"&gt;LLM&lt;/span&gt;. It follows instructions, generates tool calls with the correct syntax in its output, then sees the result in the next input, seemingly by magic. The &lt;span class="caps"&gt;LLM&lt;/span&gt; is operating in a virtualized environment controlled by an external system that doesn&amp;#8217;t have a standardized name yet. For now we&amp;#8217;ll call it the &lt;strong&gt;runtime&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Tools and toolsets make or break a &lt;span class="caps"&gt;LLM&lt;/span&gt;-based assistant. They are the only way a &lt;span class="caps"&gt;LLM&lt;/span&gt; can take actions, get data, and otherwise make sense of the external world. A &lt;span class="caps"&gt;LLM&lt;/span&gt; without any tools is analogous to a human in a sensory deprivation tank—without information from the outside world, even &lt;a href="https://en.wikipedia.org/wiki/Sensory_deprivation"&gt;human beings quickly begin to hallucinate&lt;/a&gt;.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; LLMs can be trained to make tool calls, using the same training data used to train code assistants. The tool specifications are injected into the system prompt that is passed to the model, along with guidance on when to use a tool. Tool calls generated by the model are interpreted by a runtime that detects and executes them, then passes the results of the tool call back to the &lt;span class="caps"&gt;LLM&lt;/span&gt; in the next&amp;nbsp;input.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;From here it&amp;#8217;s another 3 issues before we get to the topic of the year: &lt;span class="caps"&gt;AI&lt;/span&gt; agents. Before I get there I want to cover three more buzzphrases: &lt;strong&gt;retrieval-augmented generation&lt;/strong&gt; (&lt;span class="caps"&gt;RAG&lt;/span&gt;), &lt;strong&gt;multimodal&lt;/strong&gt; models, and &lt;strong&gt;reasoning/thinking&lt;/strong&gt;&amp;nbsp;models.&lt;/p&gt;
&lt;p&gt;By now I hope you&amp;#8217;re starting to see that LLMs really are next-token predictors underneath, and all their actual capabilities—the ones that let them know what is happening in real-time and change things in the world—are provided through the runtime. As the runtime grows more powerful and capable, LLMs must also be post-trained (using reinforcement learning a.k.a. &lt;span class="caps"&gt;RLHF&lt;/span&gt;) to use them&amp;nbsp;well.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue176.html"&gt;Issue 176: Retrieval-Augmented Generation (&lt;span class="caps"&gt;RAG&lt;/span&gt;)&lt;/a&gt;&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 174: Reinforcement Learning</title><link href="https://ngjunsiang.github.io/laymansguide/issue174.html" rel="alternate"></link><published>2026-07-06T08:00:00+08:00</published><updated>2026-07-06T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-07-06:/laymansguide/issue174.html</id><summary type="html">&lt;p&gt;Through reinforcement learning with human feedback (&lt;span class="caps"&gt;RLHF&lt;/span&gt;), the &lt;span class="caps"&gt;LLM&lt;/span&gt; is trained on labelled data until it can reliably follow instructions, avoid harmful output, and follow other desired behavior. A system prompt provides guidelines for output. The user&amp;#8217;s prompt is inserted into a templated prompt and passed to the &lt;span class="caps"&gt;LLM&lt;/span&gt;, which generates text in a markup format that a display system can understand. A chat interface wraps the entire system to create the illusion of a responsive&amp;nbsp;chatbot.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; OpenAI discovered, through models &lt;span class="caps"&gt;GPT&lt;/span&gt;-1 to &lt;span class="caps"&gt;GPT&lt;/span&gt;-3, that scaling compute and (training) data &lt;em&gt;alone&lt;/em&gt; was sufficient to sharply increase the capabilities of a &lt;span class="caps"&gt;LLM&lt;/span&gt;: the transformer architecture and unsupervised learning together resulted in a model that was alarmingly&amp;nbsp;intelligent.&lt;/p&gt;
&lt;p&gt;Mechanistically, a &lt;span class="caps"&gt;LLM&lt;/span&gt; is a next-token predictor: from a set of parameters, and an input sequence of tokens, a program continually calculates the next token, which gets appended to the input sequence, and the new sequence gets fed in as the input again, until a stop token is&amp;nbsp;generated.&lt;/p&gt;
&lt;p&gt;OpenAI had discovered that by training &lt;span class="caps"&gt;GPT&lt;/span&gt;-3 (with over a hundred billion parameters) on a very large dataset (hundreds of billions of tokens), they ended up with a next-token predictor that appeared to generate readable, sensible&amp;nbsp;text.&lt;/p&gt;
&lt;p&gt;But that doesn&amp;#8217;t mean that &lt;span class="caps"&gt;GPT&lt;/span&gt;-3 was ready for public use yet: what about those hallucinations, that toxic output, the prompt injections that caused it to ignore OpenAI&amp;#8217;s&amp;nbsp;instructions?&lt;/p&gt;
&lt;h2&gt;Reinforcement&amp;nbsp;learning&lt;/h2&gt;
&lt;p&gt;Unsupervised learning may have created a genius model, but now OpenAI had to fall back on supervised learning to make it&amp;nbsp;useful.&lt;/p&gt;
&lt;p&gt;In 2022, OpenAI researchers submitted a paper titled &lt;a href="https://arxiv.org/abs/2203.02155"&gt;&amp;#8220;Training language models to follow instructions with human feedback&amp;#8221;&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Starting with a set of labeler-written prompts and prompts submitted through the OpenAI &lt;span class="caps"&gt;API&lt;/span&gt;, we collect a dataset of labeler demonstrations of the desired model behavior, which we use to fine-tune &lt;span class="caps"&gt;GPT&lt;/span&gt;-3 using supervised learning. We then collect a dataset of rankings of model outputs, which we use to further fine-tune this supervised model using reinforcement learning from human feedback. We call the resulting models&amp;nbsp;InstructGPT.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It was back to painstaking human labelling of data again, getting humans to write desired outputs and label toxic content to train the model on. Through this process of &lt;strong&gt;reinforcement learning with human feedback&lt;/strong&gt; (&lt;span class="caps"&gt;RLHF&lt;/span&gt;), InstructGPT was&amp;nbsp;born.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;RLHF&lt;/span&gt; was necessary to adjust the model parameters so that instructions like &amp;#8220;explain &amp;#8230;&amp;#8221; were treated as guiding instructions rather than starting text that the model would steer away&amp;nbsp;from.&lt;/p&gt;
&lt;h2&gt;Data cleaning and&amp;nbsp;labelling&lt;/h2&gt;
&lt;p&gt;Prompt injections would continue to be an issue, but in the meantime OpenAI could address toxic content by first cleaning up the dataset to remove toxic, low-quality content and add other high-quality data&amp;nbsp;sources.&lt;/p&gt;
&lt;p&gt;This need for new, novel data sources still drives frontier machine learning labs today, who pay for high-quality data sources they can use to train their&amp;nbsp;models.&lt;/p&gt;
&lt;h2&gt;Creating a chat&amp;nbsp;assistant&lt;/h2&gt;
&lt;p&gt;InstructGPT was ready to take instructions. But &amp;#8230; how do we get instructions from the user? How do we pass the responses back to them? The model was trained, the &lt;span class="caps"&gt;API&lt;/span&gt; was ready &amp;#8230; but OpenAI needed a graphical interface, a familiar mental model of interaction that the public could use&amp;nbsp;intuitively.&lt;/p&gt;
&lt;p&gt;One already existed: chat apps like WhatsApp were popular at the time, and users intuitively understood a chat input when they saw one. But how could OpenAI get InstructGPT to respond reliably like a chat assistant with a consistent personality and&amp;nbsp;style?&lt;/p&gt;
&lt;p&gt;It turned out the answer was already in the training&amp;nbsp;data.&lt;/p&gt;
&lt;h2&gt;Prompt&amp;nbsp;framing&lt;/h2&gt;
&lt;p&gt;There was a lot of training data in the form of interviews, movie scripts, things that look&amp;nbsp;like:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Alice: Why do cats like to jump on furniture?&lt;br /&gt;
Bob:&amp;nbsp;&amp;#8230;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And in many cases, arranging the user&amp;#8217;s question along with a system prompt like so was enough to have the &lt;span class="caps"&gt;LLM&lt;/span&gt; roleplay a helpful&amp;nbsp;assistant:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;# System Prompt

You are ChatGPT, a large language model trained by OpenAI. [...]
Knowledge cutoff: 2024-06
Current date: 2025-09-03

Personality: Engage warmly yet honestly with the user. [...]

User: &amp;lt;user&amp;#39;s input&amp;gt;
Assistant: 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Pass the above prompt to InstructGPT and it helpfully follows the pattern, demonstrating &lt;span class="caps"&gt;GPT&lt;/span&gt;-3&amp;#8217;s capabilities token after token, until it reaches a stop token. The program then takes the tokens generated after the prompt and displays them to the&amp;nbsp;user.&lt;/p&gt;
&lt;p&gt;What if the output is toxic, hallucinatory, or otherwise unacceptable? Back to &lt;span class="caps"&gt;RLHF&lt;/span&gt;&amp;nbsp;again.&lt;/p&gt;
&lt;h2&gt;The ChatGPT&amp;nbsp;wrapper&lt;/h2&gt;
&lt;p&gt;Even with the &lt;span class="caps"&gt;API&lt;/span&gt; in place, some window dressing is still needed. The &lt;span class="caps"&gt;LLM&lt;/span&gt;, being a language model, can only generate text, not format it. Most LLMs are &lt;span class="caps"&gt;RLHF&lt;/span&gt;-trained to generate text in a markup format (such as &lt;span class="caps"&gt;HTML&lt;/span&gt; or Markdown). The display system takes the &lt;span class="caps"&gt;LLM&lt;/span&gt;&amp;#8217;s output, interprets the markup, and displays it as something the user can understand, making headers bold and larger, adding bullets or numbers to lists, formatting code accordingly, and so&amp;nbsp;on.&lt;/p&gt;
&lt;p&gt;The wrapper can also do some helpful things, like filter the &lt;span class="caps"&gt;LLM&lt;/span&gt;&amp;#8217;s output for harmful text and block it from appearing, as a kind of last-layer defence against offensive output. Add a login screen, a way for users to access past chats, a few other niceties&amp;nbsp;&amp;#8230;&lt;/p&gt;
&lt;p&gt;Finally, &lt;a href="https://openai.com/index/chatgpt/"&gt;OpenAI launched ChatGPT in November 2022&lt;/a&gt;. And the world as we knew it changed&amp;nbsp;forever.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Through reinforcement learning with human feedback (&lt;span class="caps"&gt;RLHF&lt;/span&gt;), the &lt;span class="caps"&gt;LLM&lt;/span&gt; is trained on labelled data until it can reliably follow instructions, avoid harmful output, and follow other desired behavior. A system prompt provides guidelines for output. The user&amp;#8217;s prompt is inserted into a templated prompt and passed to the &lt;span class="caps"&gt;LLM&lt;/span&gt;, which generates text in a markup format that a display system can understand. A chat interface wraps the entire system to create the illusion of a responsive&amp;nbsp;chatbot.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;ChatGPT was the beginning of many other features to follow. Among them: multimodal models, and tool calls. The former is easy to understand, so let&amp;#8217;s unpack how &lt;span class="caps"&gt;LLM&lt;/span&gt; tools work in the next&amp;nbsp;issue.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue175.html"&gt;Issue 175: &lt;span class="caps"&gt;LLM&lt;/span&gt;&amp;nbsp;tools&lt;/a&gt;&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 173: Training, Inference, and Scaling</title><link href="https://ngjunsiang.github.io/laymansguide/issue173.html" rel="alternate"></link><published>2026-06-29T08:00:00+08:00</published><updated>2026-06-29T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-06-29:/laymansguide/issue173.html</id><summary type="html">&lt;p&gt;OpenAI discovered, through models &lt;span class="caps"&gt;GPT&lt;/span&gt;-1 to &lt;span class="caps"&gt;GPT&lt;/span&gt;-3, that scaling compute and (training) data &lt;em&gt;alone&lt;/em&gt; was sufficient to sharply increase the capabilities of a &lt;span class="caps"&gt;LLM&lt;/span&gt;: the transformer architecture and unsupervised learning together resulted in a model that was alarmingly&amp;nbsp;intelligent.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; A model does not see letters or words, only tokens. These tokens are typically generated from user input through a pre-tokenizer program. Tokens are represented in the model as embeddings, a sequence of numbers representing the token&amp;#8217;s position in the embedding matrix. The model uses each token&amp;#8217;s embedding, and its surrounding tokens, to infer its meaning in&amp;nbsp;context.&lt;/p&gt;
&lt;h2&gt;Model&amp;nbsp;Training&lt;/h2&gt;
&lt;p&gt;In issue 171, I explained a little about how model training&amp;nbsp;happens:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;we pass tokens generated from text to the&amp;nbsp;input&lt;/li&gt;
&lt;li&gt;we pass the expected output (in supervised training), or the subsequent tokens (in unsupervised&amp;nbsp;training)&lt;/li&gt;
&lt;li&gt;the model generates output from&amp;nbsp;input&lt;/li&gt;
&lt;li&gt;we compare the model&amp;#8217;s output to the expected&amp;nbsp;output&lt;/li&gt;
&lt;li&gt;we adjust model&amp;nbsp;parameters&lt;/li&gt;
&lt;li&gt;we repeat from step 3, attempting to adjust parameters to have the model generate output that is closer to the expected&amp;nbsp;output&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Notice that there&amp;#8217;s a &amp;#8220;forward&amp;#8221; step: step 3, where the input &amp;#8220;feeds forward&amp;#8221; to each hidden layer. Here, the computer calculates the values for the next layer based on the values of the previous layer and on the model&amp;#8217;s parameters between the two layers. This is repeated for each layer until we get to the&amp;nbsp;output.&lt;/p&gt;
&lt;p&gt;Notice also that there&amp;#8217;s a &amp;#8220;backward&amp;#8221; step: step 5, where we could adjust model parameters randomly—inefficient! Instead, the mathematical technique of gradient descent gives us a more optimized way to adjust the last hidden layer based on how it would affect the output. The second-to-last hidden layer is then adjusted with the same technique, based on how it would affect the last hidden layer. And this is repeated all the way to the first hidden layer. This &amp;#8220;backward trickling&amp;#8221; is called &lt;strong&gt;backpropagation&lt;/strong&gt;, or &amp;#8220;backprop&amp;#8221; more&amp;nbsp;informally.&lt;/p&gt;
&lt;p&gt;The above steps are repeated &lt;em&gt;for each input:output data pair&lt;/em&gt; (supervised training) or &lt;em&gt;for each token sequence run&lt;/em&gt; (unsupervised training). That&amp;#8217;s &lt;strong&gt;a lot&lt;/strong&gt; of repeated steps; researchers often have some shortcuts they take to speed up the process. Even then, it is still too many for a typical &lt;span class="caps"&gt;CPU&lt;/span&gt; to complete in a reasonable time; the big labs use specialized GPUs instead (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue123.html"&gt;Issue 123&lt;/a&gt;), resulting in training runs that take weeks to months to complete on multiple GPUs for today&amp;#8217;s state-of-the-art&amp;nbsp;LLMs.&lt;/p&gt;
&lt;p&gt;This is not a cheap&amp;nbsp;hobby.&lt;/p&gt;
&lt;h2&gt;Inference&lt;/h2&gt;
&lt;p&gt;Fortunately, using a model is a different affair, involving only steps 1 and 3 of the above. No backpropagation, no repeated runs. Just pass the input in, run one forward step per output token, repeat until done. This process is called &lt;strong&gt;inference&lt;/strong&gt;, and is what happens when we users send a request to ChatGPT or&amp;nbsp;Claude.&lt;/p&gt;
&lt;p&gt;(Hang on, how does a model &amp;#8220;know&amp;#8221; when it is &amp;#8220;done generating text&amp;#8221;? In model training, a special token,&amp;nbsp;e.g. &lt;code&gt;&amp;lt;EOS&amp;gt;&lt;/code&gt; for end-of-sequence, is inserted at the end of text. When this token is detected in the program, it stops invoking the&amp;nbsp;model.)&lt;/p&gt;
&lt;p&gt;Needless to say, inference is much cheaper than training, which is why we are able to enjoy many of these models for&amp;nbsp;free.&lt;/p&gt;
&lt;h2&gt;Scaling up to &lt;span class="caps"&gt;GPT&lt;/span&gt;-2&lt;/h2&gt;
&lt;p&gt;&lt;span class="caps"&gt;GPT&lt;/span&gt;-1 had 117 million parameters, was trained on ~7,000 books (about &lt;span class="caps"&gt;5GB&lt;/span&gt;), took a few days to complete training on 8 GPUs, costing $0.5 mil or&amp;nbsp;less.&lt;/p&gt;
&lt;p&gt;In Nov 2019, &lt;a href="https://openai.com/index/gpt-2-1-5b-release/"&gt;OpenAI released &lt;span class="caps"&gt;GPT&lt;/span&gt;-2&lt;/a&gt;, which was the first large language model to capture some public attention. &lt;span class="caps"&gt;GPT&lt;/span&gt;-2 had 1.5 billion parameters (1.5B), was trained on ~&lt;span class="caps"&gt;40GB&lt;/span&gt; of text from the web, and took a few weeks to train on hundreds of GPUs, costing OpenAI $1 mil to $5 mil to&amp;nbsp;train.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;GPT&lt;/span&gt;-2 was the same architecture that &lt;span class="caps"&gt;GPT&lt;/span&gt;-1 used, only with a larger model (tenfold) and with more training data (eightfold). What they got was a model&amp;nbsp;that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;could perform tasks it was never explicitly trained on (zero-shot learning): answer questions, understand text, summarize, translate&amp;nbsp;(rudimentarily)&lt;/li&gt;
&lt;li&gt;could generalize from examples given in user input (one-shot/few-shot learning) without needing supervised&amp;nbsp;learning&lt;/li&gt;
&lt;li&gt;showed emerging ability on non-language tasks: counting, basic arithmetic, even some attempts at simple&amp;nbsp;proofs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These are capabilities we take for granted today, but in early 2019 this was cutting-edge performance never demonstrated by any other machine learning model, and certainly not with so little human supervision. This discovery was scary enough that it took OpenAI nine months to fully release &lt;span class="caps"&gt;GPT&lt;/span&gt;-2&amp;#8217;s weights, fearing how its capabilities might be misused. The Verge reported: &amp;#8220;&lt;a href="https://www.theverge.com/2019/11/7/20953040/openai-text-generation-ai-gpt-2-full-model-release-1-5b-parameters"&gt;OpenAI has published the text-generating &lt;span class="caps"&gt;AI&lt;/span&gt; it said was too dangerous to share&lt;/a&gt;&amp;#8221;, but fortunately in the same article &amp;#8220;the lab says it&amp;#8217;s seen &amp;#8216;no strong evidence of misuse so&amp;nbsp;far&amp;#8217;&amp;#8221;.&lt;/p&gt;
&lt;h2&gt;The bitter lesson, and &lt;span class="caps"&gt;GPT&lt;/span&gt;-3&lt;/h2&gt;
&lt;p&gt;These findings prompted Rich Sutton, an influential machine learning researcher, to write &lt;a href="http://www.incompleteideas.net/IncIdeas/BitterLesson.html"&gt;a blog post published on 13 March 2019&lt;/a&gt; where he summed up this finding in a single sentence: &amp;#8220;The bitter lesson is that general methods that leverage computation are ultimately the most effective, and by a large margin.&amp;#8221; Elaborating, he adds &amp;#8220;seeking an improvement that makes a difference in the shorter term, researchers seek to leverage their human knowledge of the domain, but the only thing that matters in the long run is the leveraging of&amp;nbsp;computation.&amp;#8221;&lt;/p&gt;
&lt;p&gt;A tenfold increase in model parameters and training data led to a surprising leap in capability. OpenAI and other researchers wondered: What if we pushed this to its logical conclusion, and threw more compute and more data into machine learning&amp;nbsp;training?&lt;/p&gt;
&lt;p&gt;In Jun 2020, &lt;a href="https://web.archive.org/web/20200611150951/https://openai.com/blog/openai-api/"&gt;OpenAI released &lt;span class="caps"&gt;GPT&lt;/span&gt;-3&lt;/a&gt;, available through their web &lt;span class="caps"&gt;API&lt;/span&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue004.html"&gt;Issue 4&lt;/a&gt;). &lt;span class="caps"&gt;GPT&lt;/span&gt;-3 had 175 billion parameters (175B, a hundredfold increase in model size), was trained on a mix of books and websites totalling 300 billion tokens, took weeks to train on hundreds of GPUs, and cost OpenAI up to $12 mil to&amp;nbsp;train.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;GPT&lt;/span&gt;-3&amp;nbsp;could:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;take instructions given in natural&amp;nbsp;language&lt;/li&gt;
&lt;li&gt;&lt;em&gt;reliably&lt;/em&gt; tackle many tasks zero-shot (with no&amp;nbsp;examples)&lt;/li&gt;
&lt;li&gt;&lt;em&gt;reliably&lt;/em&gt; adapt examples given in the user input, and generalize from&amp;nbsp;patterns&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It had reached a level of capability that took the focus away from &lt;em&gt;training data&lt;/em&gt; and placed it on the user input, called the &lt;strong&gt;prompt&lt;/strong&gt;: without further training, the model could give you a response, the quality of which depended on the quality of your&amp;nbsp;prompt.&lt;/p&gt;
&lt;h2&gt;Alarming&amp;nbsp;behavior&lt;/h2&gt;
&lt;p&gt;LLMs had finally reached a point where they were easy enough to use by the general public. But before it could actually launch for public use, there were some concerns to be&amp;nbsp;addressed.&lt;/p&gt;
&lt;p&gt;For one, &lt;span class="caps"&gt;GPT&lt;/span&gt;-3 was extremely prone to hallucinations—making up things that never happened, papers that were never written, academic journals that never existed. It also readily reproduced toxic outputs from its data source—the internet (especially reddit and 4chan). It was extremely steerable through the prompt—a little too steerable for OpenAI&amp;#8217;s liking, when some users got &lt;span class="caps"&gt;GPT&lt;/span&gt;-3 to leak its system prompt—the instructions that OpenAI prepended to every request guiding &lt;span class="caps"&gt;GPT&lt;/span&gt;-3&amp;#8217;s response style and&amp;nbsp;guardrails.&lt;/p&gt;
&lt;p&gt;It would be some time before ChatGPT could even launch without dragging OpenAI down with&amp;nbsp;it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; OpenAI discovered, through models &lt;span class="caps"&gt;GPT&lt;/span&gt;-1 to &lt;span class="caps"&gt;GPT&lt;/span&gt;-3, that scaling compute and (training) data &lt;em&gt;alone&lt;/em&gt; was sufficient to sharply increase the capabilities of a &lt;span class="caps"&gt;LLM&lt;/span&gt;: the transformer architecture and unsupervised learning together resulted in a model that was alarmingly&amp;nbsp;intelligent.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;We are getting closer to the LLMs we know and love/hate today. 
This issue covered the miracle story of GPTs 1 to 3. If &lt;span class="caps"&gt;GPT&lt;/span&gt;-3 was a child genius, ChatGPT is &lt;span class="caps"&gt;GPT&lt;/span&gt;-3 dressed up for work. Let&amp;#8217;s talk about what OpenAI had to do to it for public release—next&amp;nbsp;issue.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue174.html"&gt;Issue 174: Reinforcement&amp;nbsp;Learning&lt;/a&gt;&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 172: Tokens, the currency of LLMs</title><link href="https://ngjunsiang.github.io/laymansguide/issue172.html" rel="alternate"></link><published>2026-06-22T08:00:00+08:00</published><updated>2026-06-22T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-06-22:/laymansguide/issue172.html</id><summary type="html">&lt;p&gt;A model does not see letters or words, only tokens. These tokens are typically generated from user input through a pre-tokenizer program. Tokens are represented in the model as embeddings, a sequence of numbers representing the token&amp;#8217;s position in the embedding matrix. The model uses each token&amp;#8217;s embedding, and its surrounding tokens, to infer its meaning in&amp;nbsp;context.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; The Transformer architecture, unlike previous machine learning model architectures, could generate its next item while processing all previous items at the same time. The technique of unsupervised learning trained models on unlabelled data, letting the model pick up patterns in underlying data instead of having it learn correct answers only, and was much faster than supervised learning. OpenAI applied both these ideas at scale, producing &lt;span class="caps"&gt;GPT&lt;/span&gt;-1, a model that beat best-performing models while requiring relatively little human supervision during&amp;nbsp;training.&lt;/p&gt;
&lt;p&gt;Wait—what exactly does a large language model (&lt;span class="caps"&gt;LLM&lt;/span&gt;) work with? Individual letters? Entire words? No, they work&amp;nbsp;with—&lt;/p&gt;
&lt;h2&gt;Tokens&lt;/h2&gt;
&lt;p&gt;Tokens are clusters of letters that make up the training data. The large language model (&lt;span class="caps"&gt;LLM&lt;/span&gt;) does not &amp;#8220;see&amp;#8221; letters or words, only &lt;strong&gt;tokens&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Tokens are &amp;#8230; quite unlike phonemes, syllables, or other word-fragments you and I are familiar with. They are typically programmatically generated by a separate program (not a model), based on letter-clusters that appear most frequently in the&amp;nbsp;data.&lt;/p&gt;
&lt;p&gt;For example, using &lt;a href="https://platform.openai.com/tokenizer"&gt;OpenAI&amp;#8217;s Tokenizer tool&lt;/a&gt; to visualize the above paragraph gives us&amp;nbsp;this:&lt;/p&gt;
&lt;p&gt;&lt;img alt="OpenAI Tokenizer tool - text view" src="https://ngjunsiang.github.io/laymansguide/tokenizer-text.png" /&gt;&lt;br /&gt;
&lt;em&gt;OpenAI Tokenizer - text view&lt;/em&gt;  &lt;br /&gt;
&lt;img alt="OpenAI Tokenizer tool - token ID view" src="https://ngjunsiang.github.io/laymansguide/tokenizer-ids.png" /&gt;&lt;br /&gt;
&lt;em&gt;OpenAI Tokenizer - token &lt;span class="caps"&gt;ID&lt;/span&gt;&amp;nbsp;view&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;There is little human-discernible pattern as to what definitively constitutes a token: it could be a single punctuation mark, a letter or two (and sometimes including their preceding space, sometimes not), or an entire&amp;nbsp;word.&lt;/p&gt;
&lt;p&gt;Whatever the case, what we see&amp;nbsp;as &lt;code&gt;" you and I"&lt;/code&gt;, a &lt;span class="caps"&gt;LLM&lt;/span&gt; sees&amp;nbsp;as &lt;code&gt;[481, 326, 357]&lt;/code&gt;. A pre-tokenizer program tokenizes all input into numerical&amp;nbsp;values.&lt;/p&gt;
&lt;p&gt;Now you understand a little better why ChatGPT struggles to count Rs in &amp;#8220;strawberry&amp;#8221;, or in any other fruit&amp;nbsp;really.&lt;/p&gt;
&lt;h2&gt;Embeddings&lt;/h2&gt;
&lt;p&gt;How does the model&amp;nbsp;tell &lt;code&gt;481&lt;/code&gt;, &lt;code&gt;326&lt;/code&gt;,&amp;nbsp;and &lt;code&gt;357&lt;/code&gt; apart? How does it store or represent them within itself? Here, I am going to need you to use your imagination. You are familiar with the concept of a scatter plot, yes? A graph that looks like&amp;nbsp;this:&lt;/p&gt;
&lt;p&gt;&lt;img alt="A scatterplot with 2 dimensions" src="https://ngjunsiang.github.io/laymansguide/scatter-plot.png" /&gt;&lt;br /&gt;
&lt;em&gt;A scatterplot with 2 dimensions&lt;/em&gt;&lt;br /&gt;
Source: &lt;a href="https://www.embeddedsource.de/use-a-scatterplot-to-interpret-data/"&gt;EmbeddedSource&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now imagine a scatterplot with as many data points as tokens. In &lt;span class="caps"&gt;GPT&lt;/span&gt;-1&amp;#8217;s case, that&amp;#8217;s approx. 40,000 tokens—its vocabulary size. Yes, I know that&amp;#8217;s a lot of points, but you can &lt;em&gt;roughly&lt;/em&gt; visualize that, yes? Good, that&amp;#8217;s the easy&amp;nbsp;part.&lt;/p&gt;
&lt;p&gt;Now I need you to imagine the scatterplot with &amp;#8230; &lt;em&gt;*checks notes*&lt;/em&gt;—768 dimensions. No, that is not a typo, we &lt;em&gt;are&lt;/em&gt; talking about a scatterplot with 768 dimensions. Oh, that&amp;#8217;s too difficult to imagine? Yeah. Sorry, that&amp;#8217;s why I don&amp;#8217;t have an image attached. Just try your best&amp;nbsp;🙏&lt;/p&gt;
&lt;p&gt;Essentially that is what a &lt;span class="caps"&gt;LLM&lt;/span&gt; generates as a result of its training. Each token in its vocabulary becomes a data point, and each data point is represented in this 768-dimensional space using 768 decimal numbers ranging from 0 to 1.0. This positional representation using many decimal numbers is called an &lt;strong&gt;embedding&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;Other uses for&amp;nbsp;embeddings&lt;/h2&gt;
&lt;p&gt;Embeddings are also not a new idea: they precede &lt;span class="caps"&gt;GPT&lt;/span&gt; by decades, having been conceptualized as early as the&amp;nbsp;1980s.&lt;/p&gt;
&lt;p&gt;Because they&amp;#8217;re such a handy and intuitive mathematical way to represent or visualize tokens and semantics, they&amp;#8217;re also used often in semantic search engines (which try to infer what you &lt;em&gt;mean&lt;/em&gt; instead of what you &lt;em&gt;said&lt;/em&gt;), recommendation engines (suggesting similar things based on what you bought or liked), relevance scoring,&amp;nbsp;etc.&lt;/p&gt;
&lt;h2&gt;How a &lt;span class="caps"&gt;LLM&lt;/span&gt; represents&amp;nbsp;semantics&lt;/h2&gt;
&lt;p&gt;There&amp;#8217;s more to a &lt;span class="caps"&gt;LLM&lt;/span&gt; than this collection of 40,000 embeddings; it forms only a tiny fraction of the entire model. But it is critical to how the &lt;span class="caps"&gt;LLM&lt;/span&gt; &amp;#8220;learns&amp;#8221; information from the text. Based on where the tokens appear relative to each other in the text, and the higher-order patterns that the model detects through its hidden layers, the model adjusts the embedding for each token, placing semantically similar ones closer to each other and dissimilar tokens farther away from each&amp;nbsp;other.&lt;/p&gt;
&lt;p&gt;And because this is a mathematical space with direction (in 768 dimensions), the model can also pick up on analogy to some extent: if you draw a (768-dimensional) arrow&amp;nbsp;pointing &lt;code&gt;king → queen&lt;/code&gt; and another arrow&amp;nbsp;pointing &lt;code&gt;father → mother&lt;/code&gt; within this embedding matrix, they end up almost parallel. This means the model can solve &lt;span class="caps"&gt;SAT&lt;/span&gt; vocab pairs, giving you &amp;#8220;mother&amp;#8221; when you give it &amp;#8220;king:queen,&amp;nbsp;father:?&amp;#8221;&lt;/p&gt;
&lt;p&gt;If an &lt;span class="caps"&gt;LLM&lt;/span&gt; relied only on this embedding matrix, it would not be able to distinguish &amp;#8220;bat&amp;#8221; as a warm flying mammal from &amp;#8220;bat&amp;#8221; as a piece of sporting equipment. The rest of the model—using the Transformer architecture, you&amp;#8217;ll recall from &lt;a href="https://ngjunsiang.github.io/laymansguide/issue171.html"&gt;issue 171&lt;/a&gt;—uses the tokens surrounding it and their positions to infer the context that &amp;#8220;bat&amp;#8221; is being used&amp;nbsp;in.&lt;/p&gt;
&lt;h2&gt;Model pricing and&amp;nbsp;limits&lt;/h2&gt;
&lt;p&gt;Most ChatGPT/Claude users are familiar with those products as subscriptions, where they pay a certain price per month to use ChatGPT/Claude for some arbitrary amount, and if they use too much too quickly they hit a usage limit and have to wait for it to&amp;nbsp;reset.&lt;/p&gt;
&lt;p&gt;But if you are a business, and using the &lt;span class="caps"&gt;API&lt;/span&gt; instead, you&amp;#8217;ll be looking at a different page, such as the &lt;a href="https://developers.openai.com/api/docs/pricing"&gt;&lt;span class="caps"&gt;API&lt;/span&gt; pricing page for OpenAI&amp;#8217;s &lt;span class="caps"&gt;API&lt;/span&gt;&lt;/a&gt;. Notice that prices are typically quoted in units of &amp;#8220;1M tokens&amp;#8221;, standing for &amp;#8220;1 million tokens&amp;#8221;. Now you know what those tokens are referring&amp;nbsp;to.&lt;/p&gt;
&lt;p&gt;Likewise, when Anthropic explains how usage and length limits work, and tell you that &amp;#8220;Claude&amp;#8217;s context window is 200K tokens&amp;#8221;, you now know what they are referring to. More importantly, you know it doesn&amp;#8217;t mean 200 characters or 200&amp;nbsp;words.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; A model does not see letters or words, only tokens. These tokens are typically generated from user input through a pre-tokenizer program. Tokens are represented in the model as embeddings, a sequence of numbers representing the token&amp;#8217;s position in the embedding matrix. The model uses each token&amp;#8217;s embedding, and its surrounding tokens, to infer its meaning in&amp;nbsp;context.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;I would have gone on longer, but I think tokens are a pretty novel concept for most layfolks and deserve their own issue to sit with and digest before we talk about what a model&amp;nbsp;does.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue173.html"&gt;Issue 173: Training, Inference, and&amp;nbsp;Scaling&lt;/a&gt;&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 171: The first Generative Pre-Training model, GPT-1</title><link href="https://ngjunsiang.github.io/laymansguide/issue171.html" rel="alternate"></link><published>2026-06-15T08:00:00+08:00</published><updated>2026-06-08T16:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-06-15:/laymansguide/issue171.html</id><summary type="html">&lt;p&gt;The Transformer architecture, unlike previous machine learning model architectures, could generate its next item while processing all previous items at the same time. The technique of unsupervised learning trained models on unlabelled data, letting the model pick up patterns in underlying data instead of having it learn correct answers only, and was much faster than supervised learning. OpenAI applied both these ideas at scale, producing &lt;span class="caps"&gt;GPT&lt;/span&gt;-1, a model that beat best-performing models while requiring relatively little human supervision during&amp;nbsp;training.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Models simplify and represent a relationship between input values and output values. The more complex the relationship, the more parameters the model needs to learn. Models are simplifications of reality, and their performance depends on how well they capture underlying patterns in the data, as well as the quality and quantity of the&amp;nbsp;dataset.&lt;/p&gt;
&lt;p&gt;We are going to set aside image and audio models for today, and narrow down to focus on &lt;strong&gt;language models&lt;/strong&gt; in particular, because that&amp;#8217;s what sparked off the &lt;span class="caps"&gt;AI&lt;/span&gt;&amp;nbsp;craze.&lt;/p&gt;
&lt;h2&gt;The pre-2018 machine learning&amp;nbsp;paradigm&lt;/h2&gt;
&lt;p&gt;I am not a machine learning researcher and can&amp;#8217;t tell you what the prevailing &lt;em&gt;research&lt;/em&gt; paradigm at that point was. But in open-source and consumer applications, it seemed machine learning models were &lt;em&gt;bespoke&lt;/em&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You started with something specific you needed, like image classification, optical character recognition (&lt;span class="caps"&gt;OCR&lt;/span&gt;), speech recognition, translation,&amp;nbsp;&amp;#8230;&lt;/li&gt;
&lt;li&gt;You collected a &lt;em&gt;huuuuuge&lt;/em&gt; dataset of input-output pairs for that specific task: images and their labels, scanned documents and their text, audio, etc. And by huuuuuge I mean tens of thousands to millions of&amp;nbsp;examples.&lt;/li&gt;
&lt;li&gt;After collecting the data you often have to clean it up (remove duplicates, remove outliers, etc.) and label it (e.g. label images with their correct&amp;nbsp;labels).&lt;/li&gt;
&lt;li&gt;You then trained a model on part of the dataset, tweaking parameters and trying different architectures (ways of arranging&amp;nbsp;parameters).&lt;/li&gt;
&lt;li&gt;You tested the model on the other part of the dataset, passing each input through the model and comparing the output to the expected output, and measuring how well it&amp;nbsp;performed.&lt;/li&gt;
&lt;li&gt;You repeated steps 4 and 5 until you were satisfied with the model&amp;#8217;s performance, and then you deployed it for&amp;nbsp;use.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This technique of using labelled data to train the model is called &lt;strong&gt;supervised learning&lt;/strong&gt;, because of the need to tweak the model&amp;#8217;s parameters (under human supervision) to match the expected&amp;nbsp;output.&lt;/p&gt;
&lt;p&gt;There were (and still are) many machine learning models trained this way and used. For example, &lt;a href="https://tesseractocr.org/"&gt;tesseract&lt;/a&gt; is an open-source &lt;span class="caps"&gt;OCR&lt;/span&gt; engine that was first released in 2005. It was trained on a dataset of scanned documents and their corresponding text, and has been used in various applications for &lt;span class="caps"&gt;OCR&lt;/span&gt; tasks. Another example is the ResNet architecture for image classification, which was introduced in 2015 and has been widely used for image recognition&amp;nbsp;tasks.&lt;/p&gt;
&lt;h2&gt;The Transformer&amp;nbsp;architecture&lt;/h2&gt;
&lt;p&gt;Before Google&amp;#8217;s 2017 paper on the attention mechanism, the prevailing machine learning models had two problematic&amp;nbsp;limitations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;they &amp;#8220;looked&amp;#8221; at input data one item at a time to produce the output, resulting in slow output&amp;nbsp;generation&lt;/li&gt;
&lt;li&gt;because of the above, data that was processed earlier seldom made it through to the end of the model, resulting in a recency bias: the model tended to focus on the most recent input data and ignore earlier input&amp;nbsp;data&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The attention mechanism introduced in Google&amp;#8217;s 2017 paper allowed models to &amp;#8220;look&amp;#8221; at all input data at once, speeding up output generation. The same mechanism also computed which parts of the input data were most relevant for producing the&amp;nbsp;output.&lt;/p&gt;
&lt;p&gt;Attention was not a new mechanism in machine learning: prior models had used them, but in separate stages, and alongside other mechanisms. Google&amp;#8217;s paper was the first to ask: &amp;#8220;what if we &lt;em&gt;only used attention everywhere&lt;/em&gt;?&amp;#8221; The resulting architecture, which they called the &amp;#8220;Transformer&amp;#8221;, was a breakthrough in speed and&amp;nbsp;simplicity.&lt;/p&gt;
&lt;h2&gt;Unsupervised&amp;nbsp;learning&lt;/h2&gt;
&lt;p&gt;Besides the Transformer architecture, another breakthrough was already making its rounds: instead of task-specific datasets, researchers wondered why they needed so many task-specific datasets. Since the data represented different subsets of reality (from different tasks), what if they just trained a single model on a really, really large dataset of text to produce a &lt;strong&gt;base model&lt;/strong&gt;? Then they could fine-tune it on smaller task-specific datasets to produce task-specific&amp;nbsp;models.&lt;/p&gt;
&lt;p&gt;This technique, called &lt;strong&gt;unsupervised learning&lt;/strong&gt;, did not require data to be labelled—the model &amp;#8220;learns&amp;#8221; patterns in the underlying data without human correction, simply trying to predict the next word in the training data given the previous&amp;nbsp;words.&lt;/p&gt;
&lt;h2&gt;Generative Pre-trained Transformer (&lt;span class="caps"&gt;GPT&lt;/span&gt;)&lt;/h2&gt;
&lt;p&gt;A few researchers at OpenAI then had the idea to try this pre-training approach on the Transformer architecture. OpenAI built the first &lt;strong&gt;Generative Pre-trained Transformer&lt;/strong&gt; (&lt;span class="caps"&gt;GPT&lt;/span&gt;) model, which they released in 2018. &lt;strong&gt;Generative&lt;/strong&gt; means the model generates output based on input, producing one output item at a time (but processing all inputs simultaneously). &lt;strong&gt;Pre-trained&lt;/strong&gt; means the model was largely trained through unsupervised learning. &lt;strong&gt;Transformer&lt;/strong&gt; refers to the underlying&amp;nbsp;architecture.&lt;/p&gt;
&lt;p&gt;They went &lt;em&gt;big&lt;/em&gt; on scale: &lt;span class="caps"&gt;GPT&lt;/span&gt;-1 trained on a dataset of 7,000 self-published books comprising 985 million words, representing this data using 117 million parameters—an unheard-of scale at the time (but now considered paltry). It attracted attention from the research community not only by improving on best-performing models on various language tasks, but by improving on &lt;em&gt;all of them&lt;/em&gt;, with &lt;em&gt;minimal task-specific training&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Due to the unprecedented number of parameters used, &lt;span class="caps"&gt;GPT&lt;/span&gt;-1 was considered a &lt;strong&gt;large language model&lt;/strong&gt; (&lt;span class="caps"&gt;LLM&lt;/span&gt;), to distinguish it from smaller models that came before. However, this was a research idea, with code that was far from release-ready, and nobody except research-minded folks knew how to get &lt;span class="caps"&gt;GPT&lt;/span&gt;-1 running. And thus, this went unnoticed by the&amp;nbsp;public.&lt;/p&gt;
&lt;p&gt;Still, this was a breakthrough: no research lab before OpenAI had the kind of resources that enabled them to try this idea. It did require resources that most labs didn&amp;#8217;t have at the time: 8 GPUs, when most labs ran their training on a single &lt;span class="caps"&gt;GPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; The Transformer architecture, unlike previous machine learning model architectures, could generate its next item while processing all previous items at the same time. The technique of unsupervised learning trained models on unlabelled data, letting the model pick up patterns in underlying data instead of having it learn correct answers only, and was much faster than supervised learning. OpenAI applied both these ideas at scale, producing &lt;span class="caps"&gt;GPT&lt;/span&gt;-1, a model that beat best-performing models while requiring relatively little human supervision during&amp;nbsp;training.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;We&amp;#8217;re almost at the meaty part! I kinda snuck in 2 ideas today: the Transformer architecture (a minor part of this series actually) and unsupervised learning. I don&amp;#8217;t think you would have wanted to wait a week in between before hearing how OpenAI combined the two, haha &amp;#8230; so there you&amp;nbsp;go.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue172.html"&gt;Issue 172: Tokens, the currency of&amp;nbsp;LLMs&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Wait—what exactly does a large language model (&lt;span class="caps"&gt;LLM&lt;/span&gt;) work with? Individual letters? Entire words? Find out next&amp;nbsp;issue!&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 170: Machine learning models</title><link href="https://ngjunsiang.github.io/laymansguide/issue170.html" rel="alternate"></link><published>2026-06-08T11:30:00+08:00</published><updated>2026-06-08T11:30:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2026-06-08:/laymansguide/issue170.html</id><summary type="html">&lt;p&gt;Models simplify and represent a relationship between input values and output values. The more complex the relationship, the more parameters the model needs to learn. Models are simplifications of reality, and their performance depends on how well they capture underlying patterns in the data, as well as the quality and quantity of the&amp;nbsp;dataset.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; By better understanding how search bots categorise pages, a website owner can use keywords and other techniques to optimise the ranking of their page for specific search&amp;nbsp;terms.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;[Editor&amp;#8217;s Note]&lt;/strong&gt; Layman&amp;#8217;s Guide to Computing went on hiatus after its 13th season, because my promise when I began was to write only things widespread enough that I thought layfolks should have an accessible-yet-useful introduction&amp;nbsp;to.&lt;/p&gt;
&lt;p&gt;As I wrapped up Season 13 in 2022, the trend at that time was cloud computing. I tackled emulation and virtualization in Season 12, then the internet and online services in Season 13. ChatGPT launched in November 2022 that year. In 2024, I was first asked if I would continue Layman&amp;#8217;s Guide again to write about &lt;span class="caps"&gt;AI&lt;/span&gt;. I said no; less than half my colleagues were using ChatGPT or had heard of it, and I didn&amp;#8217;t think there would be enough common knowledge for me to usefully write about &lt;span class="caps"&gt;AI&lt;/span&gt;&amp;nbsp;yet.&lt;/p&gt;
&lt;p&gt;But now, in 2026, even my employers are actively promoting genAI, my students are using ChatGPT, and by the end of this year it would likely be difficult to find someone who hasn&amp;#8217;t heard of Claude Code or Gemini Pro or Codex. I suppose it&amp;#8217;s time to add one more&amp;nbsp;season.&lt;/p&gt;
&lt;p&gt;There are many explainers out there; I&amp;#8217;ve read a large number of them, many very good! But this is Layman&amp;#8217;s Guide to Computing, and something I noticed talking to laypeople is confusion: where did this &lt;span class="caps"&gt;AI&lt;/span&gt; come from? Why hadn&amp;#8217;t it been invented earlier? How does it work? What can it do? What can&amp;#8217;t it&amp;nbsp;do?&lt;/p&gt;
&lt;p&gt;So let&amp;#8217;s rewind time: I started writing Layman&amp;#8217;s Guide to Computing in 2018. A year before that, eight machine learning engineers at Google had published &amp;#8220;&lt;em&gt;Attention is All You Need&lt;/em&gt;,&amp;#8221; the paper that introduced the transformer architecture that underpins most of today&amp;#8217;s genAI. In mid-2018, before I started writing, OpenAI was still a non-profit research lab founded by Elon Musk and Sam Altman, and had just released the first version of &lt;span class="caps"&gt;GPT&lt;/span&gt;, a language model that was not yet large enough to generate coherent text. Following Google&amp;#8217;s whitepaper on the attention mechanism, they had just released a paper, &amp;#8220;&lt;em&gt;Improving Language Understanding by Generative Pre-Training&lt;/em&gt;&amp;#8221;, that described the architecture and training process for &lt;span class="caps"&gt;GPT&lt;/span&gt;, their first large language&amp;nbsp;model.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s a little hard to mentally reconstruct the tech culture and public awareness of the field of artificial intelligence and machine learning at that point in time. So let&amp;#8217;s start by understanding: what is a model? How were they used&amp;nbsp;then?&lt;/p&gt;
&lt;h2&gt;Models&lt;/h2&gt;
&lt;p&gt;You may not know it, but you were already using models in your daily life in 2017. When the iPhone launched, it had intelligent autocorrect and touch auto-adjustment features. For these features to work, Apple had to train machine learning models on large datasets of text and touch interactions. These models were then deployed on the iPhone to provide the autocorrect and touch adjustment&amp;nbsp;functionality.&lt;/p&gt;
&lt;p&gt;What are these models? You would likely have used them in a stats course, perhaps even in high school. If you were ever asked to sketch a best-fit line, a trendline, or a linear regression, you were already drawing a model. To do that,&amp;nbsp;you:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Hypothesized a linear relationship between an input&amp;nbsp;variable &lt;code&gt;x&lt;/code&gt; and an output&amp;nbsp;variable &lt;code&gt;y&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Collected data points&amp;nbsp;(&lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;) through an&amp;nbsp;experiment.&lt;/li&gt;
&lt;li&gt;Represented the relationship&amp;nbsp;between &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; using a mathematical formula&amp;nbsp;(&lt;code&gt;y = mx + b&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Determined the&amp;nbsp;parameters &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; that best fit the data&amp;nbsp;points.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You &lt;em&gt;compressed&lt;/em&gt; the data—multiple sets of points (which we call a &lt;strong&gt;dataset&lt;/strong&gt;)—into two &lt;strong&gt;parameters&lt;/strong&gt;, &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;, a simpler representation that captures the underlying relationship. This representation is a &lt;strong&gt;model&lt;/strong&gt;. (We sometimes call it a mental model when we don&amp;#8217;t have it formally represented as a mathematical relationship, just a conceptual&amp;nbsp;description.)&lt;/p&gt;
&lt;p&gt;Apple&amp;#8217;s machine learning models do something similar. An autocorrect model takes a dataset of incorrect words/phrases and their actual words/phrases, and compresses it into a text correction model. A touch auto-adjustment model takes a dataset of touch interactions and their intended targets, and compresses it into a model that can predict the intended touch target based on the touch&amp;nbsp;input.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;tl;dr&lt;/strong&gt; A model takes in input values and produces output values based on patterns it has learned from training&amp;nbsp;data.&lt;/p&gt;
&lt;h2&gt;More complex&amp;nbsp;models&lt;/h2&gt;
&lt;p&gt;Of course, more complex models do not use a linear equation or a simple mathematical formula anymore. Machine learning researchers first represent more complex relationships using more complex formulas, such as polynomials or decision trees, which use more&amp;nbsp;parameters.&lt;/p&gt;
&lt;p&gt;But for other purposes the input may not be a single variable and the output may not be a single variable either. For example, in image recognition, the input is an image (which can be represented as a grid of pixel values), and the output is a label (e.g., &amp;#8220;cat&amp;#8221;, &amp;#8220;dog&amp;#8221;, &amp;#8220;car&amp;#8221;). An image classifier may have 64 input values (one for each pixel in an 8×8 image) and 10 output values (one for each possible label). The model would learn to map the input pixel values to the correct label based on patterns in the training data. That&amp;#8217;s 640 parameters (64 input values x 10 output values) that the model would learn to adjust during&amp;nbsp;training.&lt;/p&gt;
&lt;p&gt;This direct mapping of input to output can only take us so far. Perhaps output 1 doesn&amp;#8217;t just depend on inputs 1 to 10, but on some intermediate value calculated from them. Now we have to add intermediate &lt;strong&gt;layers&lt;/strong&gt; between input and output, which researchers call &amp;#8220;hidden layers&amp;#8221;. These layers allow the model to learn and represent more complex relationships between input and output. Each layer can have its own parameters, and the model learns to adjust these parameters during training to improve its&amp;nbsp;performance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;tl;dr&lt;/strong&gt; More complex models use more parameters to represent the relationship between input values, intermediate values, and output values. Each parameter represents a relationship between two values. The more parameters, the more complex the relationships the model can&amp;nbsp;learn.&lt;/p&gt;
&lt;h2&gt;Limitations of&amp;nbsp;models&lt;/h2&gt;
&lt;p&gt;Models sound like mathematical dark magic, and often feel like it too. But like the mathematical models we learned in school, they have&amp;nbsp;limitations.&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;ve seen how far some of your data points deviate from your best-fit line or trendline, you already know that the model cannot accurately represent all the data points—it is only a simplification. Likewise, all machine learning models are simplifications of&amp;nbsp;reality.&lt;/p&gt;
&lt;p&gt;Their performance depends on how well they capture underlying patterns in the data: pick an inappropriate representation for the feature, e.g. a linear formula instead of a polynomial, and the model will perform&amp;nbsp;poorly.&lt;/p&gt;
&lt;p&gt;It is also possible to go to the other extreme, adding a complex model with many parameters that fits the training data perfectly, but does not predict other data points well—an overfitted model. You can have a computer come up with a sine-decay formula that fits your first 6 data points perfectly, but wildly overshoot a 7th data&amp;nbsp;point.&lt;/p&gt;
&lt;p&gt;Also, their performance depends on the quality and quantity of the dataset. If your data does not represent the underlying reality well enough, missing important patterns or exceptions, or not covering a sufficient variety of cases, the model can pick out the wrong features and learn the wrong patterns. In the early days of machine learning, some researchers found that when training image classifiers on images of dogs and cats, the model began identifying any brown creature sitting on grass as a dog, because the training dataset had many images of dogs sitting on grass, but few images of cats sitting on grass. The model had learned to associate grass with dogs, which was not the intended&amp;nbsp;pattern.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Models simplify and represent a relationship between input values and output values. The more complex the relationship, the more parameters the model needs to learn. Models are simplifications of reality, and their performance depends on how well they capture underlying patterns in the data, as well as the quality and quantity of the&amp;nbsp;dataset.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;After experiencing the magic of ChatGPT and other genAI tools, it&amp;#8217;s easy to forget, or perhaps not even realise, that fundamentally they are powered by the same underlying principles that we apply in simpler&amp;nbsp;experiments.&lt;/p&gt;
&lt;p&gt;But&amp;nbsp;between &lt;code&gt;y = mx + b&lt;/code&gt; and ChatGPT, there is still &amp;#8230; such a huge gulf of complexity. We still have quite a way to&amp;nbsp;go.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue171.html"&gt;Issue 171: The first Generative Pre-Training model, &lt;span class="caps"&gt;GPT&lt;/span&gt;-1&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;What was the fundamental insight that made &lt;span class="caps"&gt;GPT&lt;/span&gt; and other LLMs possible? Find out next season&amp;nbsp;;)&lt;/p&gt;</content><category term="Season 14"></category></entry><entry><title>Issue 169: Search engine optimisation</title><link href="https://ngjunsiang.github.io/laymansguide/issue169.html" rel="alternate"></link><published>2022-04-23T08:00:00+08:00</published><updated>2022-04-23T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-04-23:/laymansguide/issue169.html</id><summary type="html">&lt;p&gt;By better understanding how search bots categorise pages, a website owner can use keywords and other techniques to optimise the ranking of their page for specific search&amp;nbsp;terms.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; A search engine uses bots to build up a database of URLs and their contents. The search engine uses various algorithms to determine the most relevant results for a search&amp;nbsp;request.&lt;/p&gt;
&lt;p&gt;Let’s get to it: why are search results so bad so&amp;nbsp;often?&lt;/p&gt;
&lt;h2&gt;PageRank&lt;/h2&gt;
&lt;p&gt;While PageRank is no longer the only or even the dominant algorithm for ranking search results, it is probably the most familiar one to most people and is easy to&amp;nbsp;understand.&lt;/p&gt;
&lt;p&gt;PageRank ranks your page higher if there are many other pages (on other domains) linking to it, so one way to raise its rankings is to make other websites with links that point to your&amp;nbsp;page.&lt;/p&gt;
&lt;h2&gt;&lt;meta&gt; tags&lt;/h2&gt;
&lt;p&gt;Every &lt;span class="caps"&gt;HTML&lt;/span&gt; page, if you check the page source, has a section that looks&amp;nbsp;like &lt;code&gt;&amp;lt;head&amp;gt;...&amp;lt;/head&amp;gt;&lt;/code&gt;. This section is not displayed in the webpage, and usually contains metadata: information about the page. This is what many site-crawling bots use to gather information about the page, instead of trying to parse the entire page’s&amp;nbsp;content.&lt;/p&gt;
&lt;p&gt;By putting appropriate keywords or descriptions, you can make the page appear higher in search rankings. This is a well known way to increase discoverability so search engines are savvy at downranking pages that simply spam keywords. But many subtle hacks also exist for pushing a page higher in search results for specific keywords, whether that page deserves to appear higher or&amp;nbsp;not.&lt;/p&gt;
&lt;p&gt;As more and more page categorisation is automated, pages increasingly have to be bot-savvy to be correctly categorised and indexed for appearing in search&amp;nbsp;results.&lt;/p&gt;
&lt;h2&gt;Advertising&lt;/h2&gt;
&lt;p&gt;Naturally, Google runs an online advertising business and it puts advertised results first, so this obviously skews the&amp;nbsp;rankings.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; By better understanding how search bots categorise pages, a website owner can use keywords and other techniques to optimise the ranking of their page for specific search&amp;nbsp;terms.&lt;/p&gt;
&lt;p&gt;Not very detailed, because I am not an &lt;span class="caps"&gt;SEO&lt;/span&gt; master! This issue is probably the least helpful for more savvy readers, but everyone finds out about page keywords at some point and I hope this issue is that first time for some readers&amp;nbsp;:)&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;I am &amp;#8230; done! With the main part of Layman’s Guide to Computing at&amp;nbsp;least.&lt;/p&gt;
&lt;h3&gt;Going even&amp;nbsp;slower&lt;/h3&gt;
&lt;p&gt;What this means is I still have bits of content to post, but they are no longer substantial enough to form seasons of 13 issues. Instead, I’m going to be posting in dribs and drabs, guided by current affairs and any new technologies that have gone mainstream. This also means my posting frequency is going to be going way down; expect 1–2 issues a month, instead of the current 4–5. And I’m going to be taking the next 2 weekends off for a little celebration of my own, plus some thinking and&amp;nbsp;reflecting.&lt;/p&gt;
&lt;h3&gt;Better&amp;nbsp;archives&lt;/h3&gt;
&lt;p&gt;I also realised that &lt;a href="https://buttondown.email/laymansguide/archive/"&gt;Buttondown’s archive page&lt;/a&gt; no longer provides a browseable listing of all issues. I am looking into setting up my own issue index instead through Github Pages, on https://ngjunsiang.github.io/laymansguide/. Fortunately I have been using Github to archive past issues, so now it’s simply a matter of editing links for past issues and providing some kind of navigation. More updates on this when I make some kind of&amp;nbsp;progress.&lt;/p&gt;
&lt;h3&gt;Refactoring&lt;/h3&gt;
&lt;p&gt;Code refactoring is when developers reorganise and rewrite their code in a way that makes it easier for them moving&amp;nbsp;forward.&lt;/p&gt;
&lt;p&gt;You might have noticed how often I am linking to past issues, because so many concepts in computing build on each other. I knew this was going to be a problem when trying to explain computing to anyone, and I had initially envisioned some kind of wiki where pages will link to each other so you can theoretically start exploring from any topic and unpack technical terms as you&amp;nbsp;go.&lt;/p&gt;
&lt;p&gt;The main bottleneck for this had been content: it’s rather overwhelming to think about where and even how to start. And that was how this newsletter was born. Thanks to Layman’s Guide, I now have a steady base of content for publishing Layman’s Wiki, and work on it has been slow but progressing. I’ll put up a link through this newsletter once it is ready for more eyes&amp;nbsp;:)&lt;/p&gt;
&lt;h2&gt;What I&amp;#8217;ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue170.html"&gt;Issue 170: Machine learning&amp;nbsp;models&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After a long hiatus, Layman&amp;#8217;s Guide returns with Season 14! But first, let&amp;#8217;s understand what models are and how they&amp;nbsp;work.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;As always, thank you for&amp;nbsp;reading!&lt;/p&gt;</content><category term="Season 13"></category></entry><entry><title>Issue 168: Search engines</title><link href="https://ngjunsiang.github.io/laymansguide/issue168.html" rel="alternate"></link><published>2022-04-16T08:00:00+08:00</published><updated>2022-04-16T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-04-16:/laymansguide/issue168.html</id><summary type="html">&lt;p&gt;A search engine uses bots to build up a database of URLs and their contents. The search engine uses various algorithms to determine the most relevant results for a search&amp;nbsp;request.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Fragmentation is likely a contributor of system slowdown, particularly for mobile devices: the database used by most mobile apps tend to store data in many small chunks rather than fewer big chunks, which slows down data search operations. The most effective measure for improving device responsiveness is usually to clear the app cache, so the app does not attempt to read previous data from&amp;nbsp;storage.&lt;/p&gt;
&lt;p&gt;Last issue, we shed a little light on the mystery of why phone and laptop systems slow down over time—apparently the way a file database works is to&amp;nbsp;blame?&lt;/p&gt;
&lt;p&gt;This week, we switch topics, to look at something we definitely take for granted: search&amp;nbsp;engines!&lt;/p&gt;
&lt;h2&gt;What is a search&amp;nbsp;engine?&lt;/h2&gt;
&lt;p&gt;Forgive me if you think I am belabouring the obvious. There is the user definition, which is something like “a search box that answers my questions”. Then there is the developer definition which is more like “an indexed database of URLs, descriptions, and ranks”. Let’s unpack&amp;nbsp;that.&lt;/p&gt;
&lt;h2&gt;Indexing and&amp;nbsp;arachnids&lt;/h2&gt;
&lt;p&gt;Another obvious point: at the point when you submit your search query, there is no way the search engine could have trawled the entire internet so quickly to give you the results. It must have know about these pages beforehand &amp;#8230; but&amp;nbsp;how?&lt;/p&gt;
&lt;p&gt;Search engines run “bots”, also know as web spiders, or web crawlers. These are programs that retrieve pages, makes a note of the content (text, images, links, …) on that page, determine keywords for that page, and then follows the links to other pages and repeats the process. Like following the links of a (very messy) web!&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;When we “search the internet”, we are really searching the database that has been built up by these&amp;nbsp;bots.&lt;/p&gt;
&lt;h2&gt;Ranking&amp;nbsp;results&lt;/h2&gt;
&lt;p&gt;So there’s a huge database … how does the search engine determine which are the most relevant&amp;nbsp;results?&lt;/p&gt;
&lt;p&gt;Google’s PageRank algorithm is by now well known for disrupting the old directory-based method of organising information, and built for Larry Page and Sergey Brin a sizable empire. This is one way of determining how important/useful a page is: by seeing how many other pages link to it. There are other ways, but this is a layman’s newsletter and I don’t want to dive into a technical analysis and comparison of different ranking algorithms&amp;nbsp;😬&lt;/p&gt;
&lt;p&gt;Instead, consider that there are by now many different algorithms for determining the relevance of database entries for each search, even within Google itself. A search engine is essentially a way to access this database, use one or more appropriate algorithms to determine the most relevant results, and return them to the&amp;nbsp;user.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; A search engine uses bots to build up a database of URLs and their contents. The search engine uses various algorithms to determine the most relevant results for a search&amp;nbsp;request.&lt;/p&gt;
&lt;p&gt;I know &amp;#8230; nobody really cares &lt;em&gt;how&lt;/em&gt; a search engine works. We just want to know &lt;em&gt;why&lt;/em&gt; it&amp;#8217;s not giving us the results we&amp;nbsp;want!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 169: Search engine&amp;nbsp;optimisation&lt;/p&gt;
&lt;p&gt;I am not a consultant for search engine optimisation (&lt;span class="caps"&gt;SEO&lt;/span&gt;), but we can look at some of the ways people attempt to “game the system” and exploit features of the algorithms that search engines use. More next&amp;nbsp;week!&lt;/p&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Yep, we all know spiders don’t actually do this.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 13"></category></entry><entry><title>Issue 167: Database fragmentation</title><link href="https://ngjunsiang.github.io/laymansguide/issue167.html" rel="alternate"></link><published>2022-04-09T08:00:00+08:00</published><updated>2022-04-09T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-04-09:/laymansguide/issue167.html</id><summary type="html">&lt;p&gt;Fragmentation is likely a contributor of system slowdown, particularly for mobile devices: the databases used by most mobile apps tend to store data in many small chunks rather than fewer big chunks, which slows down data search operations. The most effective measure for improving device responsiveness is usually to clear the app cache, so the app does not attempt to read previous data from&amp;nbsp;storage.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; There are easy and quick ways to check the validity of the most common advice for resolving system slowdown. But it still seems to happen even after these tips have been&amp;nbsp;tried.&lt;/p&gt;
&lt;p&gt;Last issue, we talked about caches and why they are no longer as effective as a performance-boosting&amp;nbsp;measure.&lt;/p&gt;
&lt;p&gt;This issue, let’s look into a solved problem that is not-as-solved on Android: file&amp;nbsp;fragmentation.&lt;/p&gt;
&lt;h2&gt;Storage fragmentation on mobile&amp;nbsp;devices&lt;/h2&gt;
&lt;p&gt;I mentioned in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue119.html"&gt;Issue 119&lt;/a&gt;) that fragmentation is not an issue for faster SSDs; the much lower latency of SSDs makes the retrieval of multiple file chunks from multiple locations trivially easy. But for slower storage devices, whether they are hard disk drives or SSDs over a &lt;span class="caps"&gt;UFS&lt;/span&gt; interface, fragmentation is very real. Unfortunately, its not just a simple matter of defragmenting a disk regularly&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;. the source of this fragmentation comes from the way apps themselves store&amp;nbsp;data.&lt;/p&gt;
&lt;h2&gt;SQLite, a blessing and a&amp;nbsp;curse&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://sqlite.org/index.html"&gt;SQLite&lt;/a&gt; is a popular file-based relational database (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue087.html"&gt;Issue 87&lt;/a&gt;)) used by many Android apps (including WhatsApp!). This means that unlike other databases, which require a separate database program to run, SQLite just requires the programmer to bundle an appropriate library for their programming language instead of running a separate program. It makes management of data much easier for the app developers, and it is also&amp;nbsp;fast—usually.&lt;/p&gt;
&lt;p&gt;However, &lt;a href="https://www.tuxera.com/blog/why-is-my-android-phone-slowing-down/"&gt;a 2016 study found that it contributes to fragmentation&lt;/a&gt; on the Android filesystem. Even after clearing the app’s data, once SQLite starts storing data again, it tends to store them in many small chunks instead of fewer but larger chunks. On Android devices, which use the slower &lt;span class="caps"&gt;UFS&lt;/span&gt; interface, searching through the database involves reading each database chunk to see if the requested data is there; this gets slower as the database grows, and SQLite adds more chunks to the storage&amp;nbsp;device.&lt;/p&gt;
&lt;p&gt;The study ends on a pretty pessimistic note: there doesn’t seem to be any feasible mitigation other than making upstream code changes to SQLite, or designing a different filesystem that overcomes this limitation. But that was five years ago; with any luck some positive developments might have been made into this area. I’m still looking into&amp;nbsp;it!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Fragmentation is likely a contributor of system slowdown, particularly for mobile devices: the databases used by most mobile apps tend to store data in many small chunks rather than fewer big chunks, which slows down data search operations. The most effective measure for improving device responsiveness is usually to clear the app cache, so the app does not attempt to read previous data from&amp;nbsp;storage.&lt;/p&gt;
&lt;p&gt;That’s all I’ve managed to find out from reading; further research will either involve detailed tinkering and experimentation, or deeper reading, both of which are&amp;nbsp;time-consuming.&lt;/p&gt;
&lt;p&gt;Short issue this time, because if I made it any longer I think it would be more technical than I would really like. Enjoy the&amp;nbsp;brain-break!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 168: Search&amp;nbsp;engines&lt;/p&gt;
&lt;p&gt;How do search engines &amp;#8230; know&amp;nbsp;everything?&lt;/p&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Defragmentation is discouraged on solid-state devices, as each read/write operation causes the storage medium to degrade. Under typical usage, a solid-state device can be expected to last many years. Defragmentation involves lots of read/write (as you are reading files and storing them elsewhere on the disk), hastens the degradation, and will shorten the lifespan of solid-state devices unnecessarily.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 13"></category><category term="cache"></category></entry><entry><title>Issue 166: A cause of system slowdown: caches</title><link href="https://ngjunsiang.github.io/laymansguide/issue166.html" rel="alternate"></link><published>2022-04-02T08:00:00+08:00</published><updated>2022-04-02T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-04-02:/laymansguide/issue166.html</id><summary type="html">&lt;p&gt;Caches speed up app operations by storing temporary data on the device’s storage. This assumes that access to storage is much faster than access to the file’s original source. On Android, users can clear an app’s cache, but not the system&amp;nbsp;cache.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; There are easy and quick ways to check the validity of the most common advice for resolving system slowdown. But it still seems to happen even after these tips have been&amp;nbsp;tried.&lt;/p&gt;
&lt;p&gt;Last issue, I walked through common causes of system slowdown suggested by generic tech websites, and explained simple ways of checking if these are really the cause. Quite often, they are not, especially if you are the kind who is careful about internet usage and does regular system&amp;nbsp;maintenance.&lt;/p&gt;
&lt;p&gt;So what is going&amp;nbsp;on?&lt;/p&gt;
&lt;h2&gt;Caches, caches, and more&amp;nbsp;caches&lt;/h2&gt;
&lt;p&gt;In &lt;a href="https://ngjunsiang.github.io/laymansguide/issue039.html"&gt;Issue 39&lt;/a&gt;), I explained what caches are: places where you (temporarily) store the result of lookups, so you don’t have to keep performing the lookup again. In context, this referred to &lt;span class="caps"&gt;DNS&lt;/span&gt; lookups: operations that translate a domain name (such&amp;nbsp;as &lt;code&gt;google.com&lt;/code&gt;) to an &lt;span class="caps"&gt;IP&lt;/span&gt; address (such&amp;nbsp;as &lt;code&gt;173.194.217.100&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;But caches are everywhere, not just in &lt;span class="caps"&gt;DNS&lt;/span&gt;.&lt;/p&gt;
&lt;h3&gt;Examples&lt;/h3&gt;
&lt;p&gt;When you open any Microsoft Office application, it shows you your most recently accessed documents: that’s a cache! (The info has to be stored somewhere,&amp;nbsp;right?).&lt;/p&gt;
&lt;p&gt;&lt;a href="https://www.thewindowsclub.com/windows-installer-folder-to-delete-or-not-to-delete-that-is-the-question"&gt;Windows caches your old installation files&lt;/a&gt; “just in&amp;nbsp;case”.&lt;/p&gt;
&lt;p&gt;Your browser definitely caches your browsing data (they are called “Temporary internet&amp;nbsp;files”).&lt;/p&gt;
&lt;p&gt;And most apps have a cache of some kind or other to hold data which they think you will want to access again&amp;nbsp;soon.&lt;/p&gt;
&lt;p&gt;These are generally harmless uses of caches, albeit possibly annoying (when Adobe Premiere caches video files that take up gigabytes of space …). Caches are predicated on the notion that access from a storage disk (on the same device), while slow, is still faster than its alternative (fetching the data from its source, over the internet or through a computationally expensive&amp;nbsp;calculation).&lt;/p&gt;
&lt;h2&gt;App caches: a short&amp;nbsp;history&lt;/h2&gt;
&lt;p&gt;Before the internet went mainstream, a lot of software came on compact discs or diskettes. These were slow to spin up, slow to read, and slow to swap out. They were nonetheless necessary in the days when hard disks were still low on storage, and&amp;nbsp;expensive.&lt;/p&gt;
&lt;p&gt;As hard disks increased in capacity and decreased in price, it made sense to copy the information from these diskettes or compact discs into the hard disk, and subsequently access the data from disk. This process is what we know as &lt;strong&gt;software installation&lt;/strong&gt; (bet you saw that one&amp;nbsp;coming!).&lt;/p&gt;
&lt;p&gt;These days, the disk-is-faster assumption is less true across a spectrum of uses. Especially when it comes to mobile&amp;nbsp;devices.&lt;/p&gt;
&lt;h2&gt;Mobile storage and&amp;nbsp;internet&lt;/h2&gt;
&lt;p&gt;A quick note on mobile storage, which I unfortunately missed out on &lt;a href="https://ngjunsiang.github.io/laymansguide/issue119.html"&gt;Issue 119&lt;/a&gt;) on laptop and desktop solid-state disks&amp;nbsp;(SSDs).&lt;/p&gt;
&lt;p&gt;Mobile devices generally do not use the same kinds of SSDs that laptops use. Those are bulkier, use more power (not good for mobile battery life), and run hotter. Laptop SSDs use an interface called NVMe (which iPhones use as well), while most Android devices’ storage use an interface called &lt;span class="caps"&gt;UFS&lt;/span&gt; which is slower (but uses less&amp;nbsp;energy).&lt;/p&gt;
&lt;p&gt;At the same time, internet access on phones is speeding up. 4G/5G technology has increased throughput, while maintaining more or less the same latency. On the other hand, with the cloud becoming a staple of everyday life, tech companies have poured immense resources into increasing their servers’ responsiveness, resulting in lower latency for internet&amp;nbsp;access.&lt;/p&gt;
&lt;p&gt;Disk caching is no longer&amp;nbsp;king.&lt;/p&gt;
&lt;h2&gt;Caches on mobile&amp;nbsp;devices&lt;/h2&gt;
&lt;p&gt;On Android, a tip not mentioned in the previous issue is to &lt;a href="https://www.howtogeek.com/183004/why-android-phones-slow-down-over-time-and-how-to-speed-them-up/"&gt;clear the app cache&lt;/a&gt;. For many apps, the app cache can grow to tens or hundreds of megabytes. As Android device storage slows down over time, the app cache gets slower and no longer serves to speed up the app. So clearing the cache regularly can help keep performance from degrading too&amp;nbsp;much.&lt;/p&gt;
&lt;p&gt;However, Android itself uses caches for system processes as well, and these are usually not accessible to the user. That means the only practical way for most users to clear this cache is usually to perform a factory&amp;nbsp;reset.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Caches speed up app operations by storing temporary data on the device’s storage. This assumes that access to storage is much faster than access to the file’s original source. On Android, users can clear an app’s cache, but not the system&amp;nbsp;cache.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 167: Database&amp;nbsp;fragmentation&lt;/p&gt;
&lt;p&gt;This issue, I explained why caches no longer work as well as&amp;nbsp;expected.&lt;/p&gt;
&lt;p&gt;Next issue, let’s poke a little deeper: why does clearing the cache&amp;nbsp;work?&lt;/p&gt;</content><category term="Season 13"></category><category term="cache"></category></entry><entry><title>Issue 165: The myths of system slowdown</title><link href="https://ngjunsiang.github.io/laymansguide/issue165.html" rel="alternate"></link><published>2022-03-26T08:00:00+08:00</published><updated>2022-03-26T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-03-26:/laymansguide/issue165.html</id><summary type="html">&lt;p&gt;There are easy and quick ways to check the validity of the most common advice for resolving system slowdown. But it still seems to happen even after these tips have been&amp;nbsp;tried.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Linux software is distributed through Linux distros. The maintainers of distros maintain repositories of software that have been tested with the distro. Most users will access software in the distro’s repositories through a program called a package manager. So users have full control over when updates and new software should be&amp;nbsp;installed.&lt;/p&gt;
&lt;p&gt;Once your laptop hits the magical 1-year window, it somehow seems to … get slower. And slower. Everything takes just a fraction longer. What used to happen near-instantaneously now seems to take a split-second pause. The loading spinner animation feels like it plays just a little longer. And it just gets worse from there with&amp;nbsp;age.&lt;/p&gt;
&lt;p&gt;Google search results have a number of things to say about why it&amp;nbsp;happens:&lt;/p&gt;
&lt;h2&gt;Programs starting up when&amp;nbsp;booting&lt;/h2&gt;
&lt;p&gt;This is primarily an issue when you are booting up your laptop (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue112.html"&gt;Issue 112&lt;/a&gt;)) and logging in. Once you log in to your operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;), your &lt;span class="caps"&gt;OS&lt;/span&gt; will run the startup programs (which you can disable), so if you are trying to use your laptop right after logging in, this may cause some&amp;nbsp;slowdown.&lt;/p&gt;
&lt;p&gt;If your laptop is still slow about 10 minutes after &lt;span class="caps"&gt;OS&lt;/span&gt; login, this is probably not the&amp;nbsp;cause.&lt;/p&gt;
&lt;h2&gt;Programs running in&amp;nbsp;background&lt;/h2&gt;
&lt;p&gt;You can check this easily: open Task Manager (in Windows), and see if &lt;span class="caps"&gt;CPU&lt;/span&gt;, Memory, or Disk are significantly high. You can click on those columns to put the highest-usage processes at the top. Often it is some kind of antivirus or malware scanner that is hitting the disk and causing things to be slow. Wait for these programs to finish (if legitimate), then see if your computer still feels&amp;nbsp;slow.&lt;/p&gt;
&lt;h2&gt;Insufficient&amp;nbsp;memory&lt;/h2&gt;
&lt;p&gt;This is easy to check in Task Manager too. On the performance charts on the right, see if memory usage is near 100%. If it is, try closing some applications until usage drops below 100%, and see if system performance improves after a few&amp;nbsp;minutes.&lt;/p&gt;
&lt;h2&gt;Malware&lt;/h2&gt;
&lt;p&gt;Malware that slows down your laptop usually does so by taking up a lot of &lt;span class="caps"&gt;CPU&lt;/span&gt; time, or using a lot of memory. You can test this using the above tip&amp;nbsp;too.&lt;/p&gt;
&lt;p&gt;If your computer fan is spinning noisily even though &lt;span class="caps"&gt;CPU&lt;/span&gt;/Memory/Disk are low, it’s either malware that is smart enough to hide itself, or blocked computer vent (which is causing your &lt;span class="caps"&gt;CPU&lt;/span&gt; fan to be ineffective). These are easy checks to make&amp;nbsp;too.&lt;/p&gt;
&lt;h2&gt;&lt;span class="caps"&gt;OS&lt;/span&gt;&amp;nbsp;updates&lt;/h2&gt;
&lt;p&gt;Just update your &lt;span class="caps"&gt;OS&lt;/span&gt;. Most of the time this does not fix system&amp;nbsp;slowness.&lt;/p&gt;
&lt;h2&gt;Disk&amp;nbsp;full&lt;/h2&gt;
&lt;p&gt;Open My Computer (in Windows), which should show how full your system partition is. If it is below 80% full, this probably isn’t the issue, regardless of whether your system is on a hard disk or solid state disk (&lt;span class="caps"&gt;SSD&lt;/span&gt;, see &lt;a href="https://ngjunsiang.github.io/laymansguide/issue119.html"&gt;Issue 119&lt;/a&gt;)).&lt;/p&gt;
&lt;h2&gt;File&amp;nbsp;fragmentation&lt;/h2&gt;
&lt;p&gt;You can run Drive Optimizer on your system partition. Yes, there is a lot of discussion about how defragmenting an &lt;span class="caps"&gt;SSD&lt;/span&gt; is bad; fortunately Drive Optimizer is pretty smart about this and will only carry out defragmentation on hard disks; the option is called Optimize if your system partition is on a solid state&amp;nbsp;disk.&lt;/p&gt;
&lt;p&gt;This usually helps a little, especially if the hard disk is quite badly fragmented. If it is only lightly fragmented, or if you are using a solid state disk, this usually won’t resolve any&amp;nbsp;slowdown.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;These are some quick ways to check on the validity of these myths about the causes of system slowdown. In many cases they do help! But there seems to be this general malaise that affects even the most cared-for and most well-maintained laptop, after an operating system has been used for years without a&amp;nbsp;reinstall.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; There are easy and quick ways to check the validity of the most common advice for resolving system slowdown. But it still seems to happen even after these tips have been&amp;nbsp;tried.&lt;/p&gt;
&lt;p&gt;Knowing what we know, this far into this layman’s guide, I’ll look into some other possible causes next&amp;nbsp;issue!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 166: A cause of system slowdown:&amp;nbsp;caches&lt;/p&gt;</content><category term="Season 13"></category><category term="memory"></category></entry><entry><title>Issue 164: Linux, the universal operating system</title><link href="https://ngjunsiang.github.io/laymansguide/issue164.html" rel="alternate"></link><published>2022-03-19T08:00:00+08:00</published><updated>2022-03-19T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-03-19:/laymansguide/issue164.html</id><summary type="html">&lt;p&gt;Linux software is distributed through Linux distros. The maintainers of distros maintain repositories of software that have been tested with the distro. Most users will access software in the distro’s repositories through a program called a package manager. So users have full control over when updates and new software should be&amp;nbsp;installed.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Software that we use usually comes from the &lt;span class="caps"&gt;OS&lt;/span&gt; makers, or from third-party developers. These two groups of developers are not the same, and might even have conflicting intentions and&amp;nbsp;goals.&lt;/p&gt;
&lt;p&gt;Last issue, we looked at the following categories of software that an end-user might&amp;nbsp;need:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;System&amp;nbsp;updates&lt;/li&gt;
&lt;li&gt;Software by the &lt;span class="caps"&gt;OS&lt;/span&gt; maker (first-party&amp;nbsp;software)&lt;/li&gt;
&lt;li&gt;Software from other developers (third-party&amp;nbsp;software)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In general, “trusted” software comes from other sources: compact discs or the internet. While “trusted” software comes from a central, authorised source: usually some kind of app&amp;nbsp;store.&lt;/p&gt;
&lt;p&gt;This leads to a lot of conflict over who gatekeeps the list of “trusted” software. This privilege gives app store owners a lot of power, which they claim to earn by investing capital into making the &lt;span class="caps"&gt;OS&lt;/span&gt; possible, and implementing screening and filtering processes to ensure only secure software makes its way into the&amp;nbsp;list.&lt;/p&gt;
&lt;p&gt;I will not contest those claims here, but instead invite you to consider: what if it were possible for other app store makers to join in curating lists of software for that operating&amp;nbsp;system?&lt;/p&gt;
&lt;h2&gt;What is&amp;nbsp;Linux?&lt;/h2&gt;
&lt;p&gt;Today the term “Linux” refers to a lot of things, which is why we need to clarify here: the term originally referred to &lt;a href="https://www.redhat.com/en/topics/linux/what-is-the-linux-kernel"&gt;the Linux kernel&lt;/a&gt;, which is the core of the operating&amp;nbsp;system.&lt;/p&gt;
&lt;p&gt;The kernel by itself doesn’t do anything without all the other programs that make it actually useable by an informed user: to create, edit, and browse files and folders, run service programs, etc. This collection of programs, along with the kernel itself, is &lt;a href="https://www.getgnulinux.org/en/linux"&gt;officially referred to as &lt;span class="caps"&gt;GNU&lt;/span&gt;/Linux&lt;/a&gt; (but s’okay, you can just say “Linux” and we understand you mean &lt;span class="caps"&gt;GNU&lt;/span&gt;/Linux).&lt;/p&gt;
&lt;p&gt;So, &lt;span class="caps"&gt;GNU&lt;/span&gt;/Linux is similar to Windows and MacOS: they are &lt;em&gt;base operating systems&lt;/em&gt;, capable of doing stuff but not actually useful&amp;nbsp;yet.&lt;/p&gt;
&lt;p&gt;Where all our software&amp;nbsp;at?&lt;/p&gt;
&lt;h2&gt;Software for&amp;nbsp;Linux&lt;/h2&gt;
&lt;p&gt;In the very early days, because there were a number of different hardware configurations that weren’t as compatible as they are now, Linux software was distributed as source code. You downloaded a zip file containing programming code, you ran a compiler program to compile the code (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue054.html"&gt;Issue 54&lt;/a&gt;)) into an executable program. Then there is usually an &lt;strong&gt;installer&lt;/strong&gt;, a shell script that puts the compiled program in the correct place so the &lt;span class="caps"&gt;OS&lt;/span&gt; can find it, and creates other helper files (like configuration&amp;nbsp;information).&lt;/p&gt;
&lt;p&gt;And then you had to check their website (or even email them, in the days before the mainstream Internet) regularly to see if there are any bugfixes, and if yes, you downloaded the &lt;em&gt;new&lt;/em&gt; source code and repeated the process&amp;nbsp;…&lt;/p&gt;
&lt;p&gt;This was obviously tedious, so people started to compile the useful programs into their own list. This effort expanded, and became automated, so that it was not only a list, it was a collection of different versions of compiled programs for different CPUs (each version of the program is called a &lt;strong&gt;package&lt;/strong&gt;). If you were a developer for one of the programs in that list, once you made a new version of your package you could submit it to these guys, they would incorporate the required changes and then add a listing for the newest&amp;nbsp;version.&lt;/p&gt;
&lt;p&gt;These projects came to be called &lt;strong&gt;Linux distributions&lt;/strong&gt;, or &lt;strong&gt;Linux distros&lt;/strong&gt; in short. Distros maintained &lt;strong&gt;repositories&lt;/strong&gt; of software for their specific distribution—the people involved are called &lt;strong&gt;maintainers&lt;/strong&gt;. Maintainers check new versions of software to ensure that it will work as expected on their distro. Another category of software, called &lt;strong&gt;package managers&lt;/strong&gt;, were created for users to be able to use these repositories easily: to check for updates, install them, and perform any other required&amp;nbsp;maintenance.&lt;/p&gt;
&lt;p&gt;More and more distros started in the 80s and 90s, as groups of Linux users and developers decided to branch off based on differing principles and philosophies for managing a Linux computer and its software. Today, you have distros focused on reliability and stability, distros focused on simplicity, distros focused on user-friendliness, distros focused on scientific computing, distros focused on hackability,&amp;nbsp;…&lt;/p&gt;
&lt;p&gt;It is important to note that the Linux kernel development team itself does not maintain any distros. Any updates to the kernel are for distro maintainers to incorporate into their respective&amp;nbsp;repositories.&lt;/p&gt;
&lt;h2&gt;Linux software&amp;nbsp;distribution&lt;/h2&gt;
&lt;p&gt;This means that for the bulk of users, software distribution on Linux is &lt;strong&gt;centred around the distro’s repository&lt;/strong&gt;. Through the package manager, users can search for software, install it, and update it. They can also add the URLs of external repositories to access software from&amp;nbsp;them.&lt;/p&gt;
&lt;p&gt;At the same time, if users wish, they are still able to download compiled executables from the internet and run them (with the usual caveats, of course). They can also download source code, compile it themselves, and then run it. These options do not offer the same ease of maintainability as software installed through a package manager, since there is no repository to check for&amp;nbsp;updates.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Steam website showing Install Steam button for Linux. A popup asks what should Firefox do with the file, steam_latest.deb" src="https://ngjunsiang.github.io/laymansguide/issue164_01.png" /&gt;&lt;br /&gt;
&lt;em&gt;This is what many install pages look like on Linux, for software with Linux versions.&lt;br /&gt;The Install button usually downloads a compiled executable, which can be run on the computer.&lt;br /&gt;But installing through the distro’s package manager is&amp;nbsp;recommended.&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;No system updates sneaking up on you from the &lt;span class="caps"&gt;OS&lt;/span&gt; maker; updates and new software all come from the repository through the package manager. Unless you decide&amp;nbsp;otherwise.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Linux software is distributed through Linux distros. The maintainers of distros maintain repositories of software that have been tested with the distro. Most users will access software in the distro’s repositories through a program called a package manager. So users have full control over when updates and new software should be&amp;nbsp;installed.&lt;/p&gt;
&lt;p&gt;This is as far as I’ll go for technical detail on Linux. I meant this to pick up from &lt;a href="https://ngjunsiang.github.io/laymansguide/issue092.html"&gt;Season 8&lt;/a&gt;) on apps. It’s easier to go into the bird’s-eye view of how this works on Linux, because I’m more familiar with it; on the Windows and MacOS side of things it tends to be more esoteric and&amp;nbsp;proprietary.&lt;/p&gt;
&lt;p&gt;And it’s instructive to know this because … so far this season, we have been talking about distribution—content, code, and software. Notice how all of them involve infrastructure: worldwide clusters of servers for content distribution, a history of code changes for code distribution (and collaboration), and now we have repositories and package managers for software&amp;nbsp;distribution.&lt;/p&gt;
&lt;p&gt;What happens as this infrastructure&amp;nbsp;ages?&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 165: The myths of system&amp;nbsp;slowdown&lt;/p&gt;
&lt;p&gt;We won’t dive into topics as broad as digital infrastructure aging; that’s beyond the scope for a layperson’s newsletter I&amp;nbsp;think!&lt;/p&gt;
&lt;p&gt;Let’s bring it back to personal scale: what happens as your system ages? The predominant symptom that manifests itself is general slowdown: your computer takes longer to switch on, open any app, save any file, and even to shut down. For years I have been googling for reasons why this happens, and satisfying/useful/sensible answers are almost non-existent. The pithy, vague answers about background services (“bloatware”), outdated apps, malware etc are almost insulting; I have relatives who use the same handful of apps, and even after clearing old/large files their phone is still&amp;nbsp;slow!&lt;/p&gt;
&lt;p&gt;Next issue, I attempt to look into these stated reasons and see if they make&amp;nbsp;sense.&lt;/p&gt;</content><category term="Season 13"></category></entry><entry><title>Issue 163: System &amp; software ecosystems</title><link href="https://ngjunsiang.github.io/laymansguide/issue163.html" rel="alternate"></link><published>2022-03-12T08:00:00+08:00</published><updated>2022-03-12T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-03-12:/laymansguide/issue163.html</id><summary type="html">&lt;p&gt;Software that we use usually comes from the &lt;span class="caps"&gt;OS&lt;/span&gt; makers, or from third-party developers. These two groups of developers are not the same, and might even have conflicting intentions and&amp;nbsp;goals.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Typeface families consist of multiple fonts for each style in the typeface. Each font consists of glyphs, which are mathematical shapes described by curves joining points. These shapes need to be rasterised for display on a computer screen, or for printing on paper. Font files usually come&amp;nbsp;in &lt;code&gt;.ttf&lt;/code&gt;, &lt;code&gt;.otf&lt;/code&gt;,&amp;nbsp;or &lt;code&gt;.woff&lt;/code&gt; formats.&lt;/p&gt;
&lt;p&gt;Brief recap of the past few&amp;nbsp;issues:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Content distribution:&lt;/strong&gt; Images and other media are distributed with the help of content distribution networks (CDNs, &lt;a href="https://ngjunsiang.github.io/laymansguide/issue160.html"&gt;Issue 160&lt;/a&gt;)), which have regional servers closer to&amp;nbsp;users.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Code distribution:&lt;/strong&gt; Webpage documents and web scripts (in Javascript) are distributed from the host server (which may comprise more than one&amp;nbsp;computer).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And all of these takes place over the World Wide Web, often through the &lt;span class="caps"&gt;HTTP&lt;/span&gt; protocol (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue007.html"&gt;Issue 7&lt;/a&gt;)). That is how data gets to us when we use the&amp;nbsp;internet.&lt;/p&gt;
&lt;p&gt;What about the software we use, and the oft-dreaded Windows Updates? How does that get to&amp;nbsp;us?&lt;/p&gt;
&lt;h2&gt;Software distribution on Windows and&amp;nbsp;MacOS&lt;/h2&gt;
&lt;p&gt;Okay there’s not actually anything new to say here, just checking if we have the same understanding of how to find new&amp;nbsp;software:&lt;/p&gt;
&lt;p&gt;You’ve got your system updates and whatnot, inconveniences that sometimes force themselves on you. These generally come from a secure server from the operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;) maker, which is Microsoft or&amp;nbsp;Apple.&lt;/p&gt;
&lt;p&gt;You’ve got software made by Microsoft/Apple, which you either download from their website or install through your&amp;nbsp;browser.&lt;/p&gt;
&lt;p&gt;Then there’s the software in their app stores. These app stores are listings of software which developers pay to have their software listed in. A big selling point of app stores is their supposed security: app stores usually have a screening process to ensure that submitted apps are not doing Evil Things™ which harm their users. So when you download an app it is assumed that this app has passed some kind of rigorous screening&amp;nbsp;process.&lt;/p&gt;
&lt;p&gt;Developers often pay a proportion of their revenue to the app stores for this&amp;nbsp;“privilege”.&lt;/p&gt;
&lt;p&gt;And then there is … all the other software you can download from the&amp;nbsp;internet.&lt;/p&gt;
&lt;p&gt;I’m guessing the internet is where most of the desktop/laptop software you use comes&amp;nbsp;from.&lt;/p&gt;
&lt;h2&gt;Software distribution on Android and&amp;nbsp;iPhone&lt;/h2&gt;
&lt;p&gt;On the mobile side of things, it looks remarkably similar, but with the weightage somewhat&amp;nbsp;different.&lt;/p&gt;
&lt;p&gt;System updates, coming from the &lt;span class="caps"&gt;OS&lt;/span&gt; maker —&amp;nbsp;check.&lt;/p&gt;
&lt;p&gt;Software made by the &lt;span class="caps"&gt;OS&lt;/span&gt; maker — check, but coming through app store instead of&amp;nbsp;internet.&lt;/p&gt;
&lt;p&gt;App stores — check, still the&amp;nbsp;same.&lt;/p&gt;
&lt;p&gt;Software from the internet — Android allows installing software from “unknown sources”, but you’ll have to enable a system setting to allow that. It is off by default. On iPhone, this is just not possible.&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;I’m guessing the app store is where most of the mobile apps you use come&amp;nbsp;from.&lt;/p&gt;
&lt;h2&gt;Software as an&amp;nbsp;ecosystem&lt;/h2&gt;
&lt;p&gt;Notice that in neither case is finding software like foraging for berries: there is an entire ecosystem that goes into making these actions&amp;nbsp;possible!&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;&lt;span class="caps"&gt;OS&lt;/span&gt; makers&lt;/strong&gt; obviously have to distribute their &lt;span class="caps"&gt;OS&lt;/span&gt;, supporting software, and updates reliably and securely, so that other software can rely on its continued existence. An &lt;span class="caps"&gt;OS&lt;/span&gt; by itself doesn’t usually do much for users; they need software to create and manage their files, and access the internet. And much of this software is going to be created by other developers, not the &lt;span class="caps"&gt;OS&lt;/span&gt;&amp;nbsp;makers.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;developers&lt;/strong&gt; need documentation and sample code to understand how to write the software, and these usually come from the &lt;span class="caps"&gt;OS&lt;/span&gt; makers. But it can also come from a thriving community of other developers who are writing software for the same&amp;nbsp;system.&lt;/p&gt;
&lt;p&gt;And then there are the &lt;strong&gt;discovery mechanisms&lt;/strong&gt; that users need to find useful software; Google is the fallback when this doesn’t exist, but you usually want users to have a better experience than googling for installers and potentially installing&amp;nbsp;malware.&lt;/p&gt;
&lt;p&gt;Even this understanding is incomplete, but it’s the beginning of a more nuanced model of software development that will help you understand why software often does not do what it should. We are talking about factions in software&amp;nbsp;development.&lt;/p&gt;
&lt;p&gt;Far too often I see users who have no awareness of this divide, and seem to operate on an assumption that software comes from “programmers”, a hallowed, unreachable group of entities that blesses users with features or curses them with bugs at their&amp;nbsp;whim.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Software that we use usually comes from the &lt;span class="caps"&gt;OS&lt;/span&gt; makers, or from third-party developers. These two groups of developers are not the same, and might even have conflicting intentions and&amp;nbsp;goals.&lt;/p&gt;
&lt;p&gt;If I keep going on in this vein I’m going to bring in politics, and I don’t want to do that. Instead, I’ll introduce a slightly different kind of software ecosystem, which non-Linux users are likely not familiar with. My hope is for you to see that *handwaves at above paragraphs* &lt;em&gt;this&lt;/em&gt; isn’t the only way for software distribution to&amp;nbsp;work!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 164: Linux, the universal operating&amp;nbsp;system&lt;/p&gt;
&lt;p&gt;What does a model look like when there is no central app store, controlled by the &lt;span class="caps"&gt;OS&lt;/span&gt; makers? That is how the Linux distribution system&amp;nbsp;works!&lt;/p&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Okay not true, you can replace the &lt;span class="caps"&gt;OS&lt;/span&gt; on your phone through a process called &lt;strong&gt;jailbreaking&lt;/strong&gt;, but this is a layperson’s newsletter and I do not recommend this without much more extensive reading and careful consideration.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 13"></category></entry><entry><title>Issue 162: Fonts</title><link href="https://ngjunsiang.github.io/laymansguide/issue162.html" rel="alternate"></link><published>2022-03-05T08:00:00+08:00</published><updated>2022-03-05T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-03-05:/laymansguide/issue162.html</id><summary type="html">&lt;p&gt;Typeface families consist of multiple fonts for each style in the typeface. Each font consists of glyphs, which are mathematical shapes described by curves joining points. These shapes need to be rasterised for display on a computer screen, or for printing on paper. Font files usually come&amp;nbsp;in &lt;code&gt;.ttf&lt;/code&gt;, &lt;code&gt;.otf&lt;/code&gt;,&amp;nbsp;or &lt;code&gt;.woff&lt;/code&gt; formats.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Cross-site scripting attacks occur when a webpage loads malicious code from a third-party, usually carried out by a script in the page. Today, websites are protected from loading unauthorised scripts through cross-origin resource sharing (&lt;span class="caps"&gt;CORS&lt;/span&gt;) policy implemented in browsers, which only allows a website to load scripts from authorised&amp;nbsp;domains.&lt;/p&gt;
&lt;p&gt;This is the issue that doesn’t really fit anywhere, but this season is about lots of things we take for granted and fonts are one of&amp;nbsp;them.&lt;/p&gt;
&lt;p&gt;I could probably fill at least half a season talking about fonts and typesetting, but let’s stick to the basics&amp;nbsp;here.&lt;/p&gt;
&lt;h2&gt;What is a&amp;nbsp;font?&lt;/h2&gt;
&lt;p&gt;I’m going with &lt;a href="https://fonts.adobe.com/fonts/source-sans"&gt;Source Sans&lt;/a&gt;, an open-source &lt;strong&gt;typeface&lt;/strong&gt; designed in-house by Adobe. Let’s open one up one of its &lt;strong&gt;font&lt;/strong&gt;s&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;, Source Sans Pro Regular, in a font editor&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt; and&amp;nbsp;see:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Fontforge with Source Sans Pro Regular open, showing glyphs" src="https://ngjunsiang.github.io/laymansguide/issue162_01.png" /&gt;&lt;br /&gt;
&lt;em&gt;Source Sans Pro Regular, in&amp;nbsp;Fontforge.&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;You might already be aware that fonts contain &lt;strong&gt;glyphs&lt;/strong&gt;, which are the shapes of each separate character. What makes up those&amp;nbsp;glyphs?&lt;/p&gt;
&lt;p&gt;&lt;img alt="Uppercase Q from Source Sans Pro Regular" src="https://ngjunsiang.github.io/laymansguide/issue162_02.png" /&gt;&lt;br /&gt;
&lt;em&gt;Uppercase Q from Source Sans Pro&amp;nbsp;Regular.&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;These glyphs are mathematical shapes. They are stored as a series of points, joined by curves&lt;sup id="fnref:3"&gt;&lt;a class="footnote-ref" href="#fn:3"&gt;3&lt;/a&gt;&lt;/sup&gt; or straight lines&lt;sup id="fnref:4"&gt;&lt;a class="footnote-ref" href="#fn:4"&gt;4&lt;/a&gt;&lt;/sup&gt;, which are stored as numbers. The diagrams we draw in Microsoft Word are much the&amp;nbsp;same!&lt;/p&gt;
&lt;h2&gt;Font&amp;nbsp;variants&lt;/h2&gt;
&lt;p&gt;When you bold or italicise text, the operating system swaps in the appropriate glyphs from the bold or italic fonts from the same typeface family. A lot of careful work goes into ensuring that these fonts are recognised as belonging to the same typeface, or this feature would not&amp;nbsp;work.&lt;/p&gt;
&lt;p&gt;This means that when you install fonts, remember to grab the bold and italic fonts as&amp;nbsp;well!&lt;/p&gt;
&lt;p&gt;Many applications, if they are unable to find the bold/italic fonts, will artificially “bold” or “italicise” the regular font by thickening the glyph, or slanting it. Any graphic designer worth their salt will notice this immediately; even without scrutinising the font, it will feel “off” in some vaguely inscrutable way until you take a closer look and notice the proportions are&amp;nbsp;wrong.&lt;/p&gt;
&lt;h2&gt;Font&amp;nbsp;display&lt;/h2&gt;
&lt;p&gt;For an application to be able to support text formatting, it must have a program called a text rendering engine. This program takes a single long string of text and determines the appropriate places to chop it up with line breaks. To do that, it first needs to convert the glyph shapes from mathematical formulas into actual real lit pixels or real inky droplets (in a process called &lt;strong&gt;rasterisation&lt;/strong&gt;, &lt;a href="https://ngjunsiang.github.io/laymansguide/issue122.html"&gt;Issue 122&lt;/a&gt;)).&lt;/p&gt;
&lt;p&gt;Then their dimensions have to be considered in the line of text, to know where the line breaks should be placed. Because glyphs are not actually rectangular boxes and they protrude in different ways, optically they need some horizontal adjustment (called &lt;strong&gt;kerning&lt;/strong&gt;) to look evenly spaced optically, so that needs to be done too&lt;sup id="fnref:5"&gt;&lt;a class="footnote-ref" href="#fn:5"&gt;5&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Kerning for some common glyph pairs in Source Sans Pro Regular" src="https://ngjunsiang.github.io/laymansguide/issue162_03.png" /&gt;
&lt;small&gt;Kerning for some common glyph pairs in Source Sans Pro Regular.&lt;br /&gt;&lt;br /&gt;
A kern value of 0 or blank means no kerning is required. A negative value means the letters need to be brought closer, and a positive value means they need to be spaced further (quite rare).&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Advanced renderers might even do other things, like avoiding too many terminal hyphens on consecutive lines (looks ugly), or making microadjustments to letter spacing. But, oops! This changes the line length, so the engine needs to go back to re-check the line breaks. This is an iterative&amp;nbsp;process.&lt;/p&gt;
&lt;p&gt;Text rendering engines are an art in themselves, and we are not going to go in depth&amp;nbsp;here.&lt;/p&gt;
&lt;h2&gt;Font&amp;nbsp;formats&lt;/h2&gt;
&lt;p&gt;The classic file extension&amp;nbsp;is &lt;code&gt;.ttf&lt;/code&gt;, which stands for Truetype font, a font format created by Apple in the late 1980s and subsequently adopted by other&amp;nbsp;systems.&lt;/p&gt;
&lt;p&gt;These days, you might also&amp;nbsp;see &lt;code&gt;.otf&lt;/code&gt;, which stands for Opentype font. This is a more modern font format, co-developed in the mid-90s by Microsoft and Adobe. This adds much more functionality and new features, which after some deliberation I have decided not to write about—it is simply not a layperson&amp;nbsp;topic!&lt;/p&gt;
&lt;p&gt;If you do web development, you might also&amp;nbsp;see &lt;code&gt;.woff&lt;/code&gt;, the Web Open Font Format, co-developed by Mozilla and other type organisations. It shares some features in common with &lt;span class="caps"&gt;TTF&lt;/span&gt; and &lt;span class="caps"&gt;OTF&lt;/span&gt;, but adds other features for licensing information, which is usually more important for the web, where these font files need to be downloaded to the users’&amp;nbsp;computers.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Typeface families consist of multiple fonts for each style in the typeface. Each font consists of glyphs, which are mathematical shapes described by curves joining points. These shapes need to be rasterised for display on a computer screen, or for printing on paper. Font files usually come&amp;nbsp;in &lt;code&gt;.ttf&lt;/code&gt;, &lt;code&gt;.otf&lt;/code&gt;,&amp;nbsp;or &lt;code&gt;.woff&lt;/code&gt; formats.&lt;/p&gt;
&lt;p&gt;This difference in representation vs display, fonts-as-mathematical-shapes vs fonts-as-pixels-or-dots, has been and continues to be the cause of much weeping and gnashing of teeth. But I’ve decided it’s not worth delving into that for a layman’s guide to computing—perhaps in a separate&amp;nbsp;publication!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 163: System &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; software&amp;nbsp;ecosystems&lt;/p&gt;
&lt;p&gt;With this diversion over, let’s return to talking about distribution. Content distribution, code distribution, and next issue I’ll move on to software&amp;nbsp;distribution!&lt;/p&gt;
&lt;p&gt;With this I have also cleared my backlog of questions, and will be closing the below section as&amp;nbsp;well.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;del&gt;OpenType? And what are fonts anyway? [Issue 42]&lt;/del&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;A font is a single style in a typeface family. The full family will usually have regular/bold/italic styles. More advanced typefaces may have small caps, display, and caption fonts.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;The one I use is called &lt;a href="https://fontforge.org"&gt;Fontforge&lt;/a&gt;, and it is open-source.&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:3"&gt;
&lt;p&gt;For the curious, the specific type of curve used in fonts are &lt;a href="https://jdhao.github.io/2018/11/27/font_shape_mathematics_bezier_curves/"&gt;Bézier curves&lt;/a&gt;.&amp;#160;&lt;a class="footnote-backref" href="#fnref:3" title="Jump back to footnote 3 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:4"&gt;
&lt;p&gt;Don’t be alarmed if the mathematicians among us casually remark that a straight line is a trivial example of a curve.&amp;#160;&lt;a class="footnote-backref" href="#fnref:4" title="Jump back to footnote 4 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:5"&gt;
&lt;p&gt;The kerning information is created by the font designer and embedded in the font file. Applications usually pass this information to the text rendering engine, although some might not actually use it.&amp;#160;&lt;a class="footnote-backref" href="#fnref:5" title="Jump back to footnote 5 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 13"></category></entry><entry><title>Issue 161: Security and XSS</title><link href="https://ngjunsiang.github.io/laymansguide/issue161.html" rel="alternate"></link><published>2022-02-26T08:59:00+08:00</published><updated>2022-02-26T08:59:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-02-26:/laymansguide/issue161.html</id><summary type="html">&lt;p&gt;Cross-site scripting attacks occur when a webpage loads malicious code from a third-party, usually carried out by a script in the page. Today, websites are protected from loading unauthorised scripts through cross-origin resource sharing (&lt;span class="caps"&gt;CORS&lt;/span&gt;) policy implemented in browsers, which only allows a website to load scripts from authorised&amp;nbsp;domains.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; A content delivery network comprises multiple servers around the world that are able to quickly distribute static content (typically images and video) to viewers that request it. This avoids overloading the hosting server, which would otherwise have to serve data over the network, possibly through many intermediary&amp;nbsp;hops.&lt;/p&gt;
&lt;p&gt;When you load a modern webpage with all its bells and whistles, it is usually loading its content from a content delivery network (&lt;span class="caps"&gt;CDN&lt;/span&gt;; see previous issue). At the same time, it is running scripts that came with the webpage. These scripts may load other scripts on the same server (first-party scripts), or scripts on other servers (third-party&amp;nbsp;scripts).&lt;/p&gt;
&lt;p&gt;What could go&amp;nbsp;wrong?&lt;/p&gt;
&lt;h2&gt;First-party&amp;nbsp;scripts&lt;/h2&gt;
&lt;p&gt;These are scripts you trust, because you host them on your own server (or a server you administer). Ideally you also have network security measures in place and other ways to ensure those scripts are not modified by malicious&amp;nbsp;actors.&lt;/p&gt;
&lt;p&gt;It’s usually safe to load them in the webpage because they are from the same site (i.e. same&amp;nbsp;server).&lt;/p&gt;
&lt;h2&gt;Third-party&amp;nbsp;scripts&lt;/h2&gt;
&lt;p&gt;These are scripts that are loaded from a remote server. You’d usually do this to load scripts from service providers: for analytics, to serve online ads, or to use libraries and frameworks (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue017.html"&gt;Issue 17&lt;/a&gt;), &lt;a href="https://ngjunsiang.github.io/laymansguide/issue018.html"&gt;Issue 18&lt;/a&gt;)). This is mightily convenient: as a third-party service provider, you have the flexibility of updating this script and immediately benefiting your client without them having to do anything. Can’t beat that for&amp;nbsp;convenience!&lt;/p&gt;
&lt;p&gt;But once you open the door to third-party scripts, they could be loaded from &lt;em&gt;anywhere&lt;/em&gt;. And without some mechanism for verification, the client won’t actually know if they are loading your script, or someone&amp;nbsp;else’s.&lt;/p&gt;
&lt;h2&gt;Cross-site scripting (&lt;span class="caps"&gt;XSS&lt;/span&gt;)&lt;/h2&gt;
&lt;p&gt;If a webpage is insecurely scripted, e.g. by inserting data directly from a request without verification, a malicious actor might be able to subvert the source of the request and inject malicious code into the page. This malicious code, though sandboxed by the browser, still has access to data that is on the&amp;nbsp;page.&lt;/p&gt;
&lt;p&gt;This is called a &lt;strong&gt;cross-site scripting&lt;/strong&gt;&amp;nbsp;attack.&lt;/p&gt;
&lt;h2&gt;The weakest&amp;nbsp;link&lt;/h2&gt;
&lt;p&gt;The chain of security is only as strong as its weakest link, so even if your own security is tight, a malicious actor would look at your tech stack (the set of hardware/software/services your company uses), notice that your webpage is loading scripts from a third party, and attempt to hack the third-party&amp;#8217;s servers (which might be less secure). When you draw on features from multiple libraries, you are in effect setting up a circle of trust that is only as secure as the least secure library/third-party in your&amp;nbsp;web.&lt;/p&gt;
&lt;h2&gt;Protections&lt;/h2&gt;
&lt;p&gt;This is why all browsers today have &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"&gt;cross-origin resource sharing (&lt;span class="caps"&gt;CORS&lt;/span&gt;)&lt;/a&gt; active by default. This prevents scripts from loading other third-party scripts; only loading of first-party scripts are supported by default. To enable loading of scripts from other sources, your server must include special data in the &lt;span class="caps"&gt;HTTP&lt;/span&gt; response header (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue008.html"&gt;Issue 8&lt;/a&gt;)) that specify these sources explicitly. It’s tedious, but it is much more secure, and it is also why learning web programming is much harder today than it was a few years&amp;nbsp;ago.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Cross-site scripting attacks occur when a webpage loads malicious code from a third-party, usually carried out by a script in the page. Today, websites are protected from loading unauthorised scripts through cross-origin resource sharing (&lt;span class="caps"&gt;CORS&lt;/span&gt;) policy implemented in browsers, which only allows a website to load scripts from authorised&amp;nbsp;domains.&lt;/p&gt;
&lt;p&gt;Ughh, this is already getting more tedious and bureaucratic to write about. As you can see, even in the world of programming there is a load of red-tape to cut through, all to protect our works from malicious actors and ensure there is a chain of authorisation running through everything. I guess there’s a life lesson in here&amp;nbsp;somewhere.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 162:&amp;nbsp;Fonts&lt;/p&gt;
&lt;p&gt;I’ve covered content distribution, code distribution (for the web), and now I think we can go a little wider: let’s talk about software&amp;nbsp;distribution!&lt;/p&gt;
&lt;p&gt;But before that I want to slot in a little issue about fonts: just what are they? And how do they&amp;nbsp;work?&lt;/p&gt;
&lt;p&gt;// Hopefully all my readers have had the experience of searching for and actually downloading software. What kind of system is behind this? And how do system and software updates actually get to our&amp;nbsp;devices?&lt;/p&gt;
&lt;p&gt;Get ready for a deeper&amp;nbsp;dive!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;del&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue 8]&lt;/del&gt;&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 13"></category></entry><entry><title>Issue 160: CDNs and content distribution</title><link href="https://ngjunsiang.github.io/laymansguide/issue160.html" rel="alternate"></link><published>2022-02-19T08:00:00+08:00</published><updated>2022-02-19T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-02-19:/laymansguide/issue160.html</id><summary type="html">&lt;p&gt;A content delivery network comprises multiple servers around the world that are able to quickly distribute static content (typically images and video) to viewers that request it. This avoids overloading the hosting server, which would otherwise have to serve data over the network, possibly through many intermediary&amp;nbsp;hops.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Instead of &lt;span class="caps"&gt;GPS&lt;/span&gt; satellites, smartphones can also use wifi points and cell towers to determine their position (if enabled in the &lt;span class="caps"&gt;OS&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;All businessmen know that distribution is everything. How good your product is, is secondary to how you get your product to the customer. This act of getting things to your customer—it’s called distribution, and entire businesses have been built around excellent&amp;nbsp;distribution.&lt;/p&gt;
&lt;p&gt;In &lt;a href="https://ngjunsiang.github.io/laymansguide/issue157.html"&gt;Issue 157&lt;/a&gt;), I described how time is synchronised from time source to server and on to other servers, down the strata of the hierarchy tree of time servers. Whereas &lt;span class="caps"&gt;GPS&lt;/span&gt;/wifi location (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue158.html"&gt;Issue 158&lt;/a&gt;)) has a much shallower distribution system: everybody gets their location directly from a &lt;span class="caps"&gt;GPS&lt;/span&gt; satellite if there’s nothing else available, otherwise they get it from the nearest wifi point or cell&amp;nbsp;tower.&lt;/p&gt;
&lt;p&gt;What about&amp;nbsp;content?&lt;/p&gt;
&lt;h2&gt;The difficulties of&amp;nbsp;content&lt;/h2&gt;
&lt;p&gt;You make a website, type in the headers and body text, upload the images and videos … and it just works&amp;nbsp;right?&lt;/p&gt;
&lt;p&gt;Let’s think through the &lt;strong&gt;distribution&lt;/strong&gt; of that content. Text is generally small in size and easy to pass around, even through multiple hops (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue036.html"&gt;Issue 36&lt;/a&gt;)) from server to&amp;nbsp;client.&lt;/p&gt;
&lt;h3&gt;Server&amp;nbsp;load&lt;/h3&gt;
&lt;p&gt;What about the heavy stuff, like hi-res images and videos? Thousands or millions of clients all requesting the same large video file from your hosting server. That server is going to be spending many &lt;span class="caps"&gt;CPU&lt;/span&gt; cycles (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue058.html"&gt;Issue 58&lt;/a&gt;)) receiving requests, retrieving the data, splitting and encapsulating it into data packets to be sent out. All that processing adds to the server load. If there are too many clients waiting for the same data &amp;#8230; they gonna wait. And that adds to latency (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue036.html"&gt;Issue 36&lt;/a&gt;)); those viewers are going to be seeing loading spinners for a&amp;nbsp;while.&lt;/p&gt;
&lt;p&gt;Some of that processing can be mitigated with techniques such as caching (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue039.html"&gt;Issue 39&lt;/a&gt;)), but not enough; you will eventually need to add more&amp;nbsp;servers.&lt;/p&gt;
&lt;h3&gt;Bandwidth and transfer&amp;nbsp;fees&lt;/h3&gt;
&lt;p&gt;Your hosting provider is going to be paying lots of egress fees to transfer your data out of their servers (imagine sending the same &lt;span class="caps"&gt;4GB&lt;/span&gt; video to a few thousand Youtube viewers), and they’ll likely pass on the fees to you as&amp;nbsp;well.&lt;/p&gt;
&lt;h3&gt;Latency&amp;nbsp;again&lt;/h3&gt;
&lt;p&gt;If the client is geographically far away from the server, possibly even on the other side of the world, the data is going to go through a lot of hops from server to server. And if any of the servers along the way drop the packet, it is going to need to be&amp;nbsp;resent.&lt;/p&gt;
&lt;h2&gt;Improving content&amp;nbsp;distribution&lt;/h2&gt;
&lt;p&gt;So how do we lighten the server load on the hosting company’s servers, reduce the amount of data to transfer from that server, and improve latency for the&amp;nbsp;clients?&lt;/p&gt;
&lt;p&gt;You place distribution servers as close as possible to the clients, wherever they may be. This usually means you have your servers globally distributed, with regional&amp;nbsp;clusters.&lt;/p&gt;
&lt;p&gt;You place the most often requested files from that region in its distribution server, so that it can serve those files without the request hitting the hosting server. Because the distribution server is so near the client, the data goes through fewer hops to get to the&amp;nbsp;client.&lt;/p&gt;
&lt;p&gt;The main document data is still served from the hosting provider, so that any changes you make to the page get served to clients almost immediately. Otherwise every little change you make has to be reflected in every distribution server that has a copy of that data. For this reason, distribution servers are typically used for &lt;strong&gt;static data&lt;/strong&gt;: data that doesn’t change frequently, or at all—images and videos&amp;nbsp;especially.&lt;/p&gt;
&lt;h2&gt;A content delivery network (&lt;span class="caps"&gt;CDN&lt;/span&gt;)&lt;/h2&gt;
&lt;p&gt;These distribution servers, along with their supporting infrastructure, are collectively known as a &lt;strong&gt;content delivery network&lt;/strong&gt; (&lt;span class="caps"&gt;CDN&lt;/span&gt;). Sometimes, when you are grabbing the &lt;span class="caps"&gt;URL&lt;/span&gt; of an image, you might see “cdn” in the &lt;span class="caps"&gt;URL&lt;/span&gt; domain—now you know what it&amp;nbsp;means!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; A content delivery network comprises multiple servers around the world that are able to quickly distribute static content (typically images and video) to viewers that request it. This avoids overloading the hosting server, which would otherwise have to serve data over the network, possibly through many intermediary&amp;nbsp;hops.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 161: Security and &lt;span class="caps"&gt;XSS&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;With this piece of info, our mental picture of the loading of a webpage is getting more and more complex. No longer can we assume that the web document itself, its images, videos, and other content are all being loaded from the same server: static content might be coming from a &lt;span class="caps"&gt;CDN&lt;/span&gt;, and other content (e.g. ads) might be loaded from&amp;nbsp;elsewhere.&lt;/p&gt;
&lt;p&gt;Let’s talk about this from a security standpoint next&amp;nbsp;issue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 13"></category></entry><entry><title>Issue 159: Wifi &amp; cell tower location tracking</title><link href="https://ngjunsiang.github.io/laymansguide/issue159.html" rel="alternate"></link><published>2022-02-12T08:00:00+08:00</published><updated>2022-02-12T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-02-12:/laymansguide/issue159.html</id><summary type="html">&lt;p&gt;nstead of &lt;span class="caps"&gt;GPS&lt;/span&gt; satellites, smartphones can also use wifi points and cell towers to determine their position (if enabled in the &lt;span class="caps"&gt;OS&lt;/span&gt;).&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; To get your location using &lt;span class="caps"&gt;GPS&lt;/span&gt;, your phone requests information from four overhead &lt;span class="caps"&gt;GPS&lt;/span&gt; satellites: their location, and the distance between them and your phone. With this information, your phone can calculate its&amp;nbsp;location.&lt;/p&gt;
&lt;p&gt;Okay, so what happens when you are in a tunnel or building and can’t get &lt;span class="caps"&gt;GPS&lt;/span&gt;? How are you still able to use Google Maps to navigate that new sprawl of a&amp;nbsp;mall?&lt;/p&gt;
&lt;h2&gt;Wifi Positioning System (&lt;span class="caps"&gt;WPS&lt;/span&gt;)&lt;/h2&gt;
&lt;p&gt;The principles of triangulation still work within a building, thank math 🙏 but now we need other landmarks to replace &lt;span class="caps"&gt;GPS&lt;/span&gt;&amp;nbsp;satellites.&lt;/p&gt;
&lt;p&gt;What is something with a known (and ideally fixed) location, is electrically powered to receive and respond to signals, and there are enough of them to provide a sufficient number of landmarks for triangulation? If you are in a building with wifi, the wifi access points scattered throughout the building can probably provide&amp;nbsp;this.&lt;/p&gt;
&lt;p&gt;No protocol is involved in &lt;a href="https://en.wikipedia.org/wiki/Wi-Fi_positioning_system"&gt;wifi positioning&lt;/a&gt;, largely because most routers do not carry a precise hardware clock and do not have any way to know their location precisely, and therefore cannot communicate this information meaningfully. Instead, wifi positioning is a collection of techniques for &lt;em&gt;guessing&lt;/em&gt; your location. Your smartphone uses these techniques (usually through its operating system) in conjunction with available wifi networks around you to determine its own&amp;nbsp;location.&lt;/p&gt;
&lt;h2&gt;Wifi positioning&amp;nbsp;techniques&lt;/h2&gt;
&lt;p&gt;One way to figure out your location to proximal wifi points is to use the signal strength as a weak analogue for your distance from them. You can do a very rough position estimate with&amp;nbsp;this.&lt;/p&gt;
&lt;p&gt;Another common technique is to look up the hardware address, or even &lt;span class="caps"&gt;IP&lt;/span&gt; address of the wifi point you are connected to, and just use it directly (with the assumption that wifi signals get too weak outside of a 10 m radius, so you have your location accurate to within ±10&amp;nbsp;m).&lt;/p&gt;
&lt;h2&gt;Wifi location&amp;nbsp;databases&lt;/h2&gt;
&lt;p&gt;One way to keep track of wifi access points and their locations is through a global, public database. &lt;a href="https://en.wikipedia.org/wiki/Wi-Fi_positioning_system#Public_Wi-Fi_location_databases"&gt;A number of these&lt;/a&gt; are available, such as the &lt;a href="https://location.services.mozilla.com/"&gt;Mozilla Location Service&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Cell tower&amp;nbsp;triangulation&lt;/h2&gt;
&lt;p&gt;What happens when you are outdoors, far from any wifi point? As long as you have mobile data enabled and are not in airplane mode, you are still going to be getting your cell signal from a cell tower &amp;#8230; which also meet the three basic criteria for device-based triangulation&amp;nbsp;😉&lt;/p&gt;
&lt;p&gt;Your smartphone can thus triangulate its location from cell towers that it is able to reach. Again, there is no protocol for this, since your smartphone does not communicate with the towers for the express purpose of obtaining location; it is a set of similar techniques, often implemented in the operating&amp;nbsp;system.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Instead of &lt;span class="caps"&gt;GPS&lt;/span&gt; satellites, smartphones can also use wifi points and cell towers to determine their position (if enabled in the &lt;span class="caps"&gt;OS&lt;/span&gt;).&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 160: CDNs and content&amp;nbsp;distribution&lt;/p&gt;
&lt;p&gt;Coincidentally, starting with time turned out to be a good idea: time information is sort of like content. It has an origin, and it gets distributed to “consumers” who want that information. As with all distribution systems, you have cascades of product that flow outward from this&amp;nbsp;origin.&lt;/p&gt;
&lt;p&gt;We have covered time and space (I mean, location). Let’s move on to data: how does data get around the world from a few central&amp;nbsp;sources?&lt;/p&gt;
&lt;p&gt;Back in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue073.html"&gt;Issue 73&lt;/a&gt;), when I explained how online advertising works, I mentioned that advertising content is served from a content delivery network (&lt;span class="caps"&gt;CDN&lt;/span&gt;). What is this and how does it&amp;nbsp;work?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 13"></category></entry><entry><title>Issue 158: GPS</title><link href="https://ngjunsiang.github.io/laymansguide/issue158.html" rel="alternate"></link><published>2022-02-05T08:00:00+08:00</published><updated>2022-02-05T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-02-05:/laymansguide/issue158.html</id><summary type="html">&lt;p&gt;To get your location using &lt;span class="caps"&gt;GPS&lt;/span&gt;, your phone receives information from four overhead &lt;span class="caps"&gt;GPS&lt;/span&gt; satellites: their location, and the distance between them and your phone. With this information, your phone can calculate its&amp;nbsp;location.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Time is synchronised from higher-precision sources through a protocol called Network Time Protocol (&lt;span class="caps"&gt;NTP&lt;/span&gt;). A public pool of time servers is available for synchronisation at&amp;nbsp;pool.ntp.org.&lt;/p&gt;
&lt;p&gt;Ah, &lt;span class="caps"&gt;GPS&lt;/span&gt;. The only topic that actually has almost nothing to do with computing &amp;#8230; and yet the mobile computers in our pocket rely on it so&amp;nbsp;much.&lt;/p&gt;
&lt;h2&gt;A short&amp;nbsp;history&lt;/h2&gt;
&lt;p&gt;The Global Positioning System (&lt;span class="caps"&gt;GPS&lt;/span&gt;) was born of the space age, in 1973, before computers even went mainstream. It was originally used for military applications, particularly for navigation. It was first widely used in a political conflict in the Gulf War (1990–1991). The public finally had access to it in 1996, after &lt;span class="caps"&gt;US&lt;/span&gt; President Bill Clinton issued a policy directive for it to be dual-use (used for both military and civilian&amp;nbsp;purposes).&lt;/p&gt;
&lt;h2&gt;Principles&lt;/h2&gt;
&lt;p&gt;The principle of triangulation far predates &lt;span class="caps"&gt;GPS&lt;/span&gt;. Triangles have been used to estimate distance since antiquity; there is evidence of such techniques being used from sources as far back as 6th century &lt;span class="caps"&gt;BC&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;In essence, if you know the location of two reference points, then with those two reference points and your own location as the third point, you can draw a triangle and solve a little geometry puzzle to figure out your own&amp;nbsp;location.&lt;/p&gt;
&lt;p&gt;If you were hiking or just taking a walk, you need to have at least two landmarks with locations marked on a map. As long as you stop somewhere with good visibility, you can get your bearings to those landmarks on the map, draw a line backward from each landmark following your bearing, and the intersection of the lines from each landmark will show your&amp;nbsp;location.&lt;/p&gt;
&lt;p&gt;But digital compasses are not ubiquitous in all devices yet; typically they are only included in high-end phones. A simpler way for devices to get their location is to estimate their distance to the two landmarks. This they can do using radio waves, which travel at (close to) the speed of light: 300 million metres per second. Provided the landmarks contain devices that can receive this signal and send it back, the time delay can be used to calculate the distance between the device and&amp;nbsp;landmark.&lt;/p&gt;
&lt;h2&gt;The &lt;span class="caps"&gt;GPS&lt;/span&gt;&amp;nbsp;network&lt;/h2&gt;
&lt;p&gt;For this to work globally, you are going to need such landmarks positioned all over the world, within receiving range of any device. These landmarks need to&amp;nbsp;be:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;electrically powered, so they can broadcast&amp;nbsp;signals&lt;/li&gt;
&lt;li&gt;tall, very tall, since radio waves do not follow Earth’s curvature. A short tower would not be able to receive signals for devices that are too far away: the signal would be blocked by the Earths curved&amp;nbsp;surface!&lt;/li&gt;
&lt;li&gt;aware of their own position, and synchronised at regular&amp;nbsp;intervals&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let’s solve problem 2 first: instead of building millions of towers worldwide (and how would we do that on the oceans?), we can just launch satellites into space to serve as landmarks for triangulating position. &lt;span class="caps"&gt;GPS&lt;/span&gt; satellites orbit at an altitude of 20,200 km, almost twice of Earth’s diameter, allowing any of them to be reached from almost half of the Earth’s&amp;nbsp;surface.&lt;/p&gt;
&lt;p&gt;Solving problem 3: if we launch enough satellites, they can continually synchronise their clocks with each other, and triangulate their own position relative to other&amp;nbsp;satellites.&lt;/p&gt;
&lt;p&gt;Solving problem 1: satellites all face this common problem of needing electrical power. They have largely resolved it with the use of solar&amp;nbsp;panels.&lt;/p&gt;
&lt;h2&gt;Getting your location using &lt;span class="caps"&gt;GPS&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;When your smartphone tries to triangulate its location using &lt;span class="caps"&gt;GPS&lt;/span&gt;, it gets its distance from four overhead satellites, along with their location (remember that there is an altitude component!). It then determines its position from this information using&amp;nbsp;triangulation.&lt;/p&gt;
&lt;p&gt;The triangulation calculation here is rather more complicated, given that the satellites are in constant motion and not geostationary (above the same spot on earth all the time). At an orbit altitude of 20,200 km, even radio waves, travelling at lightspeed, still take about 0.07 seconds to reach your phone from a &lt;span class="caps"&gt;GPS&lt;/span&gt; satellite. But it is possible, and your phone does it each time you get your location from &lt;span class="caps"&gt;GPS&lt;/span&gt;—to a precision of about&amp;nbsp;10m!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; To get your location using &lt;span class="caps"&gt;GPS&lt;/span&gt;, your phone receives information from four overhead &lt;span class="caps"&gt;GPS&lt;/span&gt; satellites: their location, and the distance between them and your phone. With this information, your phone can calculate its&amp;nbsp;location.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 159: Wifi &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; cell tower location&amp;nbsp;tracking&lt;/p&gt;
&lt;p&gt;Receiving radio signals all the time requires the receiving unit to be on all the time; if you use &lt;span class="caps"&gt;GPS&lt;/span&gt; heavily you will find your battery draining&amp;nbsp;quickly!&lt;/p&gt;
&lt;p&gt;The reason our smartphones can maintain such good battery life is that they &lt;em&gt;don’t&lt;/em&gt; use &lt;span class="caps"&gt;GPS&lt;/span&gt; most of the time. After all, it is not possible to get &lt;span class="caps"&gt;GPS&lt;/span&gt; in a lot of places: in basements, tunnels, anywhere you can’t get a solid signal from four overhead satellites. There are less energy-costly ways to get your location these days, especially if high precision isn’t&amp;nbsp;necessary.&lt;/p&gt;
&lt;p&gt;Next issue, let’s look at how wifi and cell towers come&amp;nbsp;in!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 13"></category></entry><entry><title>Issue 157: NTP and time-syncing</title><link href="https://ngjunsiang.github.io/laymansguide/issue157.html" rel="alternate"></link><published>2022-01-29T08:00:00+08:00</published><updated>2022-01-29T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-01-29:/laymansguide/issue157.html</id><summary type="html">&lt;p&gt;Time is synchronised from higher-precision sources through a protocol called Network Time Protocol (&lt;span class="caps"&gt;NTP&lt;/span&gt;). A public pool of time servers is available for synchronisation at&amp;nbsp;pool.ntp.org.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; To speed up execution and avoid translation overhead, some systems employ ahead-of-time translation, storing the translated instructions to be executed in future. But many systems employ a mix of just-in-time (&lt;span class="caps"&gt;JIT&lt;/span&gt;) and ahead-of-time (&lt;span class="caps"&gt;AOT&lt;/span&gt;)&amp;nbsp;techniques.&lt;/p&gt;
&lt;p&gt;This season, I’ll attempt to plug the gaps in the layperson’s working knowledge of Internet-related services. Time, location, wifi and mobile data &amp;#8230; almost all will be covered this&amp;nbsp;season!&lt;/p&gt;
&lt;h2&gt;Global time&amp;nbsp;information&lt;/h2&gt;
&lt;p&gt;Frequent fliers would no doubt be familiar with the existence of timezones: geographical bands stretching from the North to South pole, within which all locations are assumed to be running on the same regional time. These timezones used to be manually synchronised, by phone or telegram, via operators all over the&amp;nbsp;globe.&lt;/p&gt;
&lt;p&gt;Today, &lt;a href="https://en.wikipedia.org/wiki/Tz_database"&gt;timezone information&lt;/a&gt; and other time information required for global coordination are maintained by the &lt;a href="https://en.wikipedia.org/wiki/ICANN"&gt;Internet Corporation for Assigned Names and Numbers&lt;/a&gt; (&lt;span class="caps"&gt;ICANN&lt;/span&gt;), which also maintains other lists of names and numbers owned by the &lt;a href="https://en.wikipedia.org/wiki/Internet_Assigned_Numbers_Authority"&gt;Internet Assigned Numbers Authority&lt;/a&gt;, which we first met back in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue027.html"&gt;Issue 27&lt;/a&gt;). These lists are used by programmers worldwide when writing programs that require time&amp;nbsp;coordination.&lt;/p&gt;
&lt;h2&gt;Time&amp;nbsp;synchronisation&lt;/h2&gt;
&lt;p&gt;What about &lt;em&gt;syncing&lt;/em&gt; time? Intuitively this process involves communication &lt;em&gt;between&lt;/em&gt; computers, and anytime computers need to communicate, you can be sure a protocol is involved. We have seen a few protocols mentioned so&amp;nbsp;far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;HTTP&lt;/span&gt;, the Hypertext Transfer Protocol, for sending web documents (aka webpages) and&amp;nbsp;requests&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;DNS&lt;/span&gt;, the Domain Name Service protocol, to translate domain names (like google.com) to &lt;span class="caps"&gt;IP&lt;/span&gt; addresses (like&amp;nbsp;142.250.64.110)&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;DHCP&lt;/span&gt;, the Dynamic Host Configuration Protocol, used by routers to assign &lt;span class="caps"&gt;IP&lt;/span&gt; addresses to its client&amp;nbsp;devices&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;USB&lt;/span&gt;, the Universal Serial Bus set of protocols, used for data transfer between a host computer and another&amp;nbsp;device&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;#8230; I hope that’s enough for an idea of where protocols are&amp;nbsp;involved.&lt;/p&gt;
&lt;p&gt;The protocol for time synchronisation is called the &lt;a href="https://en.wikipedia.org/wiki/Network_Time_Protocol"&gt;Network Time Protocol&lt;/a&gt; (&lt;span class="caps"&gt;NTP&lt;/span&gt;), in use since 1985—that makes it as old as&amp;nbsp;me!&lt;/p&gt;
&lt;h2&gt;Time&amp;nbsp;sources&lt;/h2&gt;
&lt;p&gt;In the past, people would look at a common time source—the town square clock tower, Big Ben, church bells, and so on—to get the time. When watches were invented, people who had one would synchronise their watches to these common time&amp;nbsp;sources.&lt;/p&gt;
&lt;p&gt;But watches, clock towers, and other sources of time can get out of sync: one second as measured by each device does not accurately match the scientific definition of a&amp;nbsp;second:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The second is defined as being equal to the time duration of 9,192,631,770 periods of the radiation corresponding to the transition between the two hyperfine levels of the fundamental unperturbed ground-state of the caesium-133&amp;nbsp;atom.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That’s from &lt;a href="https://www.bipm.org/documents/20126/41483022/SI-Brochure-9.pdf/fcf090b2-04e6-88cc-1149-c3e029ad8232"&gt;The International System of Units&lt;/a&gt;&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;, which I am not inclined to argue with in a layman’s newsletter. Needless to say, synchronising to a universal standard is not something the average layperson does. Only select organisations have the need to keep such accurate time on their own, typically using such precise instruments as atomic&amp;nbsp;clocks.&lt;/p&gt;
&lt;h2&gt;Trickle-down&amp;nbsp;time&lt;/h2&gt;
&lt;p&gt;How does the rest of the world get its time from these high-precision devices? It doesn’t; most of the world has no need for the sixteen zeroes of precision provided by an atomic clock. Instead, another cluster of servers synchronise their time to within microseconds of precision of these devices. And &lt;em&gt;another&lt;/em&gt; cluster of servers synchronise their time to these microsecond-precision&amp;nbsp;servers.&lt;/p&gt;
&lt;p&gt;Each “layer of precision” is called a stratum in &lt;span class="caps"&gt;NTP&lt;/span&gt;. Time “trickles down” from higher-precision sources to lower-precision sources, down the stratum. As we descend the stratum, there are more and more devices providing time at that precision. If a time server synchronises to a stratum 1 server, it becomes a stratum 2 server; if it synchronises to a stratum 7 server, it becomes a stratum 8 server. The upper limit for stratum numbers is 15; a stratum 16 device is considered&amp;nbsp;unsynchronised.&lt;/p&gt;
&lt;p&gt;The stratum number is not an indication of quality or reliability, it only indicates distance from the reference time&amp;nbsp;source.&lt;/p&gt;
&lt;h2&gt;Operating a time&amp;nbsp;server&lt;/h2&gt;
&lt;p&gt;&lt;span class="caps"&gt;NTP&lt;/span&gt; is an open protocol, which means the protocol is &lt;a href="https://www.ntp.org/"&gt;readily available online&lt;/a&gt;, and anyone can run their own server implementing this protocol. If you don’t want to write your own software, you can also use the &lt;a href="https://github.com/ntp-project/ntp"&gt;open-source ntp distribution&lt;/a&gt;, and compile it to make your own time server. Some large companies do this for their own large network, to improve the response time from time servers—public time servers, you can imagine, are under pretty heavy&amp;nbsp;load!&lt;/p&gt;
&lt;h2&gt;So is this how our smartphones synchronise their&amp;nbsp;time?&lt;/h2&gt;
&lt;p&gt;More or less, yes. (They actually use a slightly simpler protocol, in the interest of preserving battery&amp;nbsp;life.)&lt;/p&gt;
&lt;p&gt;Hypothetically, if you are designing your own operating system and having to provide a “set time automatically” feature, you can let your user connect to a public pool of time servers, &lt;a href="https://www.pool.ntp.org/en/"&gt;pool.ntp.org&lt;/a&gt; (yes that is the actual name, and also the web address). This pool is further subdivided by continent and region, down to individual countries, since you will probably need specific time for your location. For instance, the specific time server pool for Singapore is&amp;nbsp;sg.pool.ntp.org.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Time is synchronised from higher-precision sources through a protocol called Network Time Protocol (&lt;span class="caps"&gt;NTP&lt;/span&gt;). A public pool of time servers is available for synchronisation at&amp;nbsp;pool.ntp.org.&lt;/p&gt;
&lt;p&gt;I have been waiting ten seasons to write this, and it is finally out of my&amp;nbsp;system!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 158: &lt;span class="caps"&gt;GPS&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This issue is about time. Next issue is about location. With these two issues I would have explained time and space!&amp;nbsp;🤭&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;If you need a term to google for more of this kind of geekery, it is called &lt;a href="https://en.wikipedia.org/wiki/Metrology"&gt;metrology&lt;/a&gt;, the scientific study of measurement.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 13"></category></entry><entry><title>Issue 156: Translation</title><link href="https://ngjunsiang.github.io/laymansguide/issue156.html" rel="alternate"></link><published>2022-01-22T08:00:00+08:00</published><updated>2022-01-22T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-01-22:/laymansguide/issue156.html</id><summary type="html">&lt;p&gt;To speed up execution and avoid translation overhead, some systems employ ahead-of-time translation, storing the translated instructions to be executed in future. But many systems employ a mix of just-in-time (&lt;span class="caps"&gt;JIT&lt;/span&gt;) and ahead-of-time (&lt;span class="caps"&gt;AOT&lt;/span&gt;)&amp;nbsp;techniques.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Translating a set of instructions before executing it will always lead to a slowdown, although sometimes this may not be noticeable to&amp;nbsp;users.&lt;/p&gt;
&lt;p&gt;So, just-in-time (&lt;span class="caps"&gt;JIT&lt;/span&gt;) compilation is really cool and mostly works. Feed in enough instructions to fill a buffer, and execute them. Keep your fingers crossed and hope the buffer doesn’t empty. That’s kind of how our global supply chain works&amp;nbsp;too.&lt;/p&gt;
&lt;p&gt;But sometimes it doesn’t go smoothly. The program hits a code branch, new instructions have to be unpredictably injected. The emulation layer halts temporarily. The program&amp;nbsp;stutters.&lt;/p&gt;
&lt;p&gt;We can’t really avoid that, not without rewriting the program anyway. But we can at least decide &lt;em&gt;when&lt;/em&gt; to carry out the&amp;nbsp;translation.&lt;/p&gt;
&lt;h2&gt;Ahead-of-time&amp;nbsp;translation&lt;/h2&gt;
&lt;p&gt;What if we translated whatever we could ahead of time, and stored the &lt;em&gt;native&lt;/em&gt; instructions? Then, whenever we need that chunk, instead of translating the original program chunk, we just load the already-translated&amp;nbsp;instructions?&lt;/p&gt;
&lt;p&gt;This is called ahead-of-time (&lt;span class="caps"&gt;AOT&lt;/span&gt;) translation, and is what Apple Rosetta 2 does with MacOS programs compiled for Intel x86-64. While installing those applications, it also carries out translation into native &lt;span class="caps"&gt;ARM&lt;/span&gt; instructions that the M1 later uses for&amp;nbsp;execution.&lt;/p&gt;
&lt;h2&gt;Android &lt;span class="caps"&gt;AOT&lt;/span&gt;&amp;nbsp;translation&lt;/h2&gt;
&lt;p&gt;The Java virtual machine (&lt;span class="caps"&gt;VM&lt;/span&gt;), also called the Java Runtime (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue151.html"&gt;Issue 151&lt;/a&gt;)), is the interpreter that carries out the Java bytecode that a Java program&amp;nbsp;comprises.&lt;/p&gt;
&lt;p&gt;Android apps, though themselves Java programs, are run not by the Java Runtime, but by the Android Runtime&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;Whenever your Android phone finishes installing an Android update, there is always a significant block of time that it takes up “optimising” your apps. What it is doing is actually &lt;span class="caps"&gt;AOT&lt;/span&gt; translation, of the app’s Java bytecode into &lt;span class="caps"&gt;ARM&lt;/span&gt;&amp;nbsp;instructions.&lt;/p&gt;
&lt;h2&gt;Compilers vs&amp;nbsp;interpreters&lt;/h2&gt;
&lt;p&gt;Back in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue054.html"&gt;Issue 54&lt;/a&gt;), I mentioned in a footnote&amp;nbsp;that&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Purists will argue with me that Python technically runs through an interpreter, not a compiler. At this point, the distinction between the two terms for layfolks is not critical, and I choose clarity over accuracy at this point until I can delve into more detail in a future&amp;nbsp;issue.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Here is where I can draw the distinction more&amp;nbsp;clearly.&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;compiler&lt;/strong&gt; &lt;em&gt;compiles&lt;/em&gt; a programming language into another language; usually this means translating a programming language into machine instructions, or virtual machine (&lt;span class="caps"&gt;VM&lt;/span&gt;) bytecode (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue150.html"&gt;Issue 150&lt;/a&gt;)). But it is also not unheard of for compilers to translate one programming language into another, e.g. translating to Javascript for use in a&amp;nbsp;webpage.&lt;/p&gt;
&lt;p&gt;An &lt;strong&gt;interpreter&lt;/strong&gt; &lt;em&gt;interprets&lt;/em&gt; a programming language, and executes it to bring about the intended effect (creating/changing/deleting a file, producing a sound, displaying an image,&amp;nbsp;…).&lt;/p&gt;
&lt;p&gt;It may be helpful to think of a compiler as carrying out &lt;span class="caps"&gt;AOT&lt;/span&gt; translation, and an interpreter as carrying out &lt;span class="caps"&gt;JIT&lt;/span&gt; translation. But many systems will use a mix of both. The Python interpreter, for instance, actually translates Python code into intermediate Python bytecode, and then executes the bytecode. When you run the same Python script, it executes the bytecode if the Python code has not changed. If it has changed, the interpreter will recompile the bytecode first, then execute&amp;nbsp;it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; To speed up execution and avoid translation overhead, some systems employ ahead-of-time translation, storing the translated instructions to be executed in future. But many systems employ a mix of just-in-time (&lt;span class="caps"&gt;JIT&lt;/span&gt;) and ahead-of-time (&lt;span class="caps"&gt;AOT&lt;/span&gt;)&amp;nbsp;techniques.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S13] Issue 157: &lt;span class="caps"&gt;NTP&lt;/span&gt; and&amp;nbsp;time-syncing&lt;/p&gt;
&lt;p&gt;In the season finale, I’ll wrap up with a mishmash of things that are &lt;em&gt;not&lt;/em&gt; the Internet per-se, but very much a part of the Internet and our lives. First up: how do our phones always know the &lt;em&gt;actual&lt;/em&gt;&amp;nbsp;time?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Recall from &lt;a href="https://ngjunsiang.github.io/laymansguide/issue151.html"&gt;Issue 151&lt;/a&gt;) that one needs a distribution license to distribute the Java runtime environment (&lt;span class="caps"&gt;JRE&lt;/span&gt;); Google would have to pay hefty licensing fees to Oracle to bundle the &lt;span class="caps"&gt;JRE&lt;/span&gt; with each copy of Android. Instead, they decided to write their own compatible runtime: the Android Runtime. Is this legal? They already fought that battle in court (&lt;a href="https://en.wikipedia.org/wiki/Google_LLC_v._Oracle_America,_Inc."&gt;Google &lt;span class="caps"&gt;LLC&lt;/span&gt; v. Oracle America, Inc&lt;/a&gt;), and it seems the answer is “yes” (with a heap of caveats).&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 12"></category></entry><entry><title>Issue 155: Emulation performance</title><link href="https://ngjunsiang.github.io/laymansguide/issue155.html" rel="alternate"></link><published>2022-01-15T08:00:00+08:00</published><updated>2022-01-15T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-01-15:/laymansguide/issue155.html</id><summary type="html">&lt;p&gt;Translating a set of instructions before executing it will always lead to a slowdown, although sometimes this may not be noticeable to&amp;nbsp;users.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Programs that were not compiled for the instruction set of the host &lt;span class="caps"&gt;OS&lt;/span&gt; have to go through an emulation layer program. This program translates the instructions of that program into compatible instructions that its own processor can&amp;nbsp;execute.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The Apple M1 is an &lt;span class="caps"&gt;ARM&lt;/span&gt; processor that executes 64-bit &lt;span class="caps"&gt;ARM&lt;/span&gt; instructions. MacOS programs that were compiled for Intel 64-bit x86-64 processors go through the Apple Rosetta 2 emulation layer to run on the&amp;nbsp;M1.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Yes, that’s what I said last issue. But if that were all the Apple Rosetta 2 emulation layer did, The M1 Macbook would not have gotten its rave&amp;nbsp;reviews.&lt;/p&gt;
&lt;h2&gt;The act of&amp;nbsp;translation&lt;/h2&gt;
&lt;p&gt;Think about it: the &lt;span class="caps"&gt;ARM&lt;/span&gt; and x86-64 instruction sets are very different! They have different lengths, different instruction names and bit values, different concepts of operation, … they’re &lt;em&gt;very&lt;/em&gt; different. While the Intel Core and &lt;span class="caps"&gt;ARM&lt;/span&gt; processor architectures share &lt;em&gt;some&lt;/em&gt; similarities, translating instructions from one set to another, though possible, is still no simple&amp;nbsp;feat.&lt;/p&gt;
&lt;p&gt;Keep in mind that in addition to translating the x86-64 instructions to &lt;span class="caps"&gt;ARM&lt;/span&gt;, the M1 also has to &lt;em&gt;execute them&lt;/em&gt;—the program must go on. It should not be surprising at all that there is a performance hit compared with executing native &lt;span class="caps"&gt;ARM&lt;/span&gt;&amp;nbsp;instructions.&lt;/p&gt;
&lt;p&gt;If you were given a set of execution instructions in your native language, you would have little difficulty carrying out the required task. But if the execution instructions are now in alien language? Suppose you were given translation instructions, in your native language, for understanding the alien language. You would definitely be doing it slower than if the execution instructions were in your native&amp;nbsp;language.&lt;/p&gt;
&lt;h2&gt;Just-in-time&amp;nbsp;translation&lt;/h2&gt;
&lt;p&gt;These days, our processors are fast enough that if they are not too bogged down, they can actually read in instructions slightly ahead of time, translate them, and store the translated instructions for a short period of time before they are executed. This way, the operating system remains ever so slightly ahead of the program being translated, in a way reminiscent of our currently strained global supply&amp;nbsp;chain.&lt;/p&gt;
&lt;p&gt;Naturally, it doesn’t make sense to keep doing translation work. As much as possible, we want to minimise it! But&amp;nbsp;how?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Translating a set of instructions before executing it will always lead to a slowdown, although sometimes this may not be noticeable to&amp;nbsp;users.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 156:&amp;nbsp;Translation&lt;/p&gt;
&lt;p&gt;In the last issue of season 12, we talk about why app installation or system updates can take so&amp;nbsp;long.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 12"></category></entry><entry><title>Issue 154: Emulation</title><link href="https://ngjunsiang.github.io/laymansguide/issue154.html" rel="alternate"></link><published>2022-01-08T08:00:00+08:00</published><updated>2022-01-08T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-01-08:/laymansguide/issue154.html</id><summary type="html">&lt;p&gt;Programs that were not compiled for the instruction set of the host &lt;span class="caps"&gt;OS&lt;/span&gt; have to go through an emulation layer program. This program translates the instructions of that program into compatible instructions that its own processor can&amp;nbsp;execute.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; The cloud offers standard digital business services, accessible through a web interface and &lt;span class="caps"&gt;API&lt;/span&gt;, which any developer (with a credit card) can use. Developers don’t have to reinvent the wheel, so long as they know how to use web&amp;nbsp;APIs.&lt;/p&gt;
&lt;p&gt;Virtualisation, particularly system virtualization, is a real game-changer for those of us who like to have our apps all running in the same operating system, instead of switching operating systems all the time through dual-booting (or Apple&amp;nbsp;Parallels).&lt;/p&gt;
&lt;p&gt;But what is stopping us from allowing them to run near-natively in the desktop, their windows directly showing up in the taskbar, without the distracting abstraction of the virtual&amp;nbsp;machine?&lt;/p&gt;
&lt;h2&gt;Introduction to&amp;nbsp;Emulation&lt;/h2&gt;
&lt;p&gt;What you are seeking is a feature known as &lt;strong&gt;emulation&lt;/strong&gt;, in which your operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;), which we shall again call the &lt;strong&gt;host&lt;/strong&gt;, &lt;em&gt;emulates&lt;/em&gt; the instruction set (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue053.html"&gt;Issue 53&lt;/a&gt;)) that the application is compiled for. In other words, the host &lt;span class="caps"&gt;OS&lt;/span&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;presents itself as the “correct” machine type to the application (“hello program, I listen to x86 instructions and respond to x86 instructions, so please treat me like an x86&amp;nbsp;processor”),&lt;/li&gt;
&lt;li&gt;transparently interprets its machine code into its instruction set’s machine code (through a program called an &lt;strong&gt;emulation layer&lt;/strong&gt;),&lt;/li&gt;
&lt;li&gt;executes the interpreted version, producing its intended effects, and returning any intended output back to the&amp;nbsp;application.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Depending on how different the two instruction sets are, the complexity of this task differs greatly. Not much point going into detail here in a layman’s newsletter, so instead I’ll briefly illustrate some instances of emulation in the&amp;nbsp;wild.&lt;/p&gt;
&lt;p&gt;The three main instruction sets discussed here are x86 (32-bit), x86-64 (64-bit), and &lt;span class="caps"&gt;ARM&lt;/span&gt;&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue053.html"&gt;Issue 53&lt;/a&gt;)).&lt;/p&gt;
&lt;h2&gt;Windows-to-Windows emulation (&lt;span class="caps"&gt;WOW64&lt;/span&gt;)&lt;/h2&gt;
&lt;p&gt;Do you remember the great 32-to-64-bit schism of the late 2000s (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue055.html"&gt;Issue 55&lt;/a&gt;))? There was a period of time when people got confused whether a Windows program they had could run on a 32-bit x86 processor or a 64-bit x86-64 processor: programs compiled for the latter could not run on the former, but programs compiled for the former could run on the&amp;nbsp;latter.&lt;/p&gt;
&lt;p&gt;&lt;img alt="screenshot of download options for WinRAR, showing 32-bit and 64-bit options" src="https://ngjunsiang.github.io/laymansguide/issue154_01.png" /&gt;&lt;br /&gt;
&lt;em&gt;Some download sites still ask you to make this choice between downloading the 32-bit or 64-bit version, usually for users who for whatever reason have opted not to upgrade to 64-bit&amp;nbsp;processors.&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;That was a lie. Programs compiled for Windows on x86 cannot run &lt;em&gt;natively&lt;/em&gt; on x86-64, and vice-versa. x86 and x86-64, while looking similar, are different instruction sets. x86 instructions have to be translated into x86-64 instructions to run on a 64-bit&amp;nbsp;processor.&lt;/p&gt;
&lt;p&gt;What happened was Microsoft developed the &lt;span class="caps"&gt;WOW64&lt;/span&gt; subsystem, an emulation layer that translated 32-bit x86 instructions into 64-bit x86-64 instructions. When users tried to run a 32-bit application, Windows plugged the instruction stream into &lt;span class="caps"&gt;WOW64&lt;/span&gt;, executing the interpreted instructions and allowing it to run near-natively&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;And so the 32-to-64-bit transition took place more smoothly than it would otherwise&amp;nbsp;have.&lt;/p&gt;
&lt;h2&gt;Windows &lt;span class="caps"&gt;ARM&lt;/span&gt; emulation for&amp;nbsp;x86&lt;/h2&gt;
&lt;p&gt;In 2019, Microsoft released the Surface Pro X, its second&lt;sup id="fnref:3"&gt;&lt;a class="footnote-ref" href="#fn:3"&gt;3&lt;/a&gt;&lt;/sup&gt; &lt;span class="caps"&gt;ARM&lt;/span&gt;-powered laptop. That’s right, it’s Windows not running on an Intel chip. Microsoft does actually have a version of Windows, called Windows &lt;span class="caps"&gt;ARM&lt;/span&gt;, which runs on &lt;span class="caps"&gt;ARM&lt;/span&gt; chips. But what about all the programs you know and&amp;nbsp;love?&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;WOW64&lt;/span&gt; to the rescue again! This emulation layer &lt;em&gt;also&lt;/em&gt; translates 64-bit x86-64 instructions to 64-bit &lt;span class="caps"&gt;ARM&lt;/span&gt; instructions, allowing them to run on Windows &lt;span class="caps"&gt;ARM&lt;/span&gt; (with a performance penalty due to the translation&amp;nbsp;required).&lt;/p&gt;
&lt;h2&gt;Game console&amp;nbsp;emulation&lt;/h2&gt;
&lt;p&gt;If you find any “reborn” retro gaming products floating around, these are guaranteed to be emulators in disguise: the original hardware that the consoles used are no longer in production. (If you don’t find any, you can also just google “console emulation” to find a whole collection of&amp;nbsp;them.)&lt;/p&gt;
&lt;p&gt;These emulators are usually hobby projects by skilled amateurs, who attempt to reverse-engineer the workings of the original hardware. They then write programs to &lt;em&gt;emulate&lt;/em&gt; these processors on modern hardware, allowing you to “boot” a digital copy of the games that worked on those&amp;nbsp;platforms.&lt;/p&gt;
&lt;h2&gt;Apple&amp;nbsp;Rosetta&lt;/h2&gt;
&lt;p&gt;The Apple M1 is an &lt;span class="caps"&gt;ARM&lt;/span&gt; processor that executes 64-bit &lt;span class="caps"&gt;ARM&lt;/span&gt; instructions. MacOS programs compiled for Intel 64-bit x86-64 processors must go through the Apple Rosetta 2 emulation layer to run on the M1. This works like &lt;span class="caps"&gt;WOW64&lt;/span&gt; but in the reverse direction: it takes in an x86-64 instruction stream, and produces interpreted instructions for Apple &lt;span class="caps"&gt;ARM&lt;/span&gt;&amp;nbsp;processors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Programs that were not compiled for the instruction set of the host &lt;span class="caps"&gt;OS&lt;/span&gt; have to go through an emulation layer program. This program translates the instructions of that program into compatible instructions that its own processor can&amp;nbsp;execute.&lt;/p&gt;
&lt;p&gt;Simple to describe, much more difficult to execute …&amp;nbsp;😩&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 155: Emulation&amp;nbsp;performance&lt;/p&gt;
&lt;p&gt;Is this how emulation actually works? I wish! Don’t want to go too deep, but I think it is instructive to briefly discuss performance issues, and why it leads to some things you might have&amp;nbsp;observed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;&lt;span class="caps"&gt;ARM&lt;/span&gt; actually has a 32-bit instruction set—AArch32 and a 64-bit instruction set—AArch64, which are incompatible. But since Apple switched to AArch64 starting from the iPhone 5S (2013), other mobile device manufacturers have followed suit, and AArch64 is now the main instruction set used on mobile. In this issue, I use &lt;span class="caps"&gt;ARM&lt;/span&gt; to refer to AArch64.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;You can still see this happening with some old programs; this is usually indicated in the title bar as compatibility mode.&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:3"&gt;
&lt;p&gt;Their first was the ill-fated &lt;a href="https://www.techradar.com/reviews/pc-mac/tablets/microsoft-surface-rt-1085839/review"&gt;Surface &lt;span class="caps"&gt;RT&lt;/span&gt;&lt;/a&gt;, which these days is only whispered about.&amp;#160;&lt;a class="footnote-backref" href="#fnref:3" title="Jump back to footnote 3 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 12"></category></entry><entry><title>Issue 153: Using the cloud</title><link href="https://ngjunsiang.github.io/laymansguide/issue153.html" rel="alternate"></link><published>2022-01-01T08:00:00+08:00</published><updated>2022-01-01T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2022-01-01:/laymansguide/issue153.html</id><summary type="html">&lt;p&gt;The cloud offers standard digital business services, accessible through a web interface and &lt;span class="caps"&gt;API&lt;/span&gt;, which any developer (with a credit card) can use. Developers don’t have to reinvent the wheel, so long as they know how to use web&amp;nbsp;APIs.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Actually making a web application requires you to set up lots of supporting software and carry out lots of steps to create a suitable app&amp;nbsp;environment.&lt;/p&gt;
&lt;p&gt;Last issue, I described the whole host of things that need to be done just to make a web application work on another server, different from where you did your&amp;nbsp;programming.&lt;/p&gt;
&lt;p&gt;How do people deploy web services so quickly if there is so much tedium&amp;nbsp;involved?&lt;/p&gt;
&lt;h2&gt;Birth of the&amp;nbsp;Cloud&lt;/h2&gt;
&lt;p&gt;You could set up the environment, containerise it, and deploy it through a container … that’s one way to solve for&amp;nbsp;distribution.&lt;/p&gt;
&lt;p&gt;But somebody smart realised that this is likely a common problem. 99%&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt; of applications are going to need the same building blocks: one or more databases (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue079.html"&gt;Season 7&lt;/a&gt;)), remote storage servers for storing large files (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue106.html"&gt;Issue 106&lt;/a&gt;)), web servers (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue008.html"&gt;Issue 8&lt;/a&gt;)), and of course the application&amp;nbsp;itself.&lt;/p&gt;
&lt;p&gt;All these pieces must themselves run on a machine (physical or virtual). The hardware, the network resources, the type of database, the type of storage, … these are usually not key differentiators for the business. They do not derive their business value by managing it differently from other&amp;nbsp;companies.&lt;/p&gt;
&lt;p&gt;Amazon was the first to realise that whatever they were doing to scale Amazon’s business globally, their competitors and other businesses would eventually need as&amp;nbsp;well.&lt;/p&gt;
&lt;h2&gt;The Cloud is&amp;nbsp;born&lt;/h2&gt;
&lt;p&gt;The Cloud is a collection of services that can be plugged in to an application, in lieu of writing your own code. Instead of implementing your own storage server, you could use Google Cloud Storage, or Amazon S3, or Microsoft Azure Storage, etc. You access these and other services typically through a web &lt;span class="caps"&gt;API&lt;/span&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue004.html"&gt;Issue 4&lt;/a&gt;)), and have the option to manage them through a web&amp;nbsp;interface.&lt;/p&gt;
&lt;p&gt;&lt;img alt="screenshot of Google Cloud Storage’s web interface" src="https://ngjunsiang.github.io/laymansguide/issue153_01.png" /&gt;&lt;br /&gt;
&lt;em&gt;Google Cloud Storage web&amp;nbsp;interface&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Besides virtual machines (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue147.html"&gt;Issue 147&lt;/a&gt;)), cloud offerings span multiple layers of&amp;nbsp;abstraction.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Google Cloud Storage’s main cloud offerings" src="https://ngjunsiang.github.io/laymansguide/issue153_02.png" /&gt;&lt;br /&gt;
&lt;em&gt;Google Cloud main&amp;nbsp;offerings&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Here are Google’s mainstay&amp;nbsp;offerings:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Compute Engine: virtual&amp;nbsp;machines&lt;/li&gt;
&lt;li&gt;App Engine: a pre-configured, setup-free virtual machine that runs code which you upload, through a web &lt;span class="caps"&gt;API&lt;/span&gt; or their web&amp;nbsp;interface&lt;/li&gt;
&lt;li&gt;Kubernetes Engine: a hypervisor (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue148.html"&gt;Issue 148&lt;/a&gt;)) that runs your&amp;nbsp;containers&lt;/li&gt;
&lt;li&gt;Cloud Storage: a storage service for files (see earlier&amp;nbsp;screenshot)&lt;/li&gt;
&lt;li&gt;Cloud &lt;span class="caps"&gt;SQL&lt;/span&gt;: a relational database service (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue087.html"&gt;Issue 87&lt;/a&gt;))&lt;/li&gt;
&lt;li&gt;Cloud Bigtable: a NoSQL database service (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue090.html"&gt;Issue 90&lt;/a&gt;))&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Then there are variants for running big data queries, using machine learning nodes, and rebranded services for running the backends of mobile apps, … the key common factor here is that using these services is much simpler than rolling and maintaining your own version!&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt; And it lets you speed up development by not having to reinvent the wheel that cloud services have implemented for&amp;nbsp;you.&lt;/p&gt;
&lt;h2&gt;The Cloud&amp;nbsp;grows&lt;/h2&gt;
&lt;p&gt;As web applications got larger and larger, beyond the capacity of even a single high-end server to manage, they had to be redesigned so that they could run on multiple servers while maintaining data synchronicity. As businesses standardise on ways to do that, cloud providers add these tools as part of their&amp;nbsp;offerings.&lt;/p&gt;
&lt;p&gt;For example, sending/receiving messages between servers is a key engineering problem. Data packets sometimes get dropped en-route, or when a server gets overloaded. Sometimes they get held up at a server, time out, and then they get resent by the client (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue009.html"&gt;Issue 9&lt;/a&gt;)); the server receives two identical requests, and sends two identical responses, resulting in a duplicate&amp;nbsp;response.&lt;/p&gt;
&lt;p&gt;If you don’t want to write your own software for managing communication between servers, the cloud lets you write code for your machines to communicate easily, without having to crack your head thinking about how to make it&amp;nbsp;happen.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; The cloud offers standard digital business services, accessible through a web interface and &lt;span class="caps"&gt;API&lt;/span&gt;, which any developer (with a credit card) can use. Developers don’t have to reinvent the wheel, so long as they know how to use web&amp;nbsp;APIs.&lt;/p&gt;
&lt;p&gt;There is, of course, much more to developing an application than just gluing services together. For commercial applications, you still have compliance requirements, logging, monitoring, and other things to set up. But these are not new needs, and not really worth going into detail in a layman’s&amp;nbsp;newsletter.&lt;/p&gt;
&lt;p&gt;I hope the gist of what the cloud does is at least&amp;nbsp;clearer!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 154:&amp;nbsp;Emulation&lt;/p&gt;
&lt;p&gt;I am done with virtualisation and the cloud at this point. You’ve learned about hardware virtualisation (through drivers), system virtualisation (through system VMs), process virtualisation (through process VMs), and service virtualisation (through APIs) so far this&amp;nbsp;season.&lt;/p&gt;
&lt;p&gt;I’m going to use the last three issues to talk about a related and current thing: instruction translation and emulation. Let’s start with a question: How is the Apple M1, an &lt;span class="caps"&gt;ARM&lt;/span&gt; chip, able to run MacOS programs compiled for the Intel x86-64 chips? Aren’t they two very different instruction sets (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue053.html"&gt;Issue 53&lt;/a&gt;))?&lt;/p&gt;
&lt;p&gt;Yes, yes they are. More next issue&amp;nbsp;;)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Illustrative but not accurate estimation!&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;Provided your use case is in the 99-percentile! If yours is an edge case, you might find that rolling your own version is better value for money in the long run.&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 12"></category></entry><entry><title>Issue 152: Getting started with programming</title><link href="https://ngjunsiang.github.io/laymansguide/issue152.html" rel="alternate"></link><published>2021-12-25T08:00:00+08:00</published><updated>2021-12-25T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-12-25:/laymansguide/issue152.html</id><summary type="html">&lt;p&gt;Actually making a web application requires you to set up lots of supporting software and carry out lots of steps to create a suitable app&amp;nbsp;environment.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; The Java Runtime Environment (&lt;span class="caps"&gt;JRE&lt;/span&gt;) bundles the Java &lt;span class="caps"&gt;VM&lt;/span&gt; and supporting libraries. The &lt;span class="caps"&gt;JRE&lt;/span&gt; has to be installed on the user’s system for Java programs to work, unless the program bundles the supporting libraries. Solo programmers can start programming with OpenJDK for free with fewer features and less support, while commercial companies can license Oracle &lt;span class="caps"&gt;JDK&lt;/span&gt; for better support and&amp;nbsp;features.&lt;/p&gt;
&lt;p&gt;So you started taking up programming. Maybe you went to a class, where everything was set up for you and you didn’t have to worry about installing and configuring necessary software. Or you took an online course, where step-by-step instructions were provided and you mostly didn’t have to spend time scratching your head. That’s how it should be; you paid to learn &lt;em&gt;programming&lt;/em&gt;, not to learn how to configure a software development&amp;nbsp;environment.&lt;/p&gt;
&lt;p&gt;Once you actually have to start writing code though&amp;nbsp;…&lt;/p&gt;
&lt;h2&gt;Setting up a development&amp;nbsp;environment&lt;/h2&gt;
&lt;p&gt;Let’s suppose you are writing code for a web application. You start by installing the software for the compiler/interpreter program, which either executes your code directly or compiles it into an executable binary (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue054.html"&gt;Issue 54&lt;/a&gt;)).&lt;/p&gt;
&lt;p&gt;Now you begin writing your code. Along the way, you begin to install various libraries (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue017.html"&gt;Issue 17&lt;/a&gt;)) and frameworks (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue018.html"&gt;Issue 18&lt;/a&gt;)) that your code relies on. These are provided in things called &lt;strong&gt;packages&lt;/strong&gt;, which are basically zipped files containing all the files and metadata for the library/framework. You install these packages using another program, called a &lt;strong&gt;package manager&lt;/strong&gt;, through the command line terminal (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue015.html"&gt;Issue 15&lt;/a&gt;)).&lt;/p&gt;
&lt;p&gt;You write more code. And one day … you’re done! The real pain has just&amp;nbsp;begun!&lt;/p&gt;
&lt;h2&gt;Deploying&amp;nbsp;code&lt;/h2&gt;
&lt;p&gt;Thus far, you have been programming on your own laptop. But your laptop can’t handle a full webserver load once people start using your app, so you wisely decided to lease a virtual machine (&lt;span class="caps"&gt;VM&lt;/span&gt;: see &lt;a href="https://ngjunsiang.github.io/laymansguide/issue147.html"&gt;Issue 147&lt;/a&gt;)) from a cloud provider instead. You boot up the &lt;span class="caps"&gt;VM&lt;/span&gt;, it goes through its bootup process (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue112.html"&gt;Issue 112&lt;/a&gt;)), and finally completes. You are greeted with a familiar command line, the text cursor blinking&amp;nbsp;cheerfully.&lt;/p&gt;
&lt;p&gt;How are you going to get your code on that&amp;nbsp;machine?&lt;/p&gt;
&lt;p&gt;Maybe you set up a code repository (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue019.html"&gt;Issue 19&lt;/a&gt;)) on another server, and then download your code onto the &lt;span class="caps"&gt;VM&lt;/span&gt; with some&amp;nbsp;commands.&lt;/p&gt;
&lt;p&gt;Maybe you decide to turn your app into a package instead: you write software instructions (more code!) to tell the &lt;strong&gt;package manager&lt;/strong&gt; (mentioned earlier) how to install the package, and how to configure everything. You add some files with metadata, a file manifest containing all the files used by the package, and then pack it up. You install yet more software (called &lt;strong&gt;build tools&lt;/strong&gt;) to help you automate this part. Then you set up another file server, upload the package onto it from your laptop, download it from the &lt;span class="caps"&gt;VM&lt;/span&gt;, and install it on the &lt;span class="caps"&gt;VM&lt;/span&gt; using the same &lt;strong&gt;package manager&lt;/strong&gt;&amp;nbsp;software.&lt;/p&gt;
&lt;p&gt;You test it, and after many hours of cursing, confused pacing and mumbling, and much hair-tearing, it finally works.&amp;nbsp;Phew!&lt;/p&gt;
&lt;h2&gt;Expanding the&amp;nbsp;app&lt;/h2&gt;
&lt;p&gt;Unfortunately, the tiny toy server that you used to test your web app doesn’t hold up to real-world network loads. You’ll need to put the app behind a Real™ web server; as more and more users use it, you may even need to deploy your app to multiple servers to handle the load, all managed by a &lt;strong&gt;load balancer&lt;/strong&gt;. The balancer receives the web requests (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue009.html"&gt;Issue 9&lt;/a&gt;)), decides which of the multiple servers has the lowest load so far, and directs the request to it so it can serve a web response (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue008.html"&gt;Issue 8&lt;/a&gt;)).&lt;/p&gt;
&lt;p&gt;Deploying more servers … does that mean you have to do the above all over&amp;nbsp;again?!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Actually making a web application requires you to set up lots of supporting software and carry out lots of steps to create a suitable app&amp;nbsp;environment.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 153: Using the&amp;nbsp;cloud&lt;/p&gt;
&lt;p&gt;I hope this issue adequately describes the problem that the cloud attempted to resolve! When people talk about “using the cloud”, what is that actually&amp;nbsp;like?&lt;/p&gt;
&lt;p&gt;Next issue, I’ll give a peek. Short issue&amp;nbsp;guaranteed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 12"></category></entry><entry><title>Issue 151: the Java VM</title><link href="https://ngjunsiang.github.io/laymansguide/issue151.html" rel="alternate"></link><published>2021-12-18T08:00:00+08:00</published><updated>2021-12-18T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-12-18:/laymansguide/issue151.html</id><summary type="html">&lt;p&gt;The Java Runtime Environment (&lt;span class="caps"&gt;JRE&lt;/span&gt;) bundles the Java &lt;span class="caps"&gt;VM&lt;/span&gt; and supporting libraries. The &lt;span class="caps"&gt;JRE&lt;/span&gt; has to be installed on the user’s system for Java programs to work, unless the program bundles the supporting libraries. Solo programmers can start programming with OpenJDK for free with fewer features and less support, while commercial companies can license Oracle &lt;span class="caps"&gt;JDK&lt;/span&gt; for better support and&amp;nbsp;features.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; System VMs provide a set of virtualised hardware that the &lt;span class="caps"&gt;OS&lt;/span&gt; interacts with. Process VMs provide a set of libraries that a program (written in that programming language) interacts&amp;nbsp;with.&lt;/p&gt;
&lt;p&gt;If the Java &lt;span class="caps"&gt;VM&lt;/span&gt; lets us write programs that work across multiple Oses, why don’t we write everything in Java&amp;nbsp;then?&lt;/p&gt;
&lt;p&gt;Actually a lot of enterprises do! But there are some tradeoffs to make this&amp;nbsp;work.&lt;/p&gt;
&lt;h2&gt;What’s&amp;nbsp;bundled&lt;/h2&gt;
&lt;p&gt;The code required to make a Java program run on every operating system is not simple; somehow all this complexity needs to make its way to the target computer, whether it uses it or&amp;nbsp;not.&lt;/p&gt;
&lt;p&gt;Ideally, we have one part: the program you want to distribute, bundled and delivered to the user. The other part, like an adapter (the Java &lt;span class="caps"&gt;VM&lt;/span&gt;), allows your program to work on the target&amp;nbsp;computer.&lt;/p&gt;
&lt;p&gt;Up to this point I have given the impression that the Java &lt;span class="caps"&gt;VM&lt;/span&gt; is all that is needed for the adapter to work. This is inaccurate; the Java &lt;span class="caps"&gt;VM&lt;/span&gt; interprets the program instructions (in an intermediate set of instructions called &lt;em&gt;bytecode&lt;/em&gt;). It still needs a whole set of supporting libraries to enable interfacing with the &lt;span class="caps"&gt;OS&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;The Java &lt;span class="caps"&gt;VM&lt;/span&gt; and these supporting libraries are installed in a software package called the Java Runtime Environment (&lt;span class="caps"&gt;JRE&lt;/span&gt;). If you hear the term “runtime environment”, this is what it refers to: the program plus the libraries it needs to&amp;nbsp;run.&lt;/p&gt;
&lt;p&gt;If you want to write Java programs, you’ll need more than the &lt;span class="caps"&gt;JRE&lt;/span&gt; though. you will need supporting tools especially for debugging, and these are provided in the Java Development Kit (&lt;span class="caps"&gt;JDK&lt;/span&gt;). This is not only true for Java, but for many other languages as well; if you are getting into programming, and asked to pick a download, you usually want the one that says “development&amp;nbsp;kit”.&lt;/p&gt;
&lt;h1&gt;Tradeoffs in&amp;nbsp;distribution&lt;/h1&gt;
&lt;p&gt;If you want to keep the software bundle size small, you’ll have to ask the user to install the &lt;span class="caps"&gt;JRE&lt;/span&gt; separately (about ~&lt;span class="caps"&gt;80MB&lt;/span&gt; downloaded, more when installed). If you don’t want your users to face that hassle, you’ll have to take on the work of bundling the required libraries into your program yourself. Are you going to bundle libraries for all targeted OSes, or just for one particular platform? The more you bundle the larger the size&amp;nbsp;&amp;#8230;&lt;/p&gt;
&lt;p&gt;Nobody said multiplatform support is&amp;nbsp;easy!&lt;/p&gt;
&lt;h2&gt;The&amp;nbsp;legalese&lt;/h2&gt;
&lt;p&gt;Java is free to use—but only on a personal basis. Once you intend to distribute your program, and maybe even make money, the issue of licensing rears its&amp;nbsp;head.&lt;/p&gt;
&lt;p&gt;Java was originally created at Sun Microsystems in 1995, but was acquired by Oracle in 2010. They do enforce their licensing pretty strictly, so be prepared to pay for the&amp;nbsp;convenience!&lt;/p&gt;
&lt;h2&gt;Java and&amp;nbsp;open-source&lt;/h2&gt;
&lt;p&gt;If the only way to distribute programs with multiplatform support is to pay a licensing fee, Java would see much lower takeup, which would hurt long-term profits &amp;#8230; so a big portion of the Java core is open-sourced as &lt;a href="https://openjdk.java.net/"&gt;OpenJDK&lt;/a&gt;. This is free to use and extend, and many businesses have been successfully releasing software based on it. But it is going to be a lot more work carrying out testing and writing code for features which are not&amp;nbsp;provided.&lt;/p&gt;
&lt;p&gt;On top of OpenJDK, Oracle &lt;span class="caps"&gt;JDK&lt;/span&gt;—Oracle’s commercial release of the &lt;span class="caps"&gt;JDK&lt;/span&gt;—adds some proprietary code, plus lots of enterprise support and testing. In general you’re going to have a much easier time writing your code with Oracle &lt;span class="caps"&gt;JDK&lt;/span&gt; instead of OpenJDK—that is how Oracle makes&amp;nbsp;money!&lt;/p&gt;
&lt;p&gt;If you just want to get familiar with the language, or are working on a personal project, OpenJDK lets you do so for free, legally. If you are a corporation trying to get your engineers to write code that runs on multiple platforms so as to simplify your systems, Oracle &lt;span class="caps"&gt;JDK&lt;/span&gt; helps you to save time doing that, for a&amp;nbsp;fee.&lt;/p&gt;
&lt;p&gt;It’s the best of both&amp;nbsp;worlds.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; The Java Runtime Environment (&lt;span class="caps"&gt;JRE&lt;/span&gt;) bundles the Java &lt;span class="caps"&gt;VM&lt;/span&gt; and supporting libraries. The &lt;span class="caps"&gt;JRE&lt;/span&gt; has to be installed on the user’s system for Java programs to work, unless the program bundles the supporting libraries. Solo programmers can start programming with OpenJDK for free with fewer features and less support, while commercial companies can license Oracle &lt;span class="caps"&gt;JDK&lt;/span&gt; for better support and&amp;nbsp;features.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 152: Getting started with&amp;nbsp;programming&lt;/p&gt;
&lt;p&gt;Hold on, I&amp;nbsp;thought—&lt;/p&gt;
&lt;p&gt;Nah, Layman’s Guide to Computing has not suddenly switched to being a Guide to Programming! We are still on the topic of the cloud and the history of commercial computing. Where I last stopped, I was talking about how co-hosting started from virtual-hardware &lt;span class="caps"&gt;VM&lt;/span&gt; rental, and then added containerisation to its bag of tools. Then I segued into talking about process virtualisation and the Java &lt;span class="caps"&gt;VM&lt;/span&gt; (last issue &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; this&amp;nbsp;issue).&lt;/p&gt;
&lt;p&gt;A short note though: process virtualisation isn’t a co-hosting tool, but a programming tool. We use process virtualisation while writing programs, not while deploying programs to the cloud. And I do want to get back to talking about the&amp;nbsp;cloud.&lt;/p&gt;
&lt;p&gt;But before I do that, there’s one thing that the average layperson would not be familiar with, and which I need to talk&amp;nbsp;about:&lt;/p&gt;
&lt;p&gt;Why is it so difficult to &lt;em&gt;do&lt;/em&gt; programming, even with experience? Yes, &lt;em&gt;do&lt;/em&gt; programming, not merely learn&amp;nbsp;it!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 12"></category></entry><entry><title>Issue 150: System VMs vs Process VMs</title><link href="https://ngjunsiang.github.io/laymansguide/issue150.html" rel="alternate"></link><published>2021-12-11T08:00:00+08:00</published><updated>2021-12-11T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-12-11:/laymansguide/issue150.html</id><summary type="html">&lt;p&gt;System VMs provide a set of virtualised hardware that the &lt;span class="caps"&gt;OS&lt;/span&gt; interacts with. Process VMs provide a set of libraries that a program (written in that programming language) interacts&amp;nbsp;with.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Containers are one layer of virtualisation above virtual machines: containerisation systems virtualise access to the operating system, presenting a virtual interface that provides software with the resources it needs, without being aware of software running in other containers on the same&amp;nbsp;system.&lt;/p&gt;
&lt;h2&gt;Recap&lt;/h2&gt;
&lt;p&gt;If I need to configure an entire machine to install and configure my own operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;), I can rent a virtual machine — this is system&amp;nbsp;virtualisation.&lt;/p&gt;
&lt;p&gt;If I just need to run a set of software &lt;em&gt;on a particular &lt;span class="caps"&gt;OS&lt;/span&gt;&lt;/em&gt; but don’t want the hassle of managing the rest of the &lt;span class="caps"&gt;OS&lt;/span&gt;, I can containerise them for the &lt;span class="caps"&gt;OS&lt;/span&gt; — this is&amp;nbsp;containerisation.&lt;/p&gt;
&lt;p&gt;What if I want to write software that will work the same on multiple OSes? Is that&amp;nbsp;possible?&lt;/p&gt;
&lt;p&gt;Before 1995,&amp;nbsp;no!&lt;/p&gt;
&lt;h2&gt;&lt;span class="caps"&gt;OS&lt;/span&gt;-level details are&amp;nbsp;tricky&lt;/h2&gt;
&lt;p&gt;OSes mediate access to resources through libraries (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue017.html"&gt;Issue 17&lt;/a&gt;)). Instead of having to deal with storage disk sectors and blocks, you can just use library features to ask the &lt;span class="caps"&gt;OS&lt;/span&gt; to help you create a file and write data to it. The &lt;span class="caps"&gt;OS&lt;/span&gt; and filesystem (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue106.html"&gt;Issue 106&lt;/a&gt;)) take care of the&amp;nbsp;details.&lt;/p&gt;
&lt;p&gt;I don’t know how to explain this more clearly without showing some programming code; the &lt;span class="caps"&gt;OS&lt;/span&gt; controls a lot of things! Which is a problem when it comes to multi-&lt;span class="caps"&gt;OS&lt;/span&gt; support, because different OSes control different things&amp;nbsp;differently.&lt;/p&gt;
&lt;p&gt;The library features for lots of things — storage, searching through directories, networking, etc — are different between Windows, Linux, MacOS,&amp;nbsp;&amp;#8230;&lt;/p&gt;
&lt;h2&gt;Abstracting away the&amp;nbsp;details&lt;/h2&gt;
&lt;p&gt;Can’t we program in a higher-level language, and use another program (the code interpreter) to break it down to different types of operations on different operating systems?&amp;nbsp;E.g. &lt;code&gt;python.exe&lt;/code&gt; on Windows will try to make my Python code work in Windows by using Windows&amp;nbsp;libraries, &lt;code&gt;python&lt;/code&gt; on Linux will do so using Linux libraries,&amp;nbsp;and &lt;code&gt;python&lt;/code&gt; on MacOS will do so using macOS libraries. Javascript, Ruby, Perl, VBScript, and other &lt;strong&gt;interpreted programming languages&lt;/strong&gt; work this way too&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;To a large extent, this is possible. But there remain some irreconcilable&amp;nbsp;differences:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;if you need to use Python to run a command-line program, such as a shell script (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue016.html"&gt;Issue 16&lt;/a&gt;)), those will depend on terminal availability: not all terminals have cross-&lt;span class="caps"&gt;OS&lt;/span&gt;&amp;nbsp;support!&lt;/li&gt;
&lt;li&gt;If you need to specify a file location, Windows and Linux do not use the same path separators (the character that separates folder names in a folder hierarchy); Windows&amp;nbsp;uses &lt;code&gt;\&lt;/code&gt; to separate directory names, while Linux and MacOS&amp;nbsp;uses &lt;code&gt;/&lt;/code&gt;. This is often a source of bugs and&amp;nbsp;headaches.&lt;/li&gt;
&lt;li&gt;Important advanced features, such as multiprocessing (on multiple &lt;span class="caps"&gt;CPU&lt;/span&gt; cores simultaneously), are &lt;a href="https://rhodesmill.org/brandon/2010/python-multiprocessing-linux-windows/"&gt;handled differently in different OSes&lt;/a&gt; in ways that may be incompatible within the same&amp;nbsp;program.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Virtualising processes vs virtualising&amp;nbsp;hardware&lt;/h2&gt;
&lt;p&gt;So it looks like we need something more. We need a programming language that can be interpreted into intermediate instructions (these are called &lt;em&gt;bytecode&lt;/em&gt;), and we need a program for each &lt;span class="caps"&gt;OS&lt;/span&gt; that can carry out these intermediate&amp;nbsp;instructions.&lt;/p&gt;
&lt;p&gt;That program is also called a virtual machine (&lt;span class="caps"&gt;VM&lt;/span&gt;), but this is not hardware virtualisation. It is &lt;em&gt;process&lt;/em&gt;&amp;nbsp;virtualisation.&lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;OS&lt;/span&gt; interacts with virtual hardware without directly accessing the underlying hardware. Similarly, we want our code to interact with a virtual process instead of using &lt;span class="caps"&gt;OS&lt;/span&gt; libraries&amp;nbsp;directly.&lt;/p&gt;
&lt;p&gt;This is how the Java programming language works. It provides a (process) &lt;span class="caps"&gt;VM&lt;/span&gt;, called the Java &lt;span class="caps"&gt;VM&lt;/span&gt;, for each operating system. Your Java program interacts with the Java &lt;span class="caps"&gt;VM&lt;/span&gt; only; it mediates all access to the operating&amp;nbsp;system.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; System VMs provide a set of virtualised hardware that the &lt;span class="caps"&gt;OS&lt;/span&gt; interacts with. Process VMs provide a set of libraries that a program (written in that programming language) interacts&amp;nbsp;with.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 151: the Java &lt;span class="caps"&gt;VM&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The idea sounds pretty cool &amp;#8230; so why don’t we write more programs this way? And what &lt;em&gt;is&lt;/em&gt; the Java &lt;span class="caps"&gt;VM&lt;/span&gt;&amp;nbsp;anyway?&lt;/p&gt;
&lt;p&gt;Light discussion on code-adjacent issues next&amp;nbsp;issue!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;As opposed to &lt;strong&gt;compiled programming languages&lt;/strong&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue054.html"&gt;Issue 54&lt;/a&gt;)), where code is compiled into &lt;span class="caps"&gt;CPU&lt;/span&gt; instructions for one hardware platform for one &lt;span class="caps"&gt;OS&lt;/span&gt; only.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 12"></category></entry><entry><title>Issue 149: History of commercial computing - containerisation</title><link href="https://ngjunsiang.github.io/laymansguide/issue149.html" rel="alternate"></link><published>2021-12-04T08:00:00+08:00</published><updated>2021-12-04T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-12-04:/laymansguide/issue149.html</id><summary type="html">&lt;p&gt;Containers are one layer of virtualisation above virtual machines: containerisation systems virtualise access to the operating system, presenting a virtual interface that provides software with the resources it needs, without being aware of software running in other containers on the same&amp;nbsp;system.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Renting out virtual hardware instead of physical hardware meant that instead of having to move hardware around and manage it, you could send the &lt;em&gt;data&lt;/em&gt; for running an &lt;span class="caps"&gt;OS&lt;/span&gt; to the hosting company and have them be responsible for hardware&amp;nbsp;operations.&lt;/p&gt;
&lt;h2&gt;Business&amp;nbsp;concerns&lt;/h2&gt;
&lt;p&gt;Every business computer you have encountered likely runs an operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;). And yet, what value does managing the &lt;span class="caps"&gt;OS&lt;/span&gt; have for the business? They have business software to run—point-of-sale systems, accounting systems, communication systems e.g. email—but the &lt;span class="caps"&gt;OS&lt;/span&gt; is no big concern for them, as long as it runs the&amp;nbsp;software!&lt;/p&gt;
&lt;p&gt;If I have point-of-sale software that only runs in Windows, and I’m paying for a company to provide it as a service, I don’t care if the software actually runs on Windows with direct hardware access, or if it is doing it through a virtual machine, &lt;em&gt;so long as it works&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;If I am going to help other companies run point-of-sale systems, one way to do it securely (so that data from different terminals/companies do not mix) would be to run a separate virtual machine (&lt;span class="caps"&gt;VM&lt;/span&gt;) for each system. I would do this through a &lt;a href="https://en.wikipedia.org/wiki/Hypervisor"&gt;hypervisor&lt;/a&gt;, a specialised &lt;em&gt;thing&lt;/em&gt; (software/firmware/hardware) meant for running&amp;nbsp;VMs.&lt;/p&gt;
&lt;h2&gt;Virtualising&amp;nbsp;OSes&lt;/h2&gt;
&lt;p&gt;This is kind of wasteful; I am running multiple versions of the same &lt;span class="caps"&gt;OS&lt;/span&gt; to support as many copies of the point-of-sale system, even though they can all run on the same &lt;span class="caps"&gt;OS&lt;/span&gt;. I am just loathe to do so for security&amp;nbsp;reasons.&lt;/p&gt;
&lt;p&gt;What if we could apply virtualisation one layer up: instead of just virtualising hardware, we &lt;em&gt;virtualise the &lt;span class="caps"&gt;OS&lt;/span&gt;&lt;/em&gt;?&lt;/p&gt;
&lt;p&gt;To recap: when we virtualise the hardware, we provide virtual drivers that the &lt;span class="caps"&gt;OS&lt;/span&gt; can accept as valid&amp;nbsp;hardware.&lt;/p&gt;
&lt;p&gt;But programs don’t need real or virtual hardware; they need &lt;span class="caps"&gt;OS&lt;/span&gt; libraries which provide common resources and services: network, storage, compute, memory, windowing/display. If we can provide &lt;em&gt;virtual libraries&lt;/em&gt; which respond like the actual &lt;span class="caps"&gt;OS&lt;/span&gt; libraries would, the programs would be able to run as&amp;nbsp;normal.&lt;/p&gt;
&lt;p&gt;The technology that enables OSes to let programs think they are running exclusively, protected from other programs, is called &lt;span class="caps"&gt;OS&lt;/span&gt;-level virtualisation, but more widely referred to as &lt;strong&gt;containerisation&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;An &lt;span class="caps"&gt;OS&lt;/span&gt; (with the appropriate software &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; support) can run multiple &lt;strong&gt;containers&lt;/strong&gt;, each container acting like a sandbox (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue092.html"&gt;Issue 92&lt;/a&gt;)) for the software within. In each container, software has access to &lt;span class="caps"&gt;OS&lt;/span&gt; features, but are unable to affect software outside of the container. Each container appears to have exclusive access to (a portion of) the system’s network, storage, compute, and memory&amp;nbsp;resources.&lt;/p&gt;
&lt;h2&gt;Managing&amp;nbsp;containers&lt;/h2&gt;
&lt;p&gt;The word &lt;strong&gt;containers&lt;/strong&gt; may seem like a misnomer, for what are effectively software wrappers. But these do work almost like shipping containers: set up a container in an &lt;span class="caps"&gt;OS&lt;/span&gt;, install the required software, configure it, and now you have a container ready to meet business needs. You could send this container to any virtualisation service, they drop that container into their hosting system, and it runs like you&amp;nbsp;expect.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Containers are one layer of virtualisation above virtual machines: containerisation systems virtualise access to the operating system, presenting a virtual interface that provides software with the resources it needs, without being aware of software running in other containers on the same&amp;nbsp;system.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 150: System vs Process&amp;nbsp;VMs&lt;/p&gt;
&lt;p&gt;Up to this point, we have been looking at two different kinds of virtualisation: system virtualisation (virtualising hardware), and containerisation (virtualising operating system&amp;nbsp;environments).&lt;/p&gt;
&lt;p&gt;Next issue, we examine a third kind: process&amp;nbsp;virtualisation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 12"></category></entry><entry><title>Issue 148: History of commercial computing - cohosting</title><link href="https://ngjunsiang.github.io/laymansguide/issue148.html" rel="alternate"></link><published>2021-11-27T08:00:00+08:00</published><updated>2021-11-27T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-11-27:/laymansguide/issue148.html</id><summary type="html">&lt;p&gt;Renting out virtual hardware instead of physical hardware meant that instead of having to move hardware around and manage it, you could send the &lt;em&gt;data&lt;/em&gt; for running an &lt;span class="caps"&gt;OS&lt;/span&gt; to the hosting company and have them be responsible for hardware&amp;nbsp;operations.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Running a virtual machine is like running a physical machine, but within a window in your &lt;span class="caps"&gt;OS&lt;/span&gt;.&lt;/p&gt;
&lt;h2&gt;Co-located&amp;nbsp;hosting&lt;/h2&gt;
&lt;p&gt;A not-so-long time ago, to run a website, you literally just ran a webserver on your desktop, connected it to the internet, and gave your &lt;span class="caps"&gt;IP&lt;/span&gt; address to other people. This is a pretty unreliable way to host a business website though. A big company would make business arrangements to procure a reliable internet connection, set up the infrastructure (power, cooling, mounting hardware) required to run multiple computers, and then manage their multiple systems with a full &lt;span class="caps"&gt;IT&lt;/span&gt; management team (hardware &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt;&amp;nbsp;software).&lt;/p&gt;
&lt;p&gt;Not every company can afford this. Smaller companies would therefore &lt;strong&gt;co-locate&lt;/strong&gt; their computers (called colo boxes) with bigger companies, enjoying service support and infrastructure for a monthly/yearly fee. Some companies decided to just provide these services as their full-time business, and the hosting business was&amp;nbsp;born.&lt;/p&gt;
&lt;h2&gt;The difficulties of troubleshooting&amp;nbsp;remotely&lt;/h2&gt;
&lt;p&gt;Running your computer on someone else’s premises is no joke. If something went wrong, there was no way to do troubleshooting remotely. You had to drive down and do the troubleshooting onsite, usually wasting at least half a day in the process. Unsurprisingly, this was a problem many companies were happy to abstract away by paying more money. Soon, hosting companies offered to rent your &lt;em&gt;their&lt;/em&gt; computers, configured to standard specs, &lt;em&gt;and&lt;/em&gt; provide basic onsite troubleshooting. You would email/send them the software you wanted to run on those computers, with instructions, and they would do it for you. A huge timesaver; most hardware issues are now out of mind for business&amp;nbsp;owners.&lt;/p&gt;
&lt;p&gt;You still had to worry about &lt;span class="caps"&gt;OS&lt;/span&gt; issues though. If you need to have multiple pieces of software set up and configured, this was something a hosting company could not do for you. And this was where virtual machines (VMs) came in handy. What if you could set up a virtual machine, with virtual &lt;span class="caps"&gt;CPU&lt;/span&gt; and virtual memory, install your &lt;span class="caps"&gt;OS&lt;/span&gt; on a virtual disk, install all your required software in that &lt;span class="caps"&gt;OS&lt;/span&gt;, and then send that virtual disk (as data) to a hosting company? They would then run that disk through their hypervisor, a piece of software that manages virtual&amp;nbsp;machines.&lt;/p&gt;
&lt;h2&gt;The promise of&amp;nbsp;virtualisation&lt;/h2&gt;
&lt;p&gt;This was the promise that virtualisation companies offered to businesses. You could manage your virtual machines remotely, choosing when to boot them up or shut them off, paying only for virtual hardware you requested, without affecting the virtual machines of other companies. Hosting companies could “collect rent” for multiple VMs running on a single&amp;nbsp;computer.&lt;/p&gt;
&lt;p&gt;After all, as long as you have an interface to manage it, and you are able to set up your software on it, does it really matter whether it is a physical or virtual&amp;nbsp;machine?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Renting out virtual hardware instead of physical hardware meant that instead of having to move hardware around and manage it, you could send the &lt;em&gt;data&lt;/em&gt; for running an &lt;span class="caps"&gt;OS&lt;/span&gt; to the hosting company and have them be responsible for hardware&amp;nbsp;operations.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 149: History of commercial computing -&amp;nbsp;containerisation&lt;/p&gt;
&lt;p&gt;Can we push this further? Could we get hosting companies to not only help us run the hardware, but the operating system as well? Yes, yes we&amp;nbsp;can!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 12"></category></entry><entry><title>Issue 147: Operating systems on virtual hardware</title><link href="https://ngjunsiang.github.io/laymansguide/issue147.html" rel="alternate"></link><published>2021-11-20T08:00:00+08:00</published><updated>2021-11-20T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-11-20:/laymansguide/issue147.html</id><summary type="html">&lt;p&gt;Running a virtual machine is like running a physical machine, but within a window in your &lt;span class="caps"&gt;OS&lt;/span&gt;.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Virtual hardware can be created in the form of drivers that respond to a program’s requests for hardware resources. If a bootup program enumerates hardware devices and receives a response, then as long as it continues to receive valid and correct responses, it can work with the virtual hardware to run an operating&amp;nbsp;system.&lt;/p&gt;
&lt;p&gt;So &amp;#8230; what is it like to run an operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;) on virtual hardware? I promised screenshots, but they probably won’t be as exciting as you expect—it looks quite&amp;nbsp;normal!&lt;/p&gt;
&lt;h2&gt;Creating a virtual&amp;nbsp;machine&lt;/h2&gt;
&lt;p&gt;I don’t want to purchase a VMware license, so I will be using an alternative virtual machine product instead: Oracle’s free Virtualbox. This is what it looks like, running on Arch Linux on my&amp;nbsp;laptop:&lt;/p&gt;
&lt;p&gt;&lt;img alt="screenshot of Virtualbox in Arch Linux" src="https://ngjunsiang.github.io/laymansguide/issue147_01.png" /&gt;&lt;br /&gt;
&lt;em&gt;Virtualbox main&amp;nbsp;interface&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Let’s create a new virtual machine. It asks which &lt;span class="caps"&gt;OS&lt;/span&gt; I intend to use, presumably so it can pick the right virtual hardware to use (MacOS virtual machines may be more picky? I’m not too sure here either). I don’t have a valid Windows license to use, so I’m just going to demo with Arch Linux&amp;nbsp;instead.&lt;/p&gt;
&lt;p&gt;Whichever &lt;span class="caps"&gt;OS&lt;/span&gt; you pick, you are going to have to find a way to install that &lt;span class="caps"&gt;OS&lt;/span&gt; into your virtual machine—whether you are using actual boot media (such as a &lt;span class="caps"&gt;DVD&lt;/span&gt; drive), or virtual&amp;nbsp;media.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Creating a virtual machine in Virtualbox" src="https://ngjunsiang.github.io/laymansguide/issue147_02.png" /&gt;&lt;br /&gt;
&lt;em&gt;Creating a virtual&amp;nbsp;machine&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Cool, you actually get to configure how much of your computer’s memory the virtual machine will get to&amp;nbsp;use.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Configuring memory size in Virtualbox" src="https://ngjunsiang.github.io/laymansguide/issue147_03.png" /&gt;&lt;br /&gt;
&lt;em&gt;Configuring memory&amp;nbsp;size&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Without a storage disk, a computer isn’t much. Virtualbox needs &lt;em&gt;something&lt;/em&gt; it can use as a disk; the usual way is to create a file on your system that Virtualbox uses as a virtual disk. The space occupied by this virtual disk can be preallocated up front (so that your virtual machine doesn’t accidentally “run out of storage space” before the disk is full), or it can be dynamically allocated, only taking up as much space as is actually&amp;nbsp;used.&lt;/p&gt;
&lt;p&gt;I don’t have an existing virtual disk file, so I will ahead and create one here (not shown). If I have one from a previous installation, I can use it here&amp;nbsp;instead.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Configuring virtual hard disk in Virtualbox" src="https://ngjunsiang.github.io/laymansguide/issue147_04.png" /&gt;&lt;br /&gt;
&lt;em&gt;Configuring hard&amp;nbsp;disk&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;The machine is created; the main interface now looks like this. Note that all the “hardware” you see there is&amp;nbsp;virtual!&lt;/p&gt;
&lt;p&gt;&lt;img alt="Virtualbox main interface, with one virtual machine" src="https://ngjunsiang.github.io/laymansguide/issue147_05.png" /&gt;&lt;br /&gt;
&lt;em&gt;Virtualbox main interface, with one virtual&amp;nbsp;machine&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;If you are doing this on your own computer, at this point you might want to go into Settings and see what else you can toy around with: number of (virtual) CPUs, sharing some folders on your system with the virtual machine (they show up as shared network folders), adding more disks or even a virtual optical drive,&amp;nbsp;etc.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;Setting up boot&amp;nbsp;media&lt;/h2&gt;
&lt;p&gt;I could try to boot it now, but I already know it won’t work; there is nothing in the hard disk to boot from. With a physical computer, at this point we will attempt to install the &lt;span class="caps"&gt;OS&lt;/span&gt; from a &lt;span class="caps"&gt;DVD&lt;/span&gt; drive or flash drive. You could allow your virtual machine to access the &lt;span class="caps"&gt;DVD&lt;/span&gt; drive or flash drive in order to do this, or you can do it virtually. Most operating systems (including Windows) provide virtual boot media for installation: an &lt;span class="caps"&gt;ISO&lt;/span&gt; file is a virtual optical&amp;nbsp;disk.&lt;/p&gt;
&lt;p&gt;I can download the &lt;span class="caps"&gt;ISO&lt;/span&gt; boot file for Arch Linux, but it seems they have gotten savvier lately and actually provide virtual boot media in the form of &lt;em&gt;virtual hard disks&lt;/em&gt;! Let’s use that instead. I add it as a second virtual disk in&amp;nbsp;Virtualbox:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Virtualbox hard disk configuration" src="https://ngjunsiang.github.io/laymansguide/issue147_06.png" /&gt;&lt;br /&gt;
&lt;em&gt;Virtualbox hard disk configuration. packer-virtualbox.vmdk is the virtual boot media for Arch&amp;nbsp;Linux&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Once I start the virtual machine, it begins its boot sequence, and I interrupt it by pressing F12 to go to the boot menu (otherwise it will attempt to boot from the main virtual hard disk, and fail to detect any &lt;span class="caps"&gt;OS&lt;/span&gt;). Interesting to see here that it isn’t actually connected to a monitor: virtualbox presents a virtual display device, to which the virtual machine sends its video signal. Virtualbox captures these signals and displays them within the window instead. So you can flexibly configure the window size, and the virtual machine just thinks the display device it is connected to has been&amp;nbsp;resized.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Virtualbox boot menu" src="https://ngjunsiang.github.io/laymansguide/issue147_07.png" /&gt;&lt;br /&gt;
&lt;em&gt;Virtualbox boot menu. Through the virtual (&lt;span class="caps"&gt;AHCI&lt;/span&gt;) disk controller, the virtual machine detects two (virtual) disks: the new disk I created, and the Arch Linux boot media that I&amp;nbsp;loaded.&lt;/em&gt;    &lt;/p&gt;
&lt;h2&gt;Running a virtual&amp;nbsp;machine&lt;/h2&gt;
&lt;p&gt;I select the second disk to boot up Arch Linux for installation, and the login prompt&amp;nbsp;appears:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Virtualbox login prompt" src="https://ngjunsiang.github.io/laymansguide/issue147_08.png" /&gt;&lt;br /&gt;
&lt;em&gt;The Arch Linux login prompt, in Virtualbox. Yes, this is how Arch Linux gets installed the first&amp;nbsp;time.&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;I wont go any further at this point, because then I’d just be showing you how to set up Arch Linux. But I trust this is enough to give you an idea: running a virtual machine feels just like running a physical machine, but in a&amp;nbsp;window!&lt;/p&gt;
&lt;p&gt;Okay hold on, how do we shut this thing down? It is not recommended for a computer to be unplugged without a proper shutdown, so &amp;#8230; how do we do that with a virtual&amp;nbsp;machine?&lt;/p&gt;
&lt;p&gt;Even the powerdown and reset buttons on a computer are actually hardware signals which the &lt;span class="caps"&gt;OS&lt;/span&gt; receives and uses to trigger a series of actions. We can send these signals virtually too, through Virtualbox’s Machine&amp;nbsp;menu:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Virtualbox Machine menu" src="https://ngjunsiang.github.io/laymansguide/issue147_09.png" /&gt;&lt;br /&gt;
&lt;em&gt;The Virtualbox Machine&amp;nbsp;menu.&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;And with that, let’s shut it&amp;nbsp;down.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Running a virtual machine is like running a physical machine, but within a window in your &lt;span class="caps"&gt;OS&lt;/span&gt;.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 148: History of commercial computing -&amp;nbsp;cohosting&lt;/p&gt;
&lt;p&gt;Right, so this has been really cool and all, but not something a layperson would use on a daily basis usually. And the setup still seems &amp;#8230; rather technical? So why does this deserve its own&amp;nbsp;season?&lt;/p&gt;
&lt;p&gt;Remember that this happened before the turn of the century, so the tech industry has had two decades to figure out how to make money out of this. And two decades is the equivalent of a whole lifetime in this industry. How did virtualisation change the landscape of commercial computing? Time to take another walk in recent&amp;nbsp;history.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 12"></category></entry><entry><title>Issue 146: Virtual hardware</title><link href="https://ngjunsiang.github.io/laymansguide/issue146.html" rel="alternate"></link><published>2021-11-13T08:00:00+08:00</published><updated>2021-11-13T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-11-13:/laymansguide/issue146.html</id><summary type="html">&lt;p&gt;Virtual hardware can be created in the form of drivers that respond to a program’s requests for hardware resources. If a bootup program enumerates hardware devices and receives a response, then as long as it continues to receive valid and correct responses, it can work with the virtual hardware to run an operating&amp;nbsp;system.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Programs do not usually deal with the gnarly details of hardware, but instead access it through an interface. They access storage devices through a filesystem, and access hardware through&amp;nbsp;drivers.&lt;/p&gt;
&lt;p&gt;How does one trick an operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;) into coexisting with other operating systems on a single machine? By &lt;em&gt;virtualising&lt;/em&gt; hardware into virtual&amp;nbsp;drivers!&lt;/p&gt;
&lt;h2&gt;Virtual network&amp;nbsp;hardware&lt;/h2&gt;
&lt;p&gt;Let’s take an example. Take a look at your network devices: there&amp;#8217;s one for your &lt;span class="caps"&gt;LAN&lt;/span&gt; port, there’s one for your wifi card, and these days there may be one each for your Bluetooth chip and 4G/5G modem&amp;nbsp;too.&lt;/p&gt;
&lt;p&gt;If your workplace requires a &lt;span class="caps"&gt;VPN&lt;/span&gt;, you may have noticed that it adds a new network device. How is this possible if you don’t actually have any new network&amp;nbsp;hardware?&lt;/p&gt;
&lt;p&gt;Remember that in an operating system, access to hardware is mediated through drivers. A driver decides how to present its interfaces to the operating system. In essence, nothing prevents a driver from presenting multiple interfaces to the &lt;span class="caps"&gt;OS&lt;/span&gt;, provided the driver is able to receive requests and respond to&amp;nbsp;them.&lt;/p&gt;
&lt;p&gt;A &lt;span class="caps"&gt;VPN&lt;/span&gt; uses its own driver to present an additional interface to the &lt;span class="caps"&gt;OS&lt;/span&gt;, and that is how we end up with “virtual&amp;nbsp;hardware”.&lt;/p&gt;
&lt;h2&gt;Virtual&amp;nbsp;storage&lt;/h2&gt;
&lt;p&gt;We are used to thinking of storage as referring to a hard disk, or solid state disk. But technically anything that is capable of representing bits can be used as storage—with an appropriate&amp;nbsp;driver.&lt;/p&gt;
&lt;p&gt;Some operating systems/programs provide drivers for &lt;span class="caps"&gt;RAM&lt;/span&gt; disks—a storage disk that uses computer memory. These appear as a normal disk drive (in Windows) or mountpoint (in Linux). Managing files in a &lt;span class="caps"&gt;RAM&lt;/span&gt; disk is speedy, because computer memory is much faster than a storage&amp;nbsp;device.&lt;/p&gt;
&lt;h2&gt;Virtual&amp;nbsp;memory&lt;/h2&gt;
&lt;p&gt;In &lt;a href="https://ngjunsiang.github.io/laymansguide/issue055.html"&gt;Issue 55&lt;/a&gt;), I explained how the operating system offers and controls access to computer memory, the pagefile (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue117.html"&gt;Issue 117&lt;/a&gt;)), as well as hardware devices through a single addressing interface: virtual&amp;nbsp;memory.&lt;/p&gt;
&lt;p&gt;When a program requests access to the printer and the &lt;span class="caps"&gt;OS&lt;/span&gt; responds with “here, you can send your request to memory address 0x35a4b2ff”, how is it to know if the data is going to a physical printer, or to a virtual one&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;?&lt;/p&gt;
&lt;h2&gt;Virtual&amp;nbsp;hardware&lt;/h2&gt;
&lt;p&gt;Take a look at your Device Manager in Control Panel. What do you&amp;nbsp;see?&lt;/p&gt;
&lt;p&gt;&lt;img alt="screenshot of Device Manager in Windows 10" src="https://ngjunsiang.github.io/laymansguide/issue146_01.jpg" /&gt;&lt;br /&gt;
&lt;small&gt;Device Manager in Windows 10&lt;br /&gt;Note that what you are seeing are not the actual hardware (which the &lt;span class="caps"&gt;OS&lt;/span&gt; cannot possibly know). These are interfaces to the hardware.&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;A whole set of drivers and interfaces which the &lt;span class="caps"&gt;OS&lt;/span&gt; uses to carry out its&amp;nbsp;work.&lt;/p&gt;
&lt;p&gt;Many of these were initialised during bootup (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue112.html"&gt;Issue 112&lt;/a&gt;)), when the &lt;span class="caps"&gt;OS&lt;/span&gt; kernel (the core of the &lt;span class="caps"&gt;OS&lt;/span&gt;) enumerates the available hardware by sending out signals and seeing what hardware&amp;nbsp;responds.&lt;/p&gt;
&lt;p&gt;So a bunch of engineers at VMware thought: what if we &amp;#8230; made drivers to present virtual hardware emulating the &lt;span class="caps"&gt;CPU&lt;/span&gt;, memory, storage devices, &amp;#8230; and even the chipset? What if we then we booted the &lt;span class="caps"&gt;BIOS&lt;/span&gt; (the bootup program loaded on a computer’s mainboard; see &lt;a href="https://ngjunsiang.github.io/laymansguide/issue112.html"&gt;Issue 112&lt;/a&gt;)), got the virtual hardware to respond when the &lt;span class="caps"&gt;BIOS&lt;/span&gt; enumerates hardware, and then basically simulated all the signals that hardware would actually&amp;nbsp;send?&lt;/p&gt;
&lt;p&gt;We end up with a virtual machine—one that you can actually install an &lt;span class="caps"&gt;OS&lt;/span&gt;&amp;nbsp;on!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Virtual hardware can be created in the form of drivers that respond to a program’s requests for hardware resources. If a bootup program enumerates hardware devices and receives a response, then as long as it continues to receive valid and correct responses, it can work with the virtual hardware to run an operating&amp;nbsp;system.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 147: Operating systems on virtual&amp;nbsp;hardware&lt;/p&gt;
&lt;p&gt;So &amp;#8230; what is it like to run an operating system on virtual hardware? Screenshots&amp;nbsp;incoming!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;These virtual printers do, in fact, exist. It is why some OSes offer a “Print to &lt;span class="caps"&gt;PDF&lt;/span&gt;” printer device: the program effectively sends print commands to another program, which interprets the commands to produce a &lt;span class="caps"&gt;PDF&lt;/span&gt; file. This is possible because both printers and the &lt;span class="caps"&gt;PDF&lt;/span&gt; format share a common language: Postscript (see &lt;a href="https://ngjunsiang.github.io/laymansguide/issue051.html"&gt;Issue 51&lt;/a&gt;)).&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 12"></category><category term="virtualisation"></category></entry><entry><title>Issue 145: What an app wants, what an app needs</title><link href="https://ngjunsiang.github.io/laymansguide/issue145.html" rel="alternate"></link><published>2021-11-06T08:00:00+08:00</published><updated>2021-11-06T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-11-06:/laymansguide/issue145.html</id><summary type="html">&lt;p&gt;Programs do not usually deal with the gnarly details of hardware, but instead access it through an interface. They access storage devices through a filesystem, and access hardware through&amp;nbsp;drivers.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; In 1999, VMware launched VMware Workstation, which allowed multiple operating systems to run off a single&amp;nbsp;machine.&lt;/p&gt;
&lt;p&gt;In Season 5 (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue054.html"&gt;Issue 53&lt;/a&gt;)), I went into some detail on how our programs work. The programming language they are written in gets &lt;em&gt;compiled&lt;/em&gt; into &lt;span class="caps"&gt;CPU&lt;/span&gt; instructions, which get carried out by the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;But the program does not handle everything on its own. In fact, it does not usually have direct access to hardware (unless requested from and provided by the operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;)). All such access is abstracted and mediated through the operating system. The program requests and receives memory space, reads and writes files, and processes keyboard/mouse input—through the operating&amp;nbsp;system.&lt;/p&gt;
&lt;p&gt;How does the program know it is not living in a simulation? If the environment it operates in responds to its requests, the program continues running without a care. A keypress event is a keypress event, and the program can respond to it, whether it really came from a keyboard or not. A file that has data to read can be treated like a file, whether it is really stored on the hard disk or actually streamed from cloud&amp;nbsp;storage.&lt;/p&gt;
&lt;h2&gt;Computing&amp;nbsp;interfaces&lt;/h2&gt;
&lt;p&gt;This paradigm is really powerful, because it enables us to pipe data from place to place. It enables us to build &lt;strong&gt;interfaces&lt;/strong&gt;: instead of writing a program that has to grapple with the gnarly details of files in binary format, I can write a program that deals with a &lt;em&gt;file interface&lt;/em&gt; instead. The interface lets me read and write data, while the gnarly details are handled one layer down, by the&amp;nbsp;filesystem.&lt;/p&gt;
&lt;p&gt;And this is how we can open documents from a flash drive without even realising that the flash drive uses a different filesystem from our system&amp;nbsp;disk.&lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;OS&lt;/span&gt; is just another special set of programs that mediate access to the hardware. Yet, it is easy to forget that even the &lt;span class="caps"&gt;OS&lt;/span&gt;’s many programs do not actually deal with the gnarly details of hardware, but with an &lt;em&gt;interface&lt;/em&gt; to the hardware. This interface are the drivers (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue120.html"&gt;Issue 120&lt;/a&gt;)) we keep hearing about, and which our &lt;span class="caps"&gt;OS&lt;/span&gt; keeps telling us could not be found or are&amp;nbsp;outdated.&lt;/p&gt;
&lt;p&gt;As long as the “drivers” respond in the right way, the operating system continues to carry out its instructions as&amp;nbsp;programmed.&lt;/p&gt;
&lt;p&gt;How does one trick an operating system into coexisting with other operating systems on a single&amp;nbsp;machine?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Programs do not usually deal with the gnarly details of hardware, but instead access it through an interface. They access storage devices through a filesystem, and access hardware through&amp;nbsp;drivers.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 146: Virtual&amp;nbsp;hardware&lt;/p&gt;
&lt;p&gt;Examples&amp;nbsp;incoming!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 12"></category><category term="app"></category><category term="operating system"></category></entry><entry><title>Issue 144: Programs-in-a-vat</title><link href="https://ngjunsiang.github.io/laymansguide/issue144.html" rel="alternate"></link><published>2021-10-30T08:00:00+08:00</published><updated>2021-10-30T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-10-30:/laymansguide/issue144.html</id><summary type="html">&lt;p&gt;n 1999, VMware launched VMware Workstation, which allowed multiple operating systems to run off a single&amp;nbsp;machine.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; The Apple M1 is a souped-up iPhone processor, with unified&amp;nbsp;memory.&lt;/p&gt;
&lt;p&gt;I want to circle back to talking about processors again in this season, because there are a couple of pretty world-shaking ideas I haven’t fully fleshed out in Layman’s Guide&amp;nbsp;yet.&lt;/p&gt;
&lt;p&gt;One of them is—hmm where do I begin. As early as 1641, in Meditations on First Philosophy, Descartes proposes that “All that up to the present time I have accepted as most true and certain I have learned either from the senses or through the senses; but it is sometimes proved to me that these senses are deceptive, and it is wiser not to trust entirely to anything by which we have once been deceived.” In other words, Descartes isn’t always sure that he believes what he sees; his senses sometimes deceive him about the nature of&amp;nbsp;reality.&lt;/p&gt;
&lt;p&gt;More than three centuries later, in 1999, the Wachowski brothers translate this idea into a more modern form: what if the world as we know it is a simulation running on some other cosmic, otherworldly hardware? Is it possible to signal to our senses so convincingly that a simulacrum may be thought of as&amp;nbsp;real?&lt;/p&gt;
&lt;p&gt;Hold that thought, because this is the Layman’s Guide to Computing, not Philosophy (although there is plenty of that in Computing as well!). For millennials like me who came of age in the early noughties, The Matrix defined the zeitgeist for the next couple of decades, whatever one may think of its aesthetics. It is difficult to overcredit it with&amp;nbsp;this.&lt;/p&gt;
&lt;h2&gt;VMware is&amp;nbsp;founded&lt;/h2&gt;
&lt;p&gt;But allow me to put a little dent in that reputation: the year is 1998, in Palo Alto, California. A company named VMware had just been founded. One year later, in 1999, they demoed and launched &lt;a href="https://www.virten.net/2015/12/vmware-workstation-from-1999-to-2015/"&gt;VMware Workstation&lt;/a&gt;, their first product. In that demo, engineers demonstrated how VMware Workstation could allow users to run &lt;span class="caps"&gt;MS&lt;/span&gt;-&lt;span class="caps"&gt;DOS&lt;/span&gt;, Linux, FreeBSD, and multiple versions of Windows—that’s multiple operating systems (&lt;span class="caps"&gt;OS&lt;/span&gt;)—off &lt;em&gt;a single machine&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The release of VMware Workstation probably happened too late to influence the direction of The Matrix, but no doubt there was talk in the air of &lt;em&gt;virtualising OSes&lt;/em&gt; in the years leading up to it. To be clear, this is not dual-booting, in which a user can choose, through a boot menu, which &lt;span class="caps"&gt;OS&lt;/span&gt; to boot into (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue112.html"&gt;Issue 112&lt;/a&gt;). We are talking about &lt;em&gt;multiple&lt;/em&gt; OSes, running &lt;em&gt;simultaneously&lt;/em&gt;, off &lt;em&gt;a single machine&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;How does such a thing&amp;nbsp;happen?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; In 1999, VMware launched VMware Workstation, which allowed multiple operating systems to run off a single&amp;nbsp;machine.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 145: What an app wants, what an app&amp;nbsp;needs&lt;/p&gt;
&lt;p&gt;What enables an operating system to function, convinced it has full control of hardware? For that matter, how would it even know if the hardware &amp;#8230; is really&amp;nbsp;hardware?&lt;/p&gt;
&lt;p&gt;&amp;#8230;&amp;nbsp;👻&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 12"></category></entry><entry><title>Issue 143: Implications (Part 2) – Future Goals</title><link href="https://ngjunsiang.github.io/laymansguide/issue143.html" rel="alternate"></link><published>2021-10-23T08:00:00+08:00</published><updated>2021-10-23T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-10-23:/laymansguide/issue143.html</id><summary type="html"></summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Using the same hardware for both smartphones and laptops would make it much easier to write apps for both platforms. The closer they are in features, hardware, and software support, the easier things will be for&amp;nbsp;developers.&lt;/p&gt;
&lt;p&gt;So, let’s get some Likely-Asked-Questions (LAQs) out of the way in this last&amp;nbsp;issue.&lt;/p&gt;
&lt;h2&gt;If developing for a single chip makes things much easier for developers, can we just decide to develop everything for the M1 chip and somehow force Apple to share the chip&amp;nbsp;design?&lt;/h2&gt;
&lt;p&gt;You know as well as I do that the answer is no. Besides, Apple doesn’t care about the hardware needs of devices other than its own. You want other features they don’t care about? Too&amp;nbsp;bad.&lt;/p&gt;
&lt;h2&gt;So only Apple users get to enjoy unified&amp;nbsp;memory?&lt;/h2&gt;
&lt;p&gt;Yes, for now. We’ll need to wait a few years for other chipmakers to figure out their own hardware&amp;nbsp;implementations.&lt;/p&gt;
&lt;h2&gt;Who’s likely to achieve it&amp;nbsp;first?&lt;/h2&gt;
&lt;p&gt;This’ll have to be a company that designs its own CPUs and&amp;nbsp;GPUs.&lt;/p&gt;
&lt;p&gt;The incentives don’t align for Intel. Their main business has never relied on capable graphics, and they are much more concerned with saving the server market from &lt;span class="caps"&gt;ARM&lt;/span&gt;’s and &lt;span class="caps"&gt;AMD&lt;/span&gt;’s&amp;nbsp;encroachment.&lt;/p&gt;
&lt;p&gt;What about &lt;span class="caps"&gt;AMD&lt;/span&gt;? After all, they were one of the earliest companies to push for a similar idea: Heterogeneous System Architecture. And they achieved it to a lesser extent, with their Accelerated Processing Units (see &lt;a href="https://ngjunsiang.github.io/laymansguide/issue142.html"&gt;Issue 142&lt;/a&gt;); a fancy term for &lt;span class="caps"&gt;CPU&lt;/span&gt;+&lt;span class="caps"&gt;GPU&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;AMD&lt;/span&gt; has made this possible in software; that means as a programmer, you can command the &lt;span class="caps"&gt;CPU&lt;/span&gt; to store data at a memory address, pass that address to the &lt;span class="caps"&gt;GPU&lt;/span&gt;, and then get the &lt;span class="caps"&gt;GPU&lt;/span&gt; to retrieve data from that address. But in practice, benchmarks show that passing data this way falls short of the actual throughput that would be expected; possibly the hardware support is just not there yet, and not easy enough to&amp;nbsp;use.&lt;/p&gt;
&lt;p&gt;But the incentives line up quite well for &lt;span class="caps"&gt;AMD&lt;/span&gt;. If they achieve it, the performance of their APUs, their mid-range product, will see a significant boost. But they will need significant influence with developers to develop software development kits (SDKs) that developers can use to take advantage of unified memory, and that’s a big investment of&amp;nbsp;resources.&lt;/p&gt;
&lt;p&gt;Nvidia is putting a lot of effort into catching up on the &lt;span class="caps"&gt;CPU&lt;/span&gt; side of things, and they have been pushing lots of &lt;span class="caps"&gt;ARM&lt;/span&gt; chip designs to complement their strength in graphics cards. They have also recently bought &lt;span class="caps"&gt;ARM&lt;/span&gt;, so they also seem like a strong contender to implement unified memory. My gut sense is that it is not high on their priority list, as their primary business is still parallel computing and related applications, such as machine learning and scientific&amp;nbsp;computing.&lt;/p&gt;
&lt;p&gt;The work for this will have to be ongoing, of course, and likely started since 2015 or so; starting in 2021 is way too&amp;nbsp;late!&lt;/p&gt;
&lt;h2&gt;What does this mean for&amp;nbsp;Apple?&lt;/h2&gt;
&lt;p&gt;They are now almost fully in control of their own hardware and software. The main limitations where their control does not reach is their cloud computing (where iCloud happens), and the manufacturing (likely still &lt;span class="caps"&gt;TSMC&lt;/span&gt; in the near future). Their concerns now will be much more international than&amp;nbsp;before.&lt;/p&gt;
&lt;h2&gt;Should we expect to see unified memory on non-Apple&amp;nbsp;chips?&lt;/h2&gt;
&lt;p&gt;Yes, definitely, it’s something the industry has been working towards, just way too slowly &amp;#8230; and hopefully the M1’s existence will put some pressure on those development&amp;nbsp;timelines.&lt;/p&gt;
&lt;p&gt;I suspect the main cause of inertia is all the legacy software that still has to be supported. Because Intel and &lt;span class="caps"&gt;AMD&lt;/span&gt; have a lot of business riding on keeping compatibility with past hardware, they can&amp;#8217;t make sweeping changes across their entire range of products, unlike Apple. Every change that is made to an existing line of chips has to still keep it working when customers run their existing&amp;nbsp;software.&lt;/p&gt;
&lt;h2&gt;How does this affect&amp;nbsp;consumers?&lt;/h2&gt;
&lt;p&gt;Probably not much effect, beyond the gradual speed gains from generation to generation that we are already&amp;nbsp;seeing.&lt;/p&gt;
&lt;p&gt;The more significant effect is, I think, the miniaturisation of mobile systems. Already the mainboard for a laptop like the Macbook has shrunk to a narrow rectangle; most of the space for devices is now taken up by energy storage (i.e. batteries). The limiting factor now seems to be energy density: how many grams of batteries we will need per hour of laptop use. I suspect this is going to keep laptops more or less at the same size; the laptop is a mature form factor at this point and will gradually&amp;nbsp;age.&lt;/p&gt;
&lt;p&gt;What’s more exciting is when unified memory architectures can be miniaturised sufficiently for wearables. We are going to need that if we want augmented reality (&lt;span class="caps"&gt;AR&lt;/span&gt;) systems, e.g. graphics projected directly on a lens in front of our eyes, in a compact form factor. Many virtual reality (&lt;span class="caps"&gt;VR&lt;/span&gt;) and &lt;span class="caps"&gt;AR&lt;/span&gt; systems currently come in bulky designs that sit heavily on the body; there is much room for improvement&amp;nbsp;here.&lt;/p&gt;
&lt;h2&gt;Wrapping&amp;nbsp;up&lt;/h2&gt;
&lt;p&gt;This somehow ended up as a crash course in CPUs and GPUs, all in one season. I didn’t mean to carry out an industry analysis here, and this is definitely not a forecast to be relied on! It’s just a very interesting story to follow and I can’t help but think about what’s happening on multiple&amp;nbsp;levels.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S12] Issue 144:&amp;nbsp;Programs-in-a-vat&lt;/p&gt;
&lt;p&gt;How does a program on the computer know if it is in a&amp;nbsp;simulation?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 11"></category></entry><entry><title>Issue 142: Implications (Part 1) - Software</title><link href="https://ngjunsiang.github.io/laymansguide/issue142.html" rel="alternate"></link><published>2021-10-16T08:00:00+08:00</published><updated>2021-10-16T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-10-16:/laymansguide/issue142.html</id><summary type="html">&lt;p&gt;Using the same hardware for both smartphones and laptops would make it much easier to write apps for both platforms. The closer they are in features, hardware, and software support, the easier things will be for&amp;nbsp;developers.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; The Apple A14 and Apple M1 are essentially the same chip architecture: they use almost the same building blocks, just with different numbers of them. On top of that, the Apple M1 implements unified memory, allowing the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; (and other SoC components) to share the same system memory, greatly facilitating intra-chip&amp;nbsp;communication.&lt;/p&gt;
&lt;p&gt;So, before 2020: smartphones are smartphones, laptops are laptops. They use different types of CPUs with different architectures (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue141.html"&gt;Issue 141&lt;/a&gt;)) and even different instruction sets (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue053.html"&gt;Issue 53&lt;/a&gt;)). Never the twain shall&amp;nbsp;meet.&lt;/p&gt;
&lt;p&gt;After 2020: It turns out that smartphone chips can be upgraded and used in laptops, while remaining essentially the same architecture? Its power consumption dial can be turned down to almost zero but also turned all the way&amp;nbsp;up?&lt;/p&gt;
&lt;p&gt;That opens up the possibility that smartphones and laptops can run on the same hardware, and there’s nothing technically stopping apps compiled (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue054.html"&gt;Issue 54&lt;/a&gt;)) for that instruction set to run on both!&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Hmm, where has something like this happened&amp;nbsp;before?&lt;/p&gt;
&lt;h2&gt;The big console&amp;nbsp;alignment&lt;/h2&gt;
&lt;p&gt;Sometime in mid-2013, Microsoft announced the Xbox One (henceforth &lt;span class="caps"&gt;XB1&lt;/span&gt;), the successor to the Xbox 360. The 360 ran on a PowerPC &lt;span class="caps"&gt;CPU&lt;/span&gt; made by &lt;span class="caps"&gt;IBM&lt;/span&gt;—different from smartphone chips that used the &lt;span class="caps"&gt;ARM&lt;/span&gt; instruction set, and also different from laptops that use the x86 instruction set.&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;The Xbox One, on the other hand, uses a &lt;span class="caps"&gt;CPU&lt;/span&gt;+&lt;span class="caps"&gt;GPU&lt;/span&gt; made by &lt;span class="caps"&gt;AMD&lt;/span&gt;&lt;sup id="fnref:3"&gt;&lt;a class="footnote-ref" href="#fn:3"&gt;3&lt;/a&gt;&lt;/sup&gt;, following the x86 instruction&amp;nbsp;set.&lt;/p&gt;
&lt;p&gt;The Xbox One essentially uses a custom laptop&amp;nbsp;chip!&lt;/p&gt;
&lt;p&gt;This &lt;em&gt;was&lt;/em&gt; interesting news because earlier that year, in Feb 2013, Sony had announced the PlayStation 4 (&lt;span class="caps"&gt;PS4&lt;/span&gt;), which was &amp;#8230; also running on an &lt;span class="caps"&gt;AMD&lt;/span&gt; &lt;span class="caps"&gt;CPU&lt;/span&gt;+&lt;span class="caps"&gt;GPU&lt;/span&gt;! The previous iteration, the PlayStation 3 (&lt;span class="caps"&gt;PS3&lt;/span&gt;), was running on an interesting custom architecture that used PowerPC cores and a completely original &lt;span class="caps"&gt;GPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;At this point it would be oh-so-tempting, for a tech nerd, to descend into point-by-point comparisons of the hardware specifications of both consoles. We will fortunately not be doing&amp;nbsp;that.&lt;/p&gt;
&lt;p&gt;What’s more important is what this meant for the video&amp;nbsp;games.&lt;/p&gt;
&lt;h2&gt;Alignment in game&amp;nbsp;development&lt;/h2&gt;
&lt;p&gt;If you wanted to write a game for the Xbox 360, you had to learn its &lt;span class="caps"&gt;API&lt;/span&gt;: which functions to call to make it do what you want, how to store data into its storage, and so on. It’s a lot of time and effort to look at your options and figure out the limitations, and how to work around them to achieve what you want in your&amp;nbsp;game.&lt;/p&gt;
&lt;p&gt;And if you wanted to make the same game for the &lt;span class="caps"&gt;PS3&lt;/span&gt;, you now had to learn a completely different &lt;span class="caps"&gt;API&lt;/span&gt;, running on hardware with completely different limitations, and figuring out completely different approaches to achieve the same end. While the game might feel the same, the time and effort is almost as much as what it would take for a new&amp;nbsp;game!&lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;XB1&lt;/span&gt; and &lt;span class="caps"&gt;PS4&lt;/span&gt;, on the other hand, are much more similar. They both use &lt;span class="caps"&gt;AMD&lt;/span&gt; &lt;span class="caps"&gt;CPU&lt;/span&gt;+GPUs with similar architecture. While Microsoft and Sony may add their own features on top of the chips and the software, the &lt;span class="caps"&gt;API&lt;/span&gt; is ultimately guided by hardware decisions. If you made a game for &lt;span class="caps"&gt;XB1&lt;/span&gt; and wanted to port it to &lt;span class="caps"&gt;PS4&lt;/span&gt;, the effort of learning a new &lt;span class="caps"&gt;API&lt;/span&gt; is greatly&amp;nbsp;lessened.&lt;/p&gt;
&lt;h2&gt;The gulf between smartphones and&amp;nbsp;laptops&lt;/h2&gt;
&lt;p&gt;Back to smartphones vs laptops. Running on two different types of chips, using different architectures and instruction&amp;nbsp;sets.&lt;/p&gt;
&lt;p&gt;We have seen some forays from one into the other: Intel’s doomed Medfield chip was an attempt to bring the x86 architecture to smartphones, while Google has been trying to get &lt;span class="caps"&gt;ARM&lt;/span&gt; chips into Chromebooks, with limited but increasing&amp;nbsp;success.&lt;/p&gt;
&lt;p&gt;But now that we have an iPhone 12 using the A14 chip, a Macbook using the M1 chip, and we know that the A14 and M1 are essentially the same architecture and the same instruction set … it does suggest that the challenges of making software for both devices now primarily exist on the software side. The gulf of hardware incompatibility has been&amp;nbsp;closed.&lt;/p&gt;
&lt;h2&gt;Feature&amp;nbsp;alignment&lt;/h2&gt;
&lt;p&gt;The M1 chip is capable of power standby (i.e. screen off with the &lt;span class="caps"&gt;CPU&lt;/span&gt; in a low-power state), in a way that most laptop chips aren’t. This is a key feature for smartphone software and operating systems, and the M1 paves the way for laptop chip-makers to introduce this feature into their processors as&amp;nbsp;well.&lt;/p&gt;
&lt;p&gt;And the M1, being living proof that unified memory is possible, would also likely push existing companies to speed up development towards that&amp;nbsp;goal.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Using the same hardware for both smartphones and laptops would make it much easier to write apps for both platforms. The closer they are in features, hardware, and software support, the easier things will be for&amp;nbsp;developers.&lt;/p&gt;
&lt;p&gt;I was looking for a way to sneak in the &lt;span class="caps"&gt;XB1&lt;/span&gt;-and-&lt;span class="caps"&gt;PS4&lt;/span&gt; story, and I think I found just the right place for it. It really does excite me to think that one day a developer could write software for a smartphone, and it would work on laptops with minimal modification, and vice-versa. And perhaps a decade from now, we’d be scratching our heads why we even had to choose between the&amp;nbsp;two!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 143: Implications (Part 2) – Future&amp;nbsp;Goals&lt;/p&gt;
&lt;p&gt;So what’s next? Is unified memory the holy grail for hardware, and is there any further room for improvement? I’ll share some thoughts in the next&amp;nbsp;issue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Nothing, that is, besides all the software workarounds that will need to be written &amp;#8230;&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;In an interesting narrative twist, PowerPC was the architecture that Macbooks used before Apple switched them to Intel processors. And now Xbox did the same thing.&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:3"&gt;
&lt;p&gt;&lt;span class="caps"&gt;AMD&lt;/span&gt; calls it an Accelerated Processing Unit (&lt;span class="caps"&gt;APU&lt;/span&gt;). Doesn’t matter for us.&amp;#160;&lt;a class="footnote-backref" href="#fnref:3" title="Jump back to footnote 3 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 11"></category></entry><entry><title>Issue 141: The Apple A14 and M1</title><link href="https://ngjunsiang.github.io/laymansguide/issue141.html" rel="alternate"></link><published>2021-10-09T08:00:00+08:00</published><updated>2021-10-09T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-10-09:/laymansguide/issue141.html</id><summary type="html">&lt;p&gt;The Apple A14 and Apple M1 are essentially the same chip architecture: they use almost the same building blocks, just with different numbers of them. On top of that, the Apple M1 implements unified memory, allowing the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; (and other SoC components) to share the same system memory, greatly facilitating intra-chip&amp;nbsp;communication.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Shared memory is easier to implement when a company has control over the designs of both &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;So, to&amp;nbsp;recap:&lt;/p&gt;
&lt;p&gt;Most companies design either CPUs or GPUs, but seldom are well-positioned&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt; to be excellent in&amp;nbsp;both.&lt;/p&gt;
&lt;p&gt;Among the companies that design both CPUs and GPUs, almost none of them&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt; make CPUs for both mobile (smartphones + tablets) as well as laptops (including low- to mid-range desktops).&lt;sup id="fnref:3"&gt;&lt;a class="footnote-ref" href="#fn:3"&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Which leaves Apple in the (current) position of being the only chip company with a design for both mobile as well as&amp;nbsp;laptop.&lt;/p&gt;
&lt;h2&gt;The difficulties of power&amp;nbsp;scaling&lt;/h2&gt;
&lt;p&gt;It’s not that other companies have not attempted&amp;nbsp;this.&lt;/p&gt;
&lt;p&gt;They have tried to scale down laptop chips to achieve smartphone-like power consumption, but found that laptop chips can’t power down the way smartphone chips can, and require more circuitry to achieve&amp;nbsp;that.&lt;/p&gt;
&lt;p&gt;They have also tried to scale smartphone chips up to achieve laptop-like computational capacity, but found that simply pushing more electrical power doesn’t help all that much. Beyond a certain frequency limit, you simply need more bandwidth and more units, and designing a chip that easily accommodates more units like this just requires a very different&amp;nbsp;design.&lt;/p&gt;
&lt;p&gt;It seems that designing a chip that can go from 4W all the way to 65W (and possibly higher) requires intentional engineering, not simply modifying an existing smartphone/laptop chip design or bolting on/removing&amp;nbsp;features.&lt;/p&gt;
&lt;h2&gt;The Apple A14 vs the Apple M1:&amp;nbsp;similarities&lt;/h2&gt;
&lt;p&gt;Apple has managed to do just this with the Apple A14 and M1. They are, at heart, the same chip design! (In processor parlance, we say they have the same &lt;strong&gt;chip architecture&lt;/strong&gt;.)&lt;/p&gt;
&lt;p&gt;Let’s&amp;nbsp;see:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Apple A14 hardware overview" src="https://ngjunsiang.github.io/laymansguide/issue141_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;The Apple A14’s key hardware.&lt;br /&gt;Source: &lt;a href="https://www.electrony.net/350867/%D8%A7%D9%84%D9%85%D8%B9%D8%A7%D9%84%D8%AC-apple-a14-bionic-%D9%82%D8%AF-%D9%8A%D9%88%D9%81%D8%B1-%D8%A3%D8%AF%D8%A7%D8%A1%D9%8B-%D9%85%D9%85%D8%A7%D8%AB%D9%84%D8%A7%D9%8B-%D9%84%D8%A3%D8%AF%D8%A7/apple-a14/"&gt;Apparently an online Arabic image gallery site&lt;/a&gt; (I have no idea why this picture is so hard to&amp;nbsp;find!)&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;&lt;img alt="Apple M1 hardware overview" src="https://ngjunsiang.github.io/laymansguide/issue141_02.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;The Apple M1’s key hardware.&lt;br /&gt;Source: &lt;a href="https://www.techbuzzpro.com/apple-introduces-m1-5nm-octa-core-soc-for-the-mac.html"&gt;TechBuzzPro&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;We can also compare these features via&amp;nbsp;Wikipedia:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Apple&amp;nbsp;A14&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;6-core &lt;span class="caps"&gt;CPU&lt;/span&gt; (4 low-power&lt;sup id="fnref:4"&gt;&lt;a class="footnote-ref" href="#fn:4"&gt;4&lt;/a&gt;&lt;/sup&gt; cores “Icestorm”, &lt;strong&gt;2&lt;/strong&gt; high-performance “Firestorm”&amp;nbsp;cores)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;4&lt;/strong&gt;-core &lt;span class="caps"&gt;GPU&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;8&lt;/strong&gt;-core &lt;span class="caps"&gt;NPU&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;4GB&lt;/span&gt; memory (iPhone 12) / &lt;span class="caps"&gt;6GB&lt;/span&gt; memory (iPhone 12&amp;nbsp;Pro)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Apple&amp;nbsp;M1&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;8-core &lt;span class="caps"&gt;CPU&lt;/span&gt; (4 low-power&lt;sup id="fnref2:4"&gt;&lt;a class="footnote-ref" href="#fn:4"&gt;4&lt;/a&gt;&lt;/sup&gt; cores “Icestorm”, &lt;strong&gt;4&lt;/strong&gt; high-performance “Firestorm”&amp;nbsp;cores)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;8&lt;/strong&gt;-core &lt;span class="caps"&gt;GPU&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;16&lt;/strong&gt;-core &lt;span class="caps"&gt;NPU&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;8GB&lt;/span&gt; memory / &lt;span class="caps"&gt;16GB&lt;/span&gt;&amp;nbsp;memory&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Notice that at heart, they are using the same building blocks: &lt;del&gt;low-power&lt;/del&gt; high-efficiency cores, high-performance cores, &lt;span class="caps"&gt;GPU&lt;/span&gt; cores, and &lt;span class="caps"&gt;NPU&lt;/span&gt; cores (I suspect these are &lt;span class="caps"&gt;GPU&lt;/span&gt;-like cores but optimised for machine learning, i.e. they probably power Siri and other parts of the &lt;span class="caps"&gt;OS&lt;/span&gt; which lean on &lt;span class="caps"&gt;AI&lt;/span&gt; features); the A14 and M1 just has different numbers of&amp;nbsp;them.&lt;/p&gt;
&lt;p&gt;So one really amazing thing about the Apple M1 is that it is actually a boosted Apple A14: almost double the&amp;nbsp;hardware!&lt;/p&gt;
&lt;p&gt;It’s like when Magnemites join together and evolve into a Magneton&amp;nbsp;…&lt;/p&gt;
&lt;h2&gt;The Apple A14 vs the Apple M1:&amp;nbsp;differences&lt;/h2&gt;
&lt;p&gt;What’s different between the A14 and M1, besides the number of key chips? Apple isn’t forthcoming with the details, but we can guess about minor details like the image processor (for camera imaging), storage controller (the M1 can use high-power solid-state disks (SSDs) which the A14&amp;nbsp;can’t).&lt;/p&gt;
&lt;p&gt;The major difference announced between the A14 and M1 launch is that the M1 has unified&amp;nbsp;memory.&lt;/p&gt;
&lt;h2&gt;Unified memory vs &lt;span class="caps"&gt;CPU&lt;/span&gt;–&lt;span class="caps"&gt;GPU&lt;/span&gt;&amp;nbsp;transfers&lt;/h2&gt;
&lt;p&gt;Back in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue139.html"&gt;Issue 139&lt;/a&gt;), I mentioned that unified memory needs really high bandwidth to support access by the SoC&amp;nbsp;components.&lt;/p&gt;
&lt;p&gt;Today, laptop processors use an interface called PCIe to connect CPUs to GPUs. PCIe has a bandwidth of up to 16 &lt;span class="caps"&gt;GB&lt;/span&gt;/s&lt;sup id="fnref:5"&gt;&lt;a class="footnote-ref" href="#fn:5"&gt;5&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;The M1’s unified memory has a bandwidth of up to &lt;em&gt;58 &lt;span class="caps"&gt;GB&lt;/span&gt;/s&lt;/em&gt; reading from memory, and &lt;em&gt;36 &lt;span class="caps"&gt;GB&lt;/span&gt;/s&lt;/em&gt; writing to memory. Definitely an&amp;nbsp;improvement.&lt;/p&gt;
&lt;h2&gt;Unified memory: what’s yours is also&amp;nbsp;mine&lt;/h2&gt;
&lt;p&gt;The &lt;span class="caps"&gt;8GB&lt;/span&gt;/&lt;span class="caps"&gt;16GB&lt;/span&gt; of system memory is used by both &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt;. It is not partitioned at boot; both the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; (and other parts of the SoC, such as the &lt;span class="caps"&gt;NPU&lt;/span&gt;) have &lt;em&gt;full access to all system memory&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This greatly simplifies intra-chip communication, as all subchips in the SoC can request access to memory! The &lt;span class="caps"&gt;GPU&lt;/span&gt; no longer needs to keep its own (power-guzzling) memory. This reduces the motherboard space that is needed, lowers power consumption, and decreases latency for data transfer between &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue139.html"&gt;Issue 139&lt;/a&gt;)): a triple-compounding&amp;nbsp;win.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; The Apple A14 and Apple M1 are essentially the same chip architecture: they use almost the same building blocks, just with different numbers of them. On top of that, the Apple M1 implements unified memory, allowing the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; (and other SoC components) to share the same system memory, greatly facilitating intra-chip&amp;nbsp;communication.&lt;/p&gt;
&lt;p&gt;Some implications of the Apple A14–Apple M1 familial connection: the Apple M1 is truly capable of smartphone-like standby, a feature that Intel’s and &lt;span class="caps"&gt;AMD&lt;/span&gt;’s laptop chips have been striving for but not quite&amp;nbsp;achieved.&lt;/p&gt;
&lt;p&gt;It’s a lot to detail here, so instead I will do so—in a separate&amp;nbsp;issue.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 142: Implications (Part 1) -&amp;nbsp;Software&lt;/p&gt;
&lt;p&gt;Besides the reported fact that the M1 is really very fast (and yes I will spend a little time explaining just how fast), what else does this herald for expectations in the software on devices? Coming up next issue&amp;nbsp;:)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Companies that achieve both &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; excellence generally have business incentives that align with that goal (as opposed to, say, making low-power or cheap processors)&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;Intel had a short-lived but ultimately doomed attempt at a smartphone chip (it was named Medfield).&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:3"&gt;
&lt;p&gt;I‘m going to ignore servers here because I can! And because they’re not really relevant to a discussion on low-power consumer chips.&amp;#160;&lt;a class="footnote-backref" href="#fnref:3" title="Jump back to footnote 3 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:4"&gt;
&lt;p&gt;These are the same ones labelled “high-efficiency”, which is marketing speak for “designed to use very little power”&amp;#160;&lt;a class="footnote-backref" href="#fnref:4" title="Jump back to footnote 4 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;a class="footnote-backref" href="#fnref2:4" title="Jump back to footnote 4 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:5"&gt;
&lt;p&gt;Bandwidth of 16 &lt;span class="caps"&gt;GB&lt;/span&gt;/s is for PICe 3.0; PCIe 4.0 will support up to 32 &lt;span class="caps"&gt;GB&lt;/span&gt;/s, but graphics cards won’t use that much bandwidth to communicate with the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&amp;#160;&lt;a class="footnote-backref" href="#fnref:5" title="Jump back to footnote 5 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 11"></category></entry><entry><title>Issue 140: The shared memory dream</title><link href="https://ngjunsiang.github.io/laymansguide/issue140.html" rel="alternate"></link><published>2021-10-02T08:00:00+08:00</published><updated>2021-10-02T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-10-02:/laymansguide/issue140.html</id><summary type="html">&lt;p&gt;Shared memory is easier to implement when a company has control over the designs of both &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt;.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Around 2015, the high-performance computer industry quickly realised that this would be much more efficient if the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; could &lt;em&gt;share the same memory&lt;/em&gt;. This idea was labelled heterogeneous systems architecture (&lt;span class="caps"&gt;HSA&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;Let’s rewind a bit further from last issue. That was in&amp;nbsp;2015.&lt;/p&gt;
&lt;p&gt;Circa 2009, changes were happening on the desktop motherboard, as the memory controller hub (&lt;span class="caps"&gt;MCH&lt;/span&gt;) came on-board the &lt;span class="caps"&gt;CPU&lt;/span&gt; to reduce latency when communicating with memory (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue134.html"&gt;Issues 134&lt;/a&gt;)–&lt;a href="https://ngjunsiang.github.io/laymansguide/issue135.html"&gt;135&lt;/a&gt;)). But the memory chips themselves remained on the motherboard, and this was the case even in 2018, in Apple’s Macbook Air (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue136.html"&gt;Issue 136&lt;/a&gt;)).&lt;/p&gt;
&lt;h2&gt;Bringing memory&amp;nbsp;on-board&lt;/h2&gt;
&lt;p&gt;Smartphones can’t afford to do that; every bit of mainboard space is precious! The Apple A-series processors have been gradually moving more and more memory into the &lt;span class="caps"&gt;CPU&lt;/span&gt;, where it enjoys lower latency communicating with the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;In 2013, Apple released the iPhone 5S, using the Apple A7 SoC. This was Apple’s first 64-bit SoC (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue055.html"&gt;Issue 55&lt;/a&gt;)), and by this point Apple had managed to bring &lt;span class="caps"&gt;1GB&lt;/span&gt; of memory onto the SoC package. By 2018, With the Apple A12 SoC, the on-board memory had increased up to &lt;span class="caps"&gt;4GB&lt;/span&gt; on high-end iPhone X&amp;nbsp;models.&lt;/p&gt;
&lt;p&gt;So in 2015, the high-performance folks (working with workstations and servers) were dreaming of the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; sharing memory, while from 2013, in smartphones, the &lt;span class="caps"&gt;CPU&lt;/span&gt;, &lt;span class="caps"&gt;GPU&lt;/span&gt;, and system memory were already cohabiting in the same chip package! &lt;span class="caps"&gt;CPU&lt;/span&gt;, &lt;span class="caps"&gt;GPU&lt;/span&gt;, and memory all living in the same space … how does this&amp;nbsp;work?&lt;/p&gt;
&lt;h2&gt;Memory: yours or&amp;nbsp;mine?&lt;/h2&gt;
&lt;p&gt;Remember this&amp;nbsp;diagram?&lt;/p&gt;
&lt;p&gt;&lt;img alt="Chipset diagram of ATX systems for Intel Core (i-Series)" src="https://ngjunsiang.github.io/laymansguide/issue134_02.gif" /&gt;&lt;br /&gt;
&lt;em&gt;An Intel Core i-series &lt;span class="caps"&gt;ATX&lt;/span&gt; system chipset diagram.&lt;br /&gt;The &lt;span class="caps"&gt;MCH&lt;/span&gt; is merged into the &lt;span class="caps"&gt;CPU&lt;/span&gt;, but still a discrete unit.&lt;br /&gt;&lt;span class="caps"&gt;DDR&lt;/span&gt; refers to computer memory, while &lt;span class="caps"&gt;GDDR&lt;/span&gt; refers to graphics card memory (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue123.html"&gt;Issue123&lt;/a&gt;))&lt;br /&gt;Source: &lt;a href="https://arstechnica.com/gadgets/2009/09/intel-launches-all-new-pc-architecture-with-core-i5i7-cpus/"&gt;Ars&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Apple is pretty tight-lipped about the technical details of its products, but if the industry standard is anything to go by, the &lt;span class="caps"&gt;GPU&lt;/span&gt; will usually have its own memory, separate from the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;After all, CPUs and GPUs don’t do the same work, or even work the same way (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue123.html"&gt;Issue 123&lt;/a&gt;)). They use different memory, they use memory differently, they store data differently, and if they accidentally overwrote each other’s data … well, your device would just&amp;nbsp;crash.&lt;/p&gt;
&lt;p&gt;So … that on-board memory, whose is it? &lt;span class="caps"&gt;CPU&lt;/span&gt;’s, or &lt;span class="caps"&gt;GPU&lt;/span&gt;’s?&lt;/p&gt;
&lt;h2&gt;Successful sharing looks like&amp;nbsp;&amp;#8230;&lt;/h2&gt;
&lt;p&gt;One thing that makes it difficult to share memory is that the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; have to “speak the same language”; they need a common shared understanding of the workflow involved in passing data through shared&amp;nbsp;memory.&lt;/p&gt;
&lt;p&gt;This is easier to develop when a single company has control over both &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; designs. This is not always the case; many smartphones have &lt;span class="caps"&gt;CPU&lt;/span&gt; designs from one company and &lt;span class="caps"&gt;GPU&lt;/span&gt; designs from&amp;nbsp;another!&lt;/p&gt;
&lt;p&gt;For instance, the Apple A-series processors initially used GPUs from a graphics company called Imagination Technologies, designed by their PowerVR division. With a &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; from different teams, working in different ways, shared memory is not likely to happen&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;But in the A10 SoC, released in 2016, Apple had subtly started to replace parts of the &lt;span class="caps"&gt;GPU&lt;/span&gt; with their own in-house designs. The A10 would be the last in the line of the “Fusion” SoC&amp;nbsp;series.&lt;/p&gt;
&lt;p&gt;When the A11 SoC was released in late 2017—first in the “Bionic” series of SoCs—PowerVR’s &lt;span class="caps"&gt;GPU&lt;/span&gt; had been replaced by Apple’s own design&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;Apple is finally in the position of working towards shared memory with their Bionic-series SoCs, with the A14 being the fourth “Bionic”&amp;nbsp;SoC.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Shared memory is easier to implement when a company has control over the designs of both &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;The story which began in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue138.html"&gt;Issue 138&lt;/a&gt;) is coming to a close soon! Next issue, the curtain falls, the A14 and M1 are released, and Apple (probably) pulls the chip industry in a new direction&amp;nbsp;again.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 141: The Apple A14 and&amp;nbsp;M1&lt;/p&gt;
&lt;p&gt;And finally I can geek out over the A14 and M1 😎 don&amp;#8217;t worry, I’ll keep it&amp;nbsp;on-topic.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;What about other companies that had control over the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; designs? Such as &lt;span class="caps"&gt;AMD&lt;/span&gt;, Samsung, Qualcomm, &amp;#8230;? It’s a long story, and not really suitable for a layman newsletter. Sorry.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;The design is technically Apple’s, but they had been learning from many generations of working with PowerVR’s &lt;span class="caps"&gt;GPU&lt;/span&gt;, so the early initial designs are very likely heavily influenced by it.&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 11"></category><category term="memory"></category></entry><entry><title>Issue 139: What’s before this line is mine, what’s after this line is yours</title><link href="https://ngjunsiang.github.io/laymansguide/issue139.html" rel="alternate"></link><published>2021-09-25T08:00:00+08:00</published><updated>2021-09-25T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-09-25:/laymansguide/issue139.html</id><summary type="html">&lt;p&gt;Around 2015, the high-performance computer industry quickly realised that this would be much more efficient if the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; could &lt;em&gt;share the same memory&lt;/em&gt;.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; A system-on-chip (SoC) combines the core functionality of a system—processing, graphics, memory, and control—into a single chip&amp;nbsp;package.&lt;/p&gt;
&lt;p&gt;I am eager to dig into the meat of the A14 and M1! But first I must set up a&amp;nbsp;story.&lt;/p&gt;
&lt;h2&gt;The hUMA&amp;nbsp;race&lt;/h2&gt;
&lt;p&gt;Circa 2015 (actually even a couple of years before that), the industry suddenly seemed to wake up and realise that graphics cards could do a lot more than just play video games. The nature of how they work (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue121.html"&gt;Issue 121&lt;/a&gt;) &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue122.html"&gt;122&lt;/a&gt;)) makes them very amenable to solving problems in scientific computing, particularly in simulations, which use up computational resources by the petaflop, and energy by the&amp;nbsp;megawatt.&lt;/p&gt;
&lt;p&gt;In a nutshell, the problem the industry now faces is&amp;nbsp;this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The &lt;span class="caps"&gt;GPU&lt;/span&gt; is massively powerful &amp;#8230; at doing a small subset of things. You can solve scientific equations but can’t run a computer with &lt;em&gt;only&lt;/em&gt; a &lt;span class="caps"&gt;GPU&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;span class="caps"&gt;CPU&lt;/span&gt; is nimble, and much more suited for everyday tasks, like starting up a computer and connecting to multiple peripherals, and basically creating a useable digital environment for&amp;nbsp;humans.&lt;/li&gt;
&lt;li&gt;It thus makes the best sense to use the &lt;span class="caps"&gt;CPU&lt;/span&gt; to set up the heavy-lifting for the &lt;span class="caps"&gt;GPU&lt;/span&gt;, and have the &lt;span class="caps"&gt;GPU&lt;/span&gt; return the results after&amp;nbsp;computation.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Remember this diagram from &lt;a href="https://ngjunsiang.github.io/laymansguide/issue134.html"&gt;Issue 134&lt;/a&gt;)?&lt;/p&gt;
&lt;p&gt;&lt;img alt="Chipset diagram of ATX systems for Intel Core (i-Series)" src="https://ngjunsiang.github.io/laymansguide/issue134_02.gif" /&gt;&lt;/p&gt;
&lt;p&gt;Think about how information would flow&amp;nbsp;here:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The &lt;span class="caps"&gt;CPU&lt;/span&gt; requests data from the hard disks, which get put &lt;em&gt;into system memory&lt;/em&gt; (&lt;span class="caps"&gt;DDR&lt;/span&gt;; left-most&amp;nbsp;side).&lt;/li&gt;
&lt;li&gt;It carries out some pre-processing on that data to set up the task for the &lt;span class="caps"&gt;GPU&lt;/span&gt;, reading from memory, and storing the results back in&amp;nbsp;memory.&lt;/li&gt;
&lt;li&gt;The data is &lt;strong&gt;copied&lt;/strong&gt; &lt;em&gt;from system memory&lt;/em&gt; to the &lt;span class="caps"&gt;GPU&lt;/span&gt;, which stores that data &lt;em&gt;in &lt;span class="caps"&gt;GPU&lt;/span&gt; memory&lt;/em&gt; (&lt;span class="caps"&gt;GDDR&lt;/span&gt;; right-most&amp;nbsp;side).&lt;/li&gt;
&lt;li&gt;The &lt;span class="caps"&gt;GPU&lt;/span&gt; carries out the task, storing the results &lt;em&gt;in &lt;span class="caps"&gt;GPU&lt;/span&gt; memory&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;span class="caps"&gt;CPU&lt;/span&gt; requests the data from &lt;span class="caps"&gt;GPU&lt;/span&gt; memory, &lt;strong&gt;copying&lt;/strong&gt; it back &lt;em&gt;into system memory&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Are you seeing lots of wasted effort there? I bolded it in case you missed it. So much copying of&amp;nbsp;information!&lt;/p&gt;
&lt;p&gt;The high-performance computer industry quickly realised that it could be much more efficient if the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; could &lt;em&gt;share the same memory&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The information flow in this hypothetical memory-sharing system would be simplified to&amp;nbsp;this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The &lt;span class="caps"&gt;CPU&lt;/span&gt; requests data from the hard disks, which get put &lt;em&gt;into shared memory&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;span class="caps"&gt;CPU&lt;/span&gt; pre-processes the data, storing it back into shared&amp;nbsp;memory.&lt;/li&gt;
&lt;li&gt;The &lt;span class="caps"&gt;CPU&lt;/span&gt; sends &lt;em&gt;the location&lt;/em&gt; of the data to the &lt;span class="caps"&gt;GPU&lt;/span&gt;, which then reads &lt;em&gt;from shared memory&lt;/em&gt; and carries out the task, storing the results back &lt;em&gt;into shared&amp;nbsp;memory&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;The &lt;span class="caps"&gt;CPU&lt;/span&gt; retrieves the results directly &lt;em&gt;from shared memory&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We save time, bandwidth, and resources without having to copy data between &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt;, twice! The only drawback is that with so many components (&lt;span class="caps"&gt;CPU&lt;/span&gt;, &lt;span class="caps"&gt;GPU&lt;/span&gt;, and others) accessing memory at the same time, you are going to need memory with really high&amp;nbsp;bandwidth.&lt;/p&gt;
&lt;p&gt;The industry gave this dream a name. They called it &lt;a href="https://en.wikipedia.org/wiki/Heterogeneous_System_Architecture"&gt;heterogeneous system architecture (&lt;span class="caps"&gt;HSA&lt;/span&gt;)&lt;/a&gt;, using a heterogeneous unified memory architecture (hUMA) i.e. shared&amp;nbsp;memory.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Unified memory diagram from Nvidia" src="https://ngjunsiang.github.io/laymansguide/issue139_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;Nvidia’s heterogeneous unified memory architecture (&lt;span class="caps"&gt;HUMA&lt;/span&gt;) dream&lt;br /&gt;Source: &lt;a href="https://wccftech.com/intel-amd-nvidia-future-industry-hsa/2/"&gt;WCCFtech&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;It turns out that this is a pretty difficult task—consider the amount of bandwidth needed to support &lt;span class="caps"&gt;CPU&lt;/span&gt; &lt;em&gt;and&lt;/em&gt; &lt;span class="caps"&gt;GPU&lt;/span&gt; access. Today no product from any company (besides Apple) fully implements this in its SoCs (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue138.html"&gt;Issue 138&lt;/a&gt;))&amp;nbsp;yet.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Around 2015, the high-performance computer industry quickly realised that this would be much more efficient if the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; could &lt;em&gt;share the same memory&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I should stop here with this issue, and summarise the struggles of these companies in the next issue. They will make Apple’s success with the A14 &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; M1 a much more compelling read&amp;nbsp;:)&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 140: The shared memory&amp;nbsp;dream&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 11"></category><category term="memory"></category></entry><entry><title>Issue 138: System-on-Chip (SoC)</title><link href="https://ngjunsiang.github.io/laymansguide/issue138.html" rel="alternate"></link><published>2021-09-18T08:00:00+08:00</published><updated>2021-09-18T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-09-18:/laymansguide/issue138.html</id><summary type="html">&lt;p&gt;A system-on-chip (SoC) combines the core functionality of a system—processing, graphics, memory, and control—into a single chip&amp;nbsp;package.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; The M1 goes one step further: not only does it make do with fewer chips, it does so with passive&amp;nbsp;cooling.&lt;/p&gt;
&lt;p&gt;In &lt;a href="https://ngjunsiang.github.io/laymansguide/issue136.html"&gt;Issue 136&lt;/a&gt;), I showed the miniaturisation of the Macbook mainboard through a series of pictures. While the laptop has remained the same size mostly (apart from getting slimmer), that is not the case with its components. The bigger components, like memory and storage, changed from being separate discrete parts to being another component soldered directly to the&amp;nbsp;mainboard.&lt;/p&gt;
&lt;p&gt;But that only gets us so far; even in the M1 Macbook Air, the mainboard is still almost the entire length of a phone. There’s got to be something&amp;nbsp;else.&lt;/p&gt;
&lt;p&gt;Today, let’s see how the iPhone has&amp;nbsp;evolved.&lt;/p&gt;
&lt;h2&gt;What’s in a smartphone:&amp;nbsp;2008&lt;/h2&gt;
&lt;p&gt;Rewind to 2008: one year after the first-generation iPhone was launched, the iPhone 3G was released. These early smartphones let us see every little chip that was required to run a&amp;nbsp;smartphone:&lt;/p&gt;
&lt;p&gt;&lt;img alt="iPhone 3G mainboard, with parts labelled" src="https://ngjunsiang.github.io/laymansguide/issue138_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;iPhone 3G mainboard, with parts labelled&lt;br /&gt;There are lots of small, auxiliary processors around the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/Teardown/iPhone+3G+Teardown/600"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;In spirit and form, the early smartphones were a lot like the early desktop mainboards (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue132.html"&gt;Issue 132&lt;/a&gt;)): lots of chips performing highly specific&amp;nbsp;functions.&lt;/p&gt;
&lt;p&gt;After all, a smartphone has no need (or space) for a peripheral controller hub (&lt;span class="caps"&gt;PCH&lt;/span&gt;) (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue134.html"&gt;Issue 134&lt;/a&gt;)) when it does not have add-on peripherals, and no need for a memory controller hub (&lt;span class="caps"&gt;MCH&lt;/span&gt;) when it can put the memory directly on the same chip as the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;At this point, Apple was still using a &lt;span class="caps"&gt;CPU&lt;/span&gt; based on a design by &lt;span class="caps"&gt;ARM&lt;/span&gt;, and manufactured by Samsung. 2 years later, Apple had its own in-house processor: the Apple A4, their own&amp;nbsp;design.&lt;/p&gt;
&lt;h2&gt;What’s in a smartphone:&amp;nbsp;2010&lt;/h2&gt;
&lt;p&gt;This time, Apple had switched to an internal layout distinctly different from the iPhone 3G, and the basic layout (mainboard beside battery) would become a pattern for subsequent iPhone generations: battery taking up almost half the space, charging and audio circuitry at the bottom near the charging port, camera and antennas near the top, and everything else beside the&amp;nbsp;battery.&lt;/p&gt;
&lt;p&gt;&lt;img alt="iPhone 4 and iPhone 12 Pro" src="https://ngjunsiang.github.io/laymansguide/issue138_02.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;iPhone 4 on the left, iPhone 12 Pro on the right&lt;br /&gt;The basic layout of the iPhone has been preserved over a decade.&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/Teardown/iPhone+4+Teardown/3130"&gt;iFixit&lt;/a&gt; and &lt;a href="https://www.ifixit.com/Teardown/iPhone+12+and+12+Pro+Teardown/137669"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;What’s the difference between this layout and the 3G? Let’s have a look at the iPhone 4’s&amp;nbsp;mainboard:&lt;/p&gt;
&lt;p&gt;&lt;img alt="iPhone 4 mainboard" src="https://ngjunsiang.github.io/laymansguide/issue138_03.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;iPhone 4 mainboard. I got lazy with the labelling because, well, there’s nothing to label!&lt;br /&gt;The &lt;span class="caps"&gt;CPU&lt;/span&gt; is the huge chip labelled “A4”, and there’s memory and the 3G chip on the back.&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/Teardown/iPhone+4+Teardown/3130"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Similar to the transition from &lt;span class="caps"&gt;AT&lt;/span&gt; to &lt;span class="caps"&gt;ATX&lt;/span&gt; motherboards (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue132.html"&gt;Issue 132&lt;/a&gt;) and &lt;a href="https://ngjunsiang.github.io/laymansguide/issue134.html"&gt;134&lt;/a&gt;)), the iPhone underwent a great miniaturisation—in a single&amp;nbsp;generation!&lt;/p&gt;
&lt;h2&gt;System-on-Chip&lt;/h2&gt;
&lt;p&gt;What happened to all those separate chips? Most of them got moved &lt;em&gt;onboard&lt;/em&gt;, into the A4 chip, or other auxiliary chips. The great consolidating brought all their functionality under one&amp;nbsp;roof.&lt;/p&gt;
&lt;p&gt;The A4 chip&amp;nbsp;carries:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;CPU&lt;/span&gt;&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;GPU&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;MCH&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;PCH&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This chip is responsible&amp;nbsp;for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;performing&amp;nbsp;calculations&lt;/li&gt;
&lt;li&gt;rendering graphics (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue123.html"&gt;Issue 123&lt;/a&gt;))&lt;/li&gt;
&lt;li&gt;managing the flow of information between &lt;span class="caps"&gt;CPU&lt;/span&gt;, &lt;span class="caps"&gt;GPU&lt;/span&gt;, and memory (previously the job of the &lt;span class="caps"&gt;MCH&lt;/span&gt;)&lt;/li&gt;
&lt;li&gt;managing the flow of information between storage, network, and the &lt;span class="caps"&gt;MCH&lt;/span&gt; (previously the job of the &lt;span class="caps"&gt;PCH&lt;/span&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It is literally an entire system on a chip: a &lt;strong&gt;system-on-chip&lt;/strong&gt;&amp;nbsp;(SoC)!&lt;/p&gt;
&lt;p&gt;More and more functionality would gradually be migrated into the SoC itself, with fewer auxiliary chips required: sensors, gyroscopes, image processors for the camera, etc. More educational perhaps would be to look at what’s &lt;em&gt;not&lt;/em&gt; included in the SoC, particularly by the time we get to the the iPhone 12’s SoC, called the&amp;nbsp;A14.&lt;/p&gt;
&lt;p&gt;Not on the A14&amp;nbsp;SoC:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;solid state disk (only part of it is in the&amp;nbsp;SoC)&lt;/li&gt;
&lt;li&gt;power&amp;nbsp;management&lt;/li&gt;
&lt;li&gt;4G &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt;&amp;nbsp;5G&lt;/li&gt;
&lt;li&gt;audio&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The above functionality is highly specialised, especially in modern&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt; smartphones. It manages the remaining parts of the phone: camera &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; mic/speakers, wifi &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; bluetooth, and telecommunications (4G/5G). Telecommunications in particular require a lot of power and would have contributed to unnecessary heating in the small &lt;span class="caps"&gt;CPU&lt;/span&gt;&amp;nbsp;package.&lt;/p&gt;
&lt;p&gt;And this is how we shrink a laptop mainboard even&amp;nbsp;further.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; A system-on-chip (SoC) combines the core functionality of a system—processing, graphics, memory, and control—into a single chip&amp;nbsp;package.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;The M1’s design shares a lot more in common with the A14 on the iPhone and iPad than it does with the laptop CPUs that came before it. I want to go into a bit more detail about this in the next issue, so that it’s easier to see just how different it is from a typical&amp;nbsp;laptop.&lt;/p&gt;
&lt;p&gt;First question: what exactly does “unified memory” mean? Why is Apple making it such a big&amp;nbsp;deal?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 139: What’s before this line is mine, what’s after this line is&amp;nbsp;yours&lt;/p&gt;
&lt;p&gt;Next issue, we look at a trend that started being reported on in 2015: the high-performance computing industry realised that the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; need to have much more integrated memory&amp;nbsp;sharing.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;The &lt;span class="caps"&gt;CPU&lt;/span&gt; carries (some) onboard memory for itself, but the main bulk of memory is still on the mainboard.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;I know it’s strange to differentiate older vs newer smartphones when the technology is only 1.5 decades old. But the evolution of smartphone designs over the course has been significant enough that yes, I am going to make this distinction :)&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 11"></category></entry><entry><title>Issue 137: The M1 Macbook Air</title><link href="https://ngjunsiang.github.io/laymansguide/issue137.html" rel="alternate"></link><published>2021-09-11T08:00:00+08:00</published><updated>2021-09-11T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-09-11:/laymansguide/issue137.html</id><summary type="html">&lt;p&gt;The M1 goes one step further: not only does it make do with fewer chips, it does so with passive&amp;nbsp;cooling!&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Slim laptops have been undergoing a gradual transition: more and more of their chips are no longer available as a replaceable card, but instead soldered directly to the mainboard. Since 2017/2018, most slim laptops pretty much have &lt;span class="caps"&gt;CPU&lt;/span&gt;, memory, storage, and network chips all soldered directly to the&amp;nbsp;mainboard.&lt;/p&gt;
&lt;p&gt;Let’s get to it: Intel vs M1 Macbook&amp;nbsp;Air!&lt;/p&gt;
&lt;h2&gt;The 2020 Macbook Air: passing the&amp;nbsp;torch&lt;/h2&gt;
&lt;p&gt;Here’s the Macbook Air in 2020. There was one in early 2020 using an Intel Core &lt;span class="caps"&gt;CPU&lt;/span&gt;, and one in late 2020 using the Apple M1 &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Macbook Air in early 2020 (left), vs late 2020 (right)" src="https://ngjunsiang.github.io/laymansguide/issue137_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;Macbook Air, early-2020 (Intel, left) vs late-2020 (M1, right)&lt;br /&gt;&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/News/46884/m1-macbook-teardowns-something-old-something-new"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;They look almost identical, but some parts are noticeably different … can you spot the&amp;nbsp;differences?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The cooling fan (upper left) is there in the early 2020 (Intel) model, but gone in the late 2020 (M1)&amp;nbsp;model.&lt;/li&gt;
&lt;li&gt;Besides the &lt;span class="caps"&gt;CPU&lt;/span&gt; (upper centre in both models, under a heatsink), the Intel model has a mysterious-looking chip (upper right, covered in black&amp;nbsp;shrouding)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You may have spotted other differences in the hardware, but since this issue is focused on the mainboard and &lt;span class="caps"&gt;CPU&lt;/span&gt;, let’s zoom in on those. Let’s have a closer look at their&amp;nbsp;mainboards:&lt;/p&gt;
&lt;p&gt;&lt;img alt="2020 Intel Macbook Air mainboard, front(left) vs back (right)" src="https://ngjunsiang.github.io/laymansguide/issue137_04.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;2020 Intel Macbook Air mainboard, front and back&lt;br /&gt;The Intel &lt;span class="caps"&gt;CPU&lt;/span&gt; unfortunately sits under the huge heatsink, shown with its 4 securing screws&lt;br /&gt;Memory and solid state disk are on separate chips (most likely on the back)&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/Store/Mac/MacBook-Air-13-Inch-Early-2020-1-1-GHz-Core-i3-Logic-Board-with-Paired-Touch-ID-Sensor/IF188-152?o=1"&gt;iFixit&amp;nbsp;Store&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;&lt;img alt="2020 M1 Macbook Air mainboard, front (left) vs back (right)" src="https://ngjunsiang.github.io/laymansguide/issue137_02.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;2020 M1 Macbook Air mainboard, front and back&lt;br /&gt;There are fewer big chips, but the single biggest chip there is &lt;/em&gt;much&lt;em&gt; bigger, and Apple-branded&lt;br /&gt;Memory is integrated into the &lt;span class="caps"&gt;CPU&lt;/span&gt;, but the solid state disk sits on a separate pair of chips&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/News/46884/m1-macbook-teardowns-something-old-something-new"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;h2&gt;The M1 Macbook Air: all&amp;nbsp;aboard&lt;/h2&gt;
&lt;p&gt;Overall, it looks like the M1 has “swallowed” a number of chips. Compared to the 2020 Intel model, the M1 has brought on-board computer memory (the two black chips on the M1), and Apple’s T2 chip (the back shrouded chip on the 2020 Intel Macbook Air). These are major components for computer&amp;nbsp;operation.&lt;/p&gt;
&lt;p&gt;So not only does the M1 incorporate more components, it does so while drawing less power—the lack of a cooling fan implies it is passively cooled. From &lt;a href="https://ngjunsiang.github.io/laymansguide/issue129.html"&gt;Issue 129&lt;/a&gt;), this suggests the M1 Macbook Air also uses less power (8–12W) for its tasks. And reviews for the Macbook M1 Air suggest it is not being thermally throttled except under the heaviest of&amp;nbsp;loads.&lt;/p&gt;
&lt;p&gt;How did Apple manage to design a processor like&amp;nbsp;this?&lt;/p&gt;
&lt;h2&gt;The Apple M1: evolved from a smartphone&amp;nbsp;chip&lt;/h2&gt;
&lt;p&gt;To get into that story, I’ll have to go even more mobile, and look at smartphone CPUs. After all, the Apple M1 actually evolved from the Apple A-series CPUs for their iPhone and iPad. That starts next&amp;nbsp;issue.&lt;/p&gt;
&lt;h2&gt;What about other Intel Core laptops running&amp;nbsp;Windows?&lt;/h2&gt;
&lt;p&gt;They are largely undergoing the same transition, just more slowly. This is the Microsoft Surface Laptop in&amp;nbsp;2017:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Microsoft Surface Laptop (2017) mainboard" src="https://ngjunsiang.github.io/laymansguide/issue137_03.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;Microsoft Surface Laptop mainboard&lt;br /&gt;&lt;span class="caps"&gt;CPU&lt;/span&gt;(red), memory (orange), solid state disk (yellow), and network card (green) are all soldered on. (Outlined in cyan are the display control chips)&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/Teardown/Microsoft+Surface+Laptop+Teardown/92915"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Larger-sized laptops that can afford the space may still have solid state storage on a separate&amp;nbsp;card.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; The M1 goes one step further: not only does it make do with fewer chips, it does so with passive&amp;nbsp;cooling!&lt;/p&gt;
&lt;p&gt;The last issue simply went on too long, especially with all the images, so I figured this issue would stand better as a Core-vs-M1 comparison, instead of being the tail of an evolution-of-Air issue. So it’s&amp;nbsp;short.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 138: System-on-Chip&amp;nbsp;(SoC)&lt;/p&gt;
&lt;p&gt;If smartphones are even smaller than laptops, how do they do it? Laptops seem to have exhausted all the tricks, and those boards still look pretty&amp;nbsp;big.&lt;/p&gt;
&lt;p&gt;Next issue, I’ll talk about the next step in the evolution of shrinking&amp;nbsp;mainboards.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 11"></category></entry><entry><title>Issue 136: The mobile workstation – laptops</title><link href="https://ngjunsiang.github.io/laymansguide/issue136.html" rel="alternate"></link><published>2021-09-04T08:00:00+08:00</published><updated>2021-09-04T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-09-04:/laymansguide/issue136.html</id><summary type="html">&lt;p&gt;Slim laptops have been undergoing a gradual transition: more and more of their chips are no longer available as a replaceable card, but instead soldered directly to the mainboard. Since 2017/2018, most slim laptops pretty much have &lt;span class="caps"&gt;CPU&lt;/span&gt;, memory, storage, and network chips all soldered directly to the&amp;nbsp;mainboard.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; A modern &lt;span class="caps"&gt;CPU&lt;/span&gt; is manufactured through a process called photolithography, by which the &lt;span class="caps"&gt;CPU&lt;/span&gt; components are etched onto the silicon substrate by successive layers of chemicals, masking, and laser exposure. When the &lt;span class="caps"&gt;CPU&lt;/span&gt; components could be made small enough, the &lt;span class="caps"&gt;MCH&lt;/span&gt; and &lt;span class="caps"&gt;CPU&lt;/span&gt; were designed onto the same chip, and this is the design used by the Intel Core i7&amp;nbsp;(1st-gen).&lt;/p&gt;
&lt;p&gt;In the last 4 issues, I walked through the general evolution of desktop computers. Let’s go more mobile, and look at laptops. How does something as big as a desktop shrink down to the size of a laptop? And what are the tradeoffs&amp;nbsp;involved?&lt;/p&gt;
&lt;p&gt;I addressed the power part of the formula in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue130.html"&gt;Issue 130&lt;/a&gt;), on power limits; laptops are slimmer in part because part of them—the &lt;span class="caps"&gt;AC&lt;/span&gt; adapter—lies outside the&amp;nbsp;system.&lt;/p&gt;
&lt;p&gt;Let’s look at the rest of&amp;nbsp;it.&lt;/p&gt;
&lt;h2&gt;Laptops use slimmer&amp;nbsp;components&lt;/h2&gt;
&lt;p&gt;Laptops use slimmer memory than&amp;nbsp;desktops:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Desktop memory vs Laptop memory" src="https://ngjunsiang.github.io/laymansguide/issue136_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;Desktop memory (&lt;span class="caps"&gt;DIMM&lt;/span&gt;) vs laptop memory (&lt;span class="caps"&gt;SODIMM&lt;/span&gt;)&lt;br /&gt;Source: &lt;a href="https://www.quora.com/What-type-of-memory-module-is-used-in-a-desktop-and-laptop-computer"&gt;Quora&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;On a desktop mainboard, desktop memory sticks out perpendicularly from the mainboard, all the better to cram memory sticks together and maximise the use of&amp;nbsp;space.&lt;/p&gt;
&lt;p&gt;On a laptop mainboard, laptop memory sticks lie parallel to the mainboard, to reduce the mainboard height and allow a slim laptop&amp;nbsp;profile.&lt;/p&gt;
&lt;p&gt;As far as I know &amp;#8230; there aren’t any other significant differences to highlight (besides size). Unless you’re overclocking, just get the kind of memory your computer/laptop needs. These days, small-form-factor desktops use laptop memory (&lt;span class="caps"&gt;SODIMM&lt;/span&gt;) as&amp;nbsp;well!&lt;/p&gt;
&lt;p&gt;Laptops use slimmer hard drives compared to desktops as&amp;nbsp;well:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Desktop hard drive vs Laptop hard drive" src="https://ngjunsiang.github.io/laymansguide/issue136_02.png" /&gt;&lt;br /&gt;
&lt;em&gt;Desktop hard drive (3.5″) vs Laptop hard drive (2.5″)&lt;br /&gt;Source: &lt;a href="https://www.m2wificards.com/2-5-vs-3-5-hdd/"&gt;M2WifiCards&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Desktop hard drives are larger, use larger platters (3.5″ diameter), and hence draw more power (at both 12V and 5V voltages). Laptop hard drives are smaller, use smaller platters (2.5″ diameter), and draw less power (at 5V voltage only). This is why smaller external hard drives, which use laptop hard drives, can be powered over &lt;span class="caps"&gt;USB&lt;/span&gt;, but larger external hard drives, which use desktop hard drives, need an external &lt;span class="caps"&gt;AC&lt;/span&gt;&amp;nbsp;adapter.&lt;/p&gt;
&lt;p&gt;These days, laptops have mostly made the transition to solid state disks, and you are much less likely to see hard drives in&amp;nbsp;laptops.&lt;/p&gt;
&lt;h2&gt;The slim laptop in 2010: Macbook&amp;nbsp;Air&lt;/h2&gt;
&lt;p&gt;Let’s examine how a characteristic slim laptop, the Macbook Air, has changed in the past 10&amp;nbsp;years.&lt;/p&gt;
&lt;p&gt;In 2010, the Macbook Air had its solid state disk and wifi network card on separate (replaceable) cards. But the &lt;span class="caps"&gt;CPU&lt;/span&gt;, &lt;span class="caps"&gt;GPU&lt;/span&gt;, and memory were all soldered directly onto the&amp;nbsp;motherboard.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Solid state disk in the 2010 Macbook Air" src="https://ngjunsiang.github.io/laymansguide/issue136_03.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;The solid state disk in the 2010 Macbook Air&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/Teardown/MacBook+Air+11-Inch+Late+2010+Teardown/3745"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;&lt;img alt="Wifi network card on a 2010 Macbook Air" src="https://ngjunsiang.github.io/laymansguide/issue136_04.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;The wifi network card on the 2010 Macbook Air&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/Teardown/MacBook+Air+11-Inch+Late+2010+Teardown/3745"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;&lt;img alt="CPU and GPU on a 2010 Macbook Air, exposed without cooler" src="https://ngjunsiang.github.io/laymansguide/issue136_05.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;The &lt;span class="caps"&gt;CPU&lt;/span&gt; (left) and &lt;span class="caps"&gt;GPU&lt;/span&gt; (right) on the 2010 Macbook Air&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/Teardown/MacBook+Air+11-Inch+Late+2010+Teardown/3745"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;&lt;img alt="Mainboard of a 2010 Macbook Air" src="https://ngjunsiang.github.io/laymansguide/issue136_06.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;Another view. &lt;span class="caps"&gt;CPU&lt;/span&gt; (red), &lt;span class="caps"&gt;GPU&lt;/span&gt; (orange), and memory (yellow) are directly soldered onto the mainboard&lt;br /&gt;Where is the chipset? I don’t know; the Macbook Air does not seem to use the same chipset as Intel-powered desktops&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/Teardown/MacBook+Air+11-Inch+Late+2010+Teardown/3745"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;h2&gt;The slim laptop in 2018: also Macbook&amp;nbsp;Air&lt;/h2&gt;
&lt;p&gt;By 2018, while the outside of the Macbook Air still looks much the same, the insides are rather&amp;nbsp;different:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Mainboard of a 2018 Macbook Air (top view)" src="https://ngjunsiang.github.io/laymansguide/issue136_07.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;&lt;span class="caps"&gt;CPU&lt;/span&gt; (red) and solid state disk (yellow) are directly soldered onto the mainboard&lt;br /&gt;Where’s the chipset? Notice that the &lt;span class="caps"&gt;CPU&lt;/span&gt; seems to have &lt;/em&gt;2 chips&lt;em&gt; on it? They are the &lt;span class="caps"&gt;CPU&lt;/span&gt; and chipset; two chips in one &lt;span class="caps"&gt;CPU&lt;/span&gt; package!&lt;br /&gt;The next image shows the reverse side. Other chips are ignored here, see the iFixit article for full identification&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/Teardown/MacBook+Air+13-Inch+Retina+2018+Teardown/115201"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;&lt;img alt="Mainboard of a 2018 Macbook Air (bottom view)" src="https://ngjunsiang.github.io/laymansguide/issue136_08.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;Memory (red) and wifi network chip (orange) are &lt;/em&gt;also&lt;em&gt; directly soldered onto the mainboard&lt;br /&gt;This laptop only had integrated graphics; Intel had upped its integrated graphics performance sufficiently by this point&lt;br /&gt;Other chips are ignored here, see the iFixit article for full identification&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/Teardown/MacBook+Air+13-Inch+Retina+2018+Teardown/115201"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Components which in the 2010 Air were on separate cards are now all soldered directly to the mainboard! On the one hand, this saves space, which can be used for other features, or just for larger batteries. It also reduces the cost of manufacturing; connectors are costly to engineer and manufacture. On the other hand, it means upgradeability goes out the&amp;nbsp;window.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Slim laptops have been undergoing a gradual transition: more and more of their chips are no longer available as a replaceable card, but instead soldered directly to the mainboard. Since 2017/2018, most slim laptops pretty much have &lt;span class="caps"&gt;CPU&lt;/span&gt;, memory, storage, and network chips all soldered directly to the&amp;nbsp;mainboard.&lt;/p&gt;
&lt;p&gt;Sorry about the image dump, I figured it would still be more convenient than having to click-through to see the images&amp;nbsp;:)&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 137: The M1 Macbook&amp;nbsp;Air&lt;/p&gt;
&lt;p&gt;The M1 goes even further than the 2018 Macbook Air, in one pretty significant way. Next issue, we compare how the Intel and M1 Macbooks Air&amp;nbsp;differ!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 11"></category></entry><entry><title>Issue 135: Part 2 – Unifying the CPU and MCH (post-2008)</title><link href="https://ngjunsiang.github.io/laymansguide/issue135.html" rel="alternate"></link><published>2021-08-28T08:00:00+08:00</published><updated>2021-08-28T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-08-28:/laymansguide/issue135.html</id><summary type="html">&lt;p&gt;A modern &lt;span class="caps"&gt;CPU&lt;/span&gt; is manufactured through a process called photolithography, by which the &lt;span class="caps"&gt;CPU&lt;/span&gt; components are etched onto the silicon substrate by successive layers of chemicals, masking, and laser exposure. When the &lt;span class="caps"&gt;CPU&lt;/span&gt; components could be made small enough, the &lt;span class="caps"&gt;MCH&lt;/span&gt; and &lt;span class="caps"&gt;CPU&lt;/span&gt; were designed onto the same chip, and this is the design used by the Intel Core i7&amp;nbsp;(1st-gen).&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Light takes 0.3 ns to travel 10 cm, approximately the distance by wire between the &lt;span class="caps"&gt;CPU&lt;/span&gt; and the &lt;span class="caps"&gt;MCH&lt;/span&gt;. This potentially causes operations between the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;MCH&lt;/span&gt; to slow down by one cycle, at frequencies above 3 GHz. One way the Intel Core i-series resolves this conundrum is to move the &lt;span class="caps"&gt;MCH&lt;/span&gt; &lt;em&gt;into&lt;/em&gt; the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Chipset diagram of ATX systems for Intel Core (i-Series)" src="https://ngjunsiang.github.io/laymansguide/issue134_02.gif" /&gt;&lt;br /&gt;
&lt;em&gt;An Intel Core i-series &lt;span class="caps"&gt;ATX&lt;/span&gt; system chipset diagram.&lt;br /&gt;The &lt;span class="caps"&gt;MCH&lt;/span&gt; is merged into the &lt;span class="caps"&gt;CPU&lt;/span&gt;, but still a discrete unit.&lt;br /&gt;&lt;span class="caps"&gt;DDR&lt;/span&gt; refers to computer memory, while &lt;span class="caps"&gt;GDDR&lt;/span&gt; refers to graphics card memory (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue123.html"&gt;Issue123&lt;/a&gt;))&lt;br /&gt;Source: &lt;a href="https://arstechnica.com/gadgets/2009/09/intel-launches-all-new-pc-architecture-with-core-i5i7-cpus/"&gt;Ars&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Time to close up some open plot points from last&amp;nbsp;issue:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The number of pins on 1st-gen Core i7 is almost triple that of the Pentium 4; what are all those pins&amp;nbsp;for?&lt;/li&gt;
&lt;li&gt;The &lt;span class="caps"&gt;MCH&lt;/span&gt; has been moved into the &lt;span class="caps"&gt;CPU&lt;/span&gt; to improve latencies, but how is it possible to make it small enough to do&amp;nbsp;that?&lt;/li&gt;
&lt;li&gt;Are there any&amp;nbsp;disadvantages?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I’ll answer the second question first. It’s quite simple&amp;nbsp;really.&lt;/p&gt;
&lt;p&gt;You see, for circuit components, size doesn’t always benefit performance. A large transistor does essentially the same thing as a smaller transistor. So making them smaller is advantageous really; you can fit more into a single&amp;nbsp;chip!&lt;/p&gt;
&lt;h2&gt;Making a modern &lt;span class="caps"&gt;CPU&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;Modern CPUs are manufactured through a process called &lt;strong&gt;photolithography&lt;/strong&gt;—literally it means “etching with light” (Greek; &lt;em&gt;photo-&lt;/em&gt; “light” + &lt;em&gt;litho-&lt;/em&gt; “stone” + &lt;em&gt;-graphie&lt;/em&gt; “to draw”). By layering chemicals over the silicon base, putting a mask over them, and exposing them to light, a series of chemical reactions are induced to create the circuit pattern on the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Multiple CPUs are created on a single die this way, then individually cut and processed, in multiple steps spanning several months&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;. The precision and fineness of the etching laser determine how small we can create components on this substrate. As the manufacturing process improves, semiconductor manufacturing companies are able to create CPUs that can cram more and more transistors into each square mm (or inch) of silicon&amp;nbsp;die.&lt;/p&gt;
&lt;p&gt;Besides being able to cram more transistors into the same space, it turns out that smaller components also use much less power! So we not only get performance gains, we get power efficiency gains as well—two birds with one&amp;nbsp;stone.&lt;/p&gt;
&lt;p&gt;&lt;img alt="CPU diagram of the Intel Core i7 (1st-gen)" src="https://ngjunsiang.github.io/laymansguide/issue135_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;&lt;span class="caps"&gt;CPU&lt;/span&gt; diagram of the Intel Core i7 (1st-gen)&lt;br /&gt;The memory controller, misc. &lt;span class="caps"&gt;IO&lt;/span&gt;, and &lt;span class="caps"&gt;QPI&lt;/span&gt; areas perform the role that the &lt;span class="caps"&gt;MCH&lt;/span&gt; used to take up&lt;br /&gt;Source: &lt;a href="https://www.anandtech.com/print/2658/"&gt;AnandTech&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;h2&gt;Moving&amp;nbsp;in&lt;/h2&gt;
&lt;p&gt;Over multiple generations of process improvements, the &lt;span class="caps"&gt;MCH&lt;/span&gt; and the &lt;span class="caps"&gt;CPU&lt;/span&gt; could finally be made small enough that they could both reasonably fit into the same die. There are, of course, &lt;em&gt;implications&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Previously, the &lt;span class="caps"&gt;CPU&lt;/span&gt; only needed pins to communicate with the &lt;span class="caps"&gt;MCH&lt;/span&gt;. Now, the combined chip needs more pins than before to communicate with the computer memory, graphics processing unit (&lt;span class="caps"&gt;GPU&lt;/span&gt;), and &lt;span class="caps"&gt;PCH&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;So that answers the first question of what the additional pins are&amp;nbsp;for.&lt;/p&gt;
&lt;h2&gt;Working as one&amp;nbsp;unit&lt;/h2&gt;
&lt;p&gt;Which leaves the third question: besides latency improvements, are there any other&amp;nbsp;advantages?&lt;/p&gt;
&lt;p&gt;Mainboard manufacturers save on the cost of the &lt;span class="caps"&gt;MCH&lt;/span&gt; chipset, &lt;a href="https://www.anandtech.com/show/2824"&gt;which works out to about $40&lt;/a&gt;. Pretty significant when a mid-range mainboard costs&amp;nbsp;$80–$160.&lt;/p&gt;
&lt;p&gt;With the &lt;span class="caps"&gt;MCH&lt;/span&gt; and its requisite wires gone, the mainboard can be shrunk further; motherboards gradually shrank from &lt;span class="caps"&gt;ATX&lt;/span&gt; and microATX form factor sizes, to smaller form factors, such as &lt;span class="caps"&gt;ITX&lt;/span&gt; and the current popular &lt;span class="caps"&gt;NUC&lt;/span&gt; form&amp;nbsp;factors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mainboard form&amp;nbsp;factors&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;ATX&lt;/span&gt;: 30×24cm (12×9.6&amp;nbsp;in)&lt;/li&gt;
&lt;li&gt;microATX: 24×24cm (9.6×9.6&amp;nbsp;in)&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;ITX&lt;/span&gt;: 17×17cm (7×7&amp;nbsp;in)&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;NUC&lt;/span&gt;: 10×10cm (4×4&amp;nbsp;in)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And the disadvantages &amp;#8230; well, none on the consumer side actually. It seems to be positive all&amp;nbsp;around!&lt;/p&gt;
&lt;p&gt;Well actually, complexity rears its ugly head in power-saving&amp;nbsp;features.&lt;/p&gt;
&lt;p&gt;Previously, when the computer is in standby (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue115.html"&gt;Issue 115&lt;/a&gt;)), the &lt;span class="caps"&gt;CPU&lt;/span&gt; could be safely shut down (i.e. cut power to &lt;span class="caps"&gt;CPU&lt;/span&gt;), leaving only the &lt;span class="caps"&gt;MCH&lt;/span&gt; minimally powered so the computer memory retains its&amp;nbsp;information.&lt;/p&gt;
&lt;p&gt;With the &lt;span class="caps"&gt;MCH&lt;/span&gt; and the &lt;span class="caps"&gt;CPU&lt;/span&gt; now sharing the same chip, they have to be put in separate power zones so that the &lt;span class="caps"&gt;MCH&lt;/span&gt; portion remains powered while in standby, while the &lt;span class="caps"&gt;CPU&lt;/span&gt; can be shut down safely, making the chip more complicated than its&amp;nbsp;predecessors.&lt;/p&gt;
&lt;p&gt;But that is of little concern for us&amp;nbsp;layfolks.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; A modern &lt;span class="caps"&gt;CPU&lt;/span&gt; is manufactured through a process called photolithography, by which the &lt;span class="caps"&gt;CPU&lt;/span&gt; components are etched onto the silicon substrate by successive layers of chemicals, masking, and laser exposure. When the &lt;span class="caps"&gt;CPU&lt;/span&gt; components could be made small enough, the &lt;span class="caps"&gt;MCH&lt;/span&gt; and &lt;span class="caps"&gt;CPU&lt;/span&gt; were designed onto the same chip, and this is the design used by the Intel Core i7&amp;nbsp;(1st-gen).&lt;/p&gt;
&lt;p&gt;This is where the story stops with Intel for this season; their current-gen Core series still uses much the same chipset diagram, and a similar basic architecture, so there’s little new info of relevance for me to add&amp;nbsp;here.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 136: The mobile workstation –&amp;nbsp;laptops&lt;/p&gt;
&lt;p&gt;To continue the story towards the Apple M1, it’s time to switch our lens to the mobile world: tablets, smartphones, and things smaller than a laptop. How are these things designed? What are their CPUs like? We’ll examine the evolution of the iconic Macbook Air, from 2010 to 2020 (warning:&amp;nbsp;image-heavy!)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;See &lt;a href="https://www.washingtonpost.com/technology/2021/07/07/making-semiconductors-is-hard/"&gt;Three months, 700 steps: Why it takes so long to produce a computer chip (WashPo)&lt;/a&gt; for a more comprehensive description of the process&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 11"></category></entry><entry><title>Issue 134: Part 1 – the Intel Core i-series launches!</title><link href="https://ngjunsiang.github.io/laymansguide/issue134.html" rel="alternate"></link><published>2021-08-21T08:00:00+08:00</published><updated>2021-08-21T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-08-21:/laymansguide/issue134.html</id><summary type="html">&lt;p&gt;Light takes 0.3 ns to travel 10 cm, approximately the distance by wire between the &lt;span class="caps"&gt;CPU&lt;/span&gt; and the &lt;span class="caps"&gt;MCH&lt;/span&gt;. This potentially causes operations between the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;MCH&lt;/span&gt; to slow down by one cycle, at frequencies above 3 GHz. One way the Intel Core i-series resolves this conundrum is to move the memory controller &lt;em&gt;into&lt;/em&gt; the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; The &lt;span class="caps"&gt;ATX&lt;/span&gt; form factor also brought with it a new breed of computers with more specialised chipsets: the memory controller hub (&lt;span class="caps"&gt;MCH&lt;/span&gt;) and peripheral controller hub (&lt;span class="caps"&gt;PCH&lt;/span&gt;). The &lt;span class="caps"&gt;MCH&lt;/span&gt; specialises in high-throughput requirements, such as computer memory and graphics. The &lt;span class="caps"&gt;PCH&lt;/span&gt; specialises in lower-throughput&amp;nbsp;needs.&lt;/p&gt;
&lt;p&gt;Last issue, we looked at the &lt;span class="caps"&gt;ATX&lt;/span&gt; form factor by Intel, which replaced the &lt;span class="caps"&gt;AT&lt;/span&gt; form factor by &lt;span class="caps"&gt;IBM&lt;/span&gt;. While the &lt;span class="caps"&gt;AT&lt;/span&gt; could get by with a smattering of chips, which worked fine for mostly text-only computers, the &lt;span class="caps"&gt;ATX&lt;/span&gt; has much higher throughput requirements. To help the &lt;span class="caps"&gt;CPU&lt;/span&gt; focus on serving the user’s applications, two chipsets—the memory controller hub (&lt;span class="caps"&gt;MCH&lt;/span&gt;) and peripheral controller hub (&lt;span class="caps"&gt;PCH&lt;/span&gt;), take charge of managing the data throughput. The &lt;span class="caps"&gt;MCH&lt;/span&gt; manages data between &lt;span class="caps"&gt;CPU&lt;/span&gt;, computer memory, the graphics processor unit (&lt;span class="caps"&gt;GPU&lt;/span&gt;), and the &lt;span class="caps"&gt;PCH&lt;/span&gt;, while the &lt;span class="caps"&gt;PCH&lt;/span&gt; manages data between the peripherals (audio, storage, network, &lt;span class="caps"&gt;USB&lt;/span&gt;, &amp;#8230;) and the &lt;span class="caps"&gt;MCH&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Chipset diagram of ATX systems, up to early Intel Core (i-Series)" src="https://ngjunsiang.github.io/laymansguide/issue134_01.gif" /&gt;&lt;br /&gt;
&lt;em&gt;An Intel pre-Core i-series &lt;span class="caps"&gt;ATX&lt;/span&gt; system chipset diagram.&lt;br /&gt;The &lt;span class="caps"&gt;MCH&lt;/span&gt; and &lt;span class="caps"&gt;PCH&lt;/span&gt; (labelled &lt;span class="caps"&gt;ICH&lt;/span&gt; here for unimportant reasons) support the &lt;span class="caps"&gt;CPU&lt;/span&gt; in its data operations&lt;br /&gt;&lt;span class="caps"&gt;DDR&lt;/span&gt; refers to computer memory, while &lt;span class="caps"&gt;GDDR&lt;/span&gt; refers to graphics card memory (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue123.html"&gt;Issue123&lt;/a&gt;))&lt;br /&gt;Source: &lt;a href="https://arstechnica.com/gadgets/2009/09/intel-launches-all-new-pc-architecture-with-core-i5i7-cpus/"&gt;Ars&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;There are terms for each of the connections between chips, which I won’t get into because it largely won’t concern us until we have to design performant&amp;nbsp;systems.&lt;/p&gt;
&lt;h2&gt;The evolution of Intel &lt;span class="caps"&gt;ATX&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;The technical geeks are probably fuming at this point because &lt;span class="caps"&gt;ATX&lt;/span&gt; is a motherboard standard, while I’m talking about the evolution of processors which have little to do with the motherboards, at least not directly &amp;#8230; but that’s of little importance at this point. Because we first need to talk about &lt;span class="caps"&gt;CPU&lt;/span&gt;&amp;nbsp;pins.&lt;/p&gt;
&lt;p&gt;From &lt;a href="https://ngjunsiang.github.io/laymansguide/issue131.html"&gt;Issue 131&lt;/a&gt;), I gave a simple model of the limitations of data&amp;nbsp;transfer:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;There is a max frequency they can operate at, and a limit to the number of wires they can be connected to (throughput = no. of wires ×&amp;nbsp;frequency)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The number of pins on processors have been steadily increasing up to this point, and so have the frequencies of processors. The Pentium 4 was succeeded by the Pentium D, then the Pentium Dual Core, then the Core 2. This Core processor preceded the Core i3/i5/i7 processors we know today; I’ll refer to this family of processors as the pre-i Core (rather than the more technical &lt;span class="caps"&gt;LGA775&lt;/span&gt;&amp;nbsp;series).&lt;/p&gt;
&lt;p&gt;Pentium 4: 478 pins&lt;br /&gt;
Core, Core 2 (pre-i Core): 775 pins&lt;br /&gt;
Core (i7, first-gen): &lt;strong&gt;1155&amp;nbsp;pins&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Yup, the number of pins have &lt;em&gt;almost tripled&lt;/em&gt; since the Pentium 4! Remember that more pins does not make the &lt;span class="caps"&gt;CPU&lt;/span&gt; &lt;em&gt;itself&lt;/em&gt; calculate faster, it just helps it to &lt;em&gt;transfer data&lt;/em&gt;&amp;nbsp;faster.&lt;/p&gt;
&lt;p&gt;What are all those pins for, if there is the &lt;span class="caps"&gt;MCH&lt;/span&gt; to manage data&amp;nbsp;flow?&lt;/p&gt;
&lt;p&gt;Let’s talk about the limitations of the pre-i Core&amp;nbsp;setup.&lt;/p&gt;
&lt;h2&gt;Communication at a&amp;nbsp;distance&lt;/h2&gt;
&lt;p&gt;Wait … don’t electrical signals travel at &lt;em&gt;speeds near the speed of light&lt;/em&gt;?&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt; How would disappearing the &lt;span class="caps"&gt;MCH&lt;/span&gt; improve&amp;nbsp;latency?&lt;/p&gt;
&lt;p&gt;Consider some&amp;nbsp;numbers:&lt;/p&gt;
&lt;p&gt;The typical distance between the &lt;span class="caps"&gt;CPU&lt;/span&gt; and the &lt;span class="caps"&gt;MCH&lt;/span&gt; is about 5 cm (2 in). Since the wires between them are not straight, let’s approximately double that to 10 cm (4 in). Light would take 0.3 ns to travel that distance. Which is roughly one clock cycle on a 3 GHz processor—at 3 billion cycles per second, each cycle takes a third of a billionth of a second!&lt;sup id="fnref2:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Remember that everything in a computer needs to happen like clockwork: for data to sync up, when the &lt;span class="caps"&gt;CPU&lt;/span&gt; sets a bit to one, the other party has to detect the bit signal before the clock cycle ends. If not, it will have to wait for the next clock cycle, causing the operation to slow down and take two clock cycles instead of&amp;nbsp;one.&lt;/p&gt;
&lt;p&gt;It’s like when you don’t manage to post the mail by 5pm, the postman has emptied the mailbox, and now you have to wait for 5pm the next day for your mail to be picked up&amp;nbsp;instead.&lt;/p&gt;
&lt;h2&gt;It’s all about throughput &amp;#8230; but also&amp;nbsp;latency&lt;/h2&gt;
&lt;p&gt;If light is taking one clock cycle to get out of the &lt;span class="caps"&gt;CPU&lt;/span&gt;, you have a problem. Raise the frequency higher than 3GHz, and you can cause a one-cycle lag just waiting for data to come in from the &lt;span class="caps"&gt;MCH&lt;/span&gt;, and to go out again to the &lt;span class="caps"&gt;MCH&lt;/span&gt;. That would counter-intuitively &lt;em&gt;slow down&lt;/em&gt; the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Solution: move the &lt;span class="caps"&gt;MCH&lt;/span&gt; into the &lt;span class="caps"&gt;CPU&lt;/span&gt;!&lt;/p&gt;
&lt;p&gt;&lt;img alt="Chipset diagram of ATX systems for Intel Core (i-Series)" src="https://ngjunsiang.github.io/laymansguide/issue134_02.gif" /&gt;&lt;br /&gt;
&lt;em&gt;An Intel Core i-series &lt;span class="caps"&gt;ATX&lt;/span&gt; system chipset diagram.&lt;br /&gt;The &lt;span class="caps"&gt;MCH&lt;/span&gt; is merged into the &lt;span class="caps"&gt;CPU&lt;/span&gt;, but still a discrete unit.&lt;br /&gt;&lt;span class="caps"&gt;DDR&lt;/span&gt; refers to computer memory, while &lt;span class="caps"&gt;GDDR&lt;/span&gt; refers to graphics card memory (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue123.html"&gt;Issue123&lt;/a&gt;))&lt;br /&gt;Source: &lt;a href="https://arstechnica.com/gadgets/2009/09/intel-launches-all-new-pc-architecture-with-core-i5i7-cpus/"&gt;Ars&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;h2&gt;Squeezing more tenants into the&amp;nbsp;building&lt;/h2&gt;
&lt;p&gt;Wait &amp;#8230; you can just do&amp;nbsp;that?&lt;/p&gt;
&lt;p&gt;I will need many more issues to lay out the mechanics of this, so I won’t—I think it’s way beyond the scope of a layman’s guide at that point!—but let’s see what I can come up with in the next&amp;nbsp;issue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Light takes 0.3 ns to travel 10 cm, approximately the distance by wire between the &lt;span class="caps"&gt;CPU&lt;/span&gt; and the &lt;span class="caps"&gt;MCH&lt;/span&gt;. This potentially causes operations between the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;MCH&lt;/span&gt; to slow down by one cycle, at frequencies above 3 GHz. One way the Intel Core i-series resolves this conundrum is to move the memory controller &lt;em&gt;into&lt;/em&gt; the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;This is what I spent years reading and thinking about to explain, and I finally get to lay it out in text. Incredibly excited to get to the next few&amp;nbsp;issues!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 135: Part 2 – Unifying the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;MCH&lt;/span&gt;&amp;nbsp;(post-2008)&lt;/p&gt;
&lt;p&gt;Next issue: how the &lt;span class="caps"&gt;ATX&lt;/span&gt; form factor evolved to eliminate the &lt;span class="caps"&gt;MCH&lt;/span&gt;. Sorry to end on a&amp;nbsp;cliffhanger!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;I want to just make a note here that while I believe my choice of analogy is justified, the numbers are wildly off: &lt;a href="https://www.realworldtech.com/nehalem/3/"&gt;RealWorldTech here puts the 1st-gen Core at approx 30 ns&lt;/a&gt;, for technical reasons that will take at least half a season to unpack (definitely not layman content!). But he also notes that latency for remote memory (i.e. memory not on the &lt;span class="caps"&gt;CPU&lt;/span&gt;, but on the motherboard) is “roughly 30 ns slower than local [memory]” (i.e. memory residing directly on the &lt;span class="caps"&gt;CPU&lt;/span&gt;). So the remote-vs-local latency gap is real and significant!&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;a class="footnote-backref" href="#fnref2:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 11"></category></entry><entry><title>Issue 133: the ATX form factor (post-1995)</title><link href="https://ngjunsiang.github.io/laymansguide/issue133.html" rel="alternate"></link><published>2021-08-14T08:00:00+08:00</published><updated>2021-08-14T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-08-14:/laymansguide/issue133.html</id><summary type="html">&lt;p&gt;The &lt;span class="caps"&gt;ATX&lt;/span&gt; form factor also brought with it a new breed of computers with more specialised chipsets: the memory controller hub (&lt;span class="caps"&gt;MCH&lt;/span&gt;) and peripheral controller hub (&lt;span class="caps"&gt;PCH&lt;/span&gt;). The &lt;span class="caps"&gt;MCH&lt;/span&gt; coordinates high-throughput components, such as computer memory and graphics. The &lt;span class="caps"&gt;PCH&lt;/span&gt; specialises in lower-throughput&amp;nbsp;needs.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Chipsets served as go-betweens in the &lt;span class="caps"&gt;AT&lt;/span&gt; form factor by &lt;span class="caps"&gt;IBM&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;In 1993, Intel launched its Pentium line of processors; barely two years later, in 1995, Intel launched the &lt;span class="caps"&gt;ATX&lt;/span&gt; form factor. This was the beginning of Intel’s dominance in the desktop space, and they could well afford to dictate most of the standards for this form&amp;nbsp;factor.&lt;/p&gt;
&lt;h2&gt;Chipset&amp;nbsp;diagram&lt;/h2&gt;
&lt;p&gt;Mainboards at this point were complicated enough that as part of the marketing, tech publications had taken to staring at diagrams of how the chips were connected. These diagrams are called &lt;strong&gt;chipset diagrams&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This is the chipset diagram of a typical mainboard for the Pentium&amp;nbsp;4:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Chipset diagram of a mainboard for the Pentium 4" src="https://ngjunsiang.github.io/laymansguide/issue133_02.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;I tried to find a chipset diagram that used human terms instead of acronyms.&lt;br /&gt;This is the best I could do. Annotations my own.&lt;br /&gt;Source: &lt;a href="https://www.hexus.net/tech/reviews/mainboard/635-sis655fx-dual-channel-p4-chipset/?page=2"&gt;Hexus&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;memory controller hub&lt;/strong&gt; (&lt;span class="caps"&gt;MCH&lt;/span&gt;) now takes on a much bigger role; it is managing data transfer between the &lt;span class="caps"&gt;CPU&lt;/span&gt;, graphics card, computer memory, &lt;em&gt;and&lt;/em&gt; the &lt;span class="caps"&gt;PCH&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;peripheral controller hub&lt;/strong&gt; (&lt;span class="caps"&gt;PCH&lt;/span&gt;), while managing connections to many more devices, actually has less work to do; these are all low-throughput devices that don’t send much data to the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;h2&gt;&lt;span class="caps"&gt;ATX&lt;/span&gt;&amp;nbsp;mainboard&lt;/h2&gt;
&lt;p&gt;And this is where the components are found on the&amp;nbsp;motherboard:&lt;/p&gt;
&lt;p&gt;&lt;img alt="A mainboard for the Pentium 4" src="https://ngjunsiang.github.io/laymansguide/issue133_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;A motherboard for the Pentium 4, with key components outlined.&lt;br /&gt;Annotations are my&amp;nbsp;own.&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;CPU&lt;/span&gt; clearly draws the most power and produces the most heat here. But notice now that the &lt;span class="caps"&gt;MCH&lt;/span&gt; is no longer bare; it now produces so much heat (&lt;a href="https://hexus.net/tech/news/mainboard/132515-der8auer-examines-amd-x570-chipset-power-consumption/"&gt;4–10 W&lt;/a&gt;) that it needs to be passively cooled with a heatsink (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue129.html"&gt;Issue 129&lt;/a&gt;)). the &lt;span class="caps"&gt;PCH&lt;/span&gt;, on the other hand, is still chill enough to get by bare naked (4 W or&amp;nbsp;less).&lt;/p&gt;
&lt;p&gt;3D graphics at this point is a rapidly growing industry, especially for videogames. Graphics cards needed much more throughput to the &lt;span class="caps"&gt;CPU&lt;/span&gt; and memory, so the &lt;span class="caps"&gt;MCH&lt;/span&gt; grew to fit into this role as the mediator between these throughput-hungry&amp;nbsp;components&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; The &lt;span class="caps"&gt;ATX&lt;/span&gt; form factor also brought with it a new breed of computers with more specialised chipsets: the memory controller hub (&lt;span class="caps"&gt;MCH&lt;/span&gt;) and peripheral controller hub (&lt;span class="caps"&gt;PCH&lt;/span&gt;). The &lt;span class="caps"&gt;MCH&lt;/span&gt; coordinates high-throughput components, such as computer memory and graphics. The &lt;span class="caps"&gt;PCH&lt;/span&gt; specialises in lower-throughput&amp;nbsp;needs.&lt;/p&gt;
&lt;p&gt;Much as I try to avoid using acronyms, here they are really just easier to&amp;nbsp;read.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 134: Part 1 – the Intel Core i-series&amp;nbsp;launches!&lt;/p&gt;
&lt;p&gt;I don’t know if you noticed, but there seem to be fewer chips here than on the &lt;span class="caps"&gt;AT&lt;/span&gt; board. That’s misleading though; the components that were on the &lt;span class="caps"&gt;AT&lt;/span&gt; board are also on the &lt;span class="caps"&gt;ATX&lt;/span&gt; board, but greatly shrunk. Some of the functionality that used to require multiple chips on &lt;span class="caps"&gt;AT&lt;/span&gt; have been replaced by a single chip in &lt;span class="caps"&gt;ATX&lt;/span&gt;, hence the appearance of simplicity. In reality, the &lt;span class="caps"&gt;ATX&lt;/span&gt; mainboard is more&amp;nbsp;complex!&lt;/p&gt;
&lt;p&gt;Next issue, onward with the&amp;nbsp;integration!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 11"></category></entry><entry><title>Issue 132: the AT form factor (pre-1995)</title><link href="https://ngjunsiang.github.io/laymansguide/issue132.html" rel="alternate"></link><published>2021-08-07T08:00:00+08:00</published><updated>2021-08-07T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-08-07:/laymansguide/issue132.html</id><summary type="html">&lt;p&gt;Chipsets served as go-betweens in the &lt;span class="caps"&gt;AT&lt;/span&gt; form factor by &lt;span class="caps"&gt;IBM&lt;/span&gt;.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; CPUs have limited throughput, since there is a max frequency they can operate at, and a limit to the number of wires they can be connected to (throughput = no. of wires × frequency). Later designs of early computers increased the capability of computers by delegating more work to secondary&amp;nbsp;chips.&lt;/p&gt;
&lt;p&gt;When computers began hitting the mainstream market, they were designed to be able to use interchangeable parts so as to reduce cost and inventory. To support this effort, manufacturers came up with standards for how to lay out computer components on a mainboard; the different patterns came to be known as &lt;strong&gt;form factors&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;AT&lt;/span&gt; form factor, by &lt;span class="caps"&gt;IBM&lt;/span&gt;, is one of the early ones. An &lt;span class="caps"&gt;AT&lt;/span&gt; motherboard looks something like&amp;nbsp;this:&lt;/p&gt;
&lt;h2&gt;The &lt;span class="caps"&gt;AT&lt;/span&gt;&amp;nbsp;mainboard&lt;/h2&gt;
&lt;p&gt;&lt;img alt="An AT motherboard" src="https://ngjunsiang.github.io/laymansguide/issue132_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;An &lt;span class="caps"&gt;AT&lt;/span&gt; motherboard, with key components outlined.&lt;br /&gt;Annotations are my own.&lt;br /&gt;Original: &lt;a href="https://en.wikipedia.org/wiki/Skylake_(microarchitecture)"&gt;Wikipedia&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Graphics cards, usually added as an expansion card, communicated with the &lt;span class="caps"&gt;CPU&lt;/span&gt; (under the heatsink) through a chipset, while the &lt;span class="caps"&gt;CPU&lt;/span&gt; communicated with memory through another&amp;nbsp;chipset.&lt;/p&gt;
&lt;p&gt;At this point, graphics were still barely powerful enough to run 3D graphics (this was before Windows 95!), and the chipsets mainly served as go-betweens between memory, expansion slots (called buses), and the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;After 1995, this would&amp;nbsp;change.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Chipsets served as go-betweens in the &lt;span class="caps"&gt;AT&lt;/span&gt; form factor by &lt;span class="caps"&gt;IBM&lt;/span&gt;.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 133: the &lt;span class="caps"&gt;ATX&lt;/span&gt; form factor&amp;nbsp;(post-1995)&lt;/p&gt;
&lt;p&gt;Short issue here, just to introduce the idea of chipsets! You can see the chips on the &lt;span class="caps"&gt;AT&lt;/span&gt; board look very similar. On the &lt;span class="caps"&gt;ATX&lt;/span&gt; form factor, they will begin to differentiate and&amp;nbsp;specialise.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 11"></category></entry><entry><title>Issue 131: What do early CPUs and startup founders have in common?</title><link href="https://ngjunsiang.github.io/laymansguide/issue131.html" rel="alternate"></link><published>2021-07-31T08:00:00+08:00</published><updated>2021-07-31T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-07-31:/laymansguide/issue131.html</id><summary type="html">&lt;p&gt;CPUs have limited throughput, since there is a max frequency they can operate at, and a limit to the number of wires they can be connected to (throughput = no. of wires × frequency). Later designs of early computers increased the capability of computers by delegating more work to secondary&amp;nbsp;chips.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; &lt;span class="caps"&gt;AC&lt;/span&gt; power from the wall uses electric current that alternates directions, while &lt;span class="caps"&gt;DC&lt;/span&gt; power from batteries uses electric current that flows in one direction only. All electronics are &lt;span class="caps"&gt;DC&lt;/span&gt;-only, and require an &lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; adapter to be powered from the wall. The &lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; conversion produces a significant amount of heat; &lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; adapters are usually external unless the device has sufficient space or cooling capacity for&amp;nbsp;it.&lt;/p&gt;
&lt;p&gt;This season, let’s open up that computer case and see what’s inside. Where does everything fit, and how does all that information get around? More importantly, how are computers able to cover such a large range of sizes, from towering desktops to tiny&amp;nbsp;smartphones?&lt;/p&gt;
&lt;h2&gt;What a computer wants, what a computer&amp;nbsp;needs&lt;/h2&gt;
&lt;p&gt;The common model of a computer is that it … computes. It calculates. It takes in numbers, and spits out more&amp;nbsp;numbers.&lt;/p&gt;
&lt;p&gt;That’s not quite&amp;nbsp;right.&lt;/p&gt;
&lt;p&gt;While a computer does carry out compute operations, these are far outnumbered by load/store operations (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue058.html"&gt;Issue 58&lt;/a&gt;)). Why so much loading and storing of&amp;nbsp;data?&lt;/p&gt;
&lt;h2&gt;Moving&amp;nbsp;data&lt;/h2&gt;
&lt;p&gt;The &lt;span class="caps"&gt;CPU&lt;/span&gt; itself has precious little storage (&amp;lt;20 &lt;span class="caps"&gt;MB&lt;/span&gt; of cache storage); it is only a wee little chip! Most of the data in a computer is stored in a hard drive or solid state drive; let’s just call them storage drives for&amp;nbsp;now.&lt;/p&gt;
&lt;p&gt;So CPUs have to read data from a storage drive. This is a slow operation, because storage drives are slow; writing to storage drives is even slower than reading from&amp;nbsp;them.&lt;/p&gt;
&lt;p&gt;In the meantime, the &lt;span class="caps"&gt;CPU&lt;/span&gt; needs a place to dump working data; this is computer memory (2–&lt;span class="caps"&gt;32GB&lt;/span&gt;). Memory is slower than the &lt;span class="caps"&gt;CPU&lt;/span&gt;’s cache, but much faster than a storage&amp;nbsp;drive.&lt;/p&gt;
&lt;p&gt;That’s 3 places to stash data so far: storage drives, &lt;span class="caps"&gt;CPU&lt;/span&gt; cache, and computer memory. You with me so&amp;nbsp;far?&lt;/p&gt;
&lt;h2&gt;Pipelines&lt;/h2&gt;
&lt;p&gt;The next place that often requires lots of data is the graphics card (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue123.html"&gt;Issue 123&lt;/a&gt;)). For you to play a video game, the computer has&amp;nbsp;to:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Load game data from the storage&amp;nbsp;disk,&lt;/li&gt;
&lt;li&gt;Store most of it in memory while it’s doing some number crunching in its&amp;nbsp;cache,&lt;/li&gt;
&lt;li&gt;Get the crunched numbers to the graphics card for rendering graphics (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue122.html"&gt;Issue 122&lt;/a&gt;)),&lt;/li&gt;
&lt;li&gt;Load more data from memory while crunching more numbers, and passing them to the graphics&amp;nbsp;card.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This involves far more loading and storing than computation. And there are limitations to how quickly data can be&amp;nbsp;transferred.&lt;/p&gt;
&lt;h2&gt;Throughput&lt;/h2&gt;
&lt;p&gt;How does data get transferred? Through very fine wires usually. One side (e.g. the &lt;span class="caps"&gt;CPU&lt;/span&gt;) applies a voltage to the wire, the other side (e.g. memory) checks the voltage on the wire. No applied voltage = 0, applied voltage =&amp;nbsp;1.&lt;/p&gt;
&lt;p&gt;How does the &lt;span class="caps"&gt;CPU&lt;/span&gt; know when to apply the voltage, and the memory know when to check it? These operations are synchronised through cycles, like a highly coordinated factory. A &lt;span class="caps"&gt;CPU&lt;/span&gt; operates on a frequency of up to billions of cycles per second, each cycle potentially transferring one bit of data (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue040.html"&gt;Issue 40&lt;/a&gt;)) if there are no&amp;nbsp;delays.&lt;/p&gt;
&lt;p&gt;Typically, the transfer rate is somewhat slower; how do we transfer more data per second? By adding more wires! With two wires, we can transfer two bits per cycle; four wires = four bits per second, eight wires = 8 bits per second … at some point, we run into a different problem. The &lt;span class="caps"&gt;CPU&lt;/span&gt; is a small chip, and there is only so much surface area for us to connect wires&amp;nbsp;to.&lt;/p&gt;
&lt;p&gt;&lt;img alt="An Intel Skylake CPU, showing the pins underneath" src="https://ngjunsiang.github.io/laymansguide/issue131_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;An Intel Skylake desktop &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;br /&gt;Each gold contact on the under-surface connects to a pin on the motherboard when the &lt;span class="caps"&gt;CPU&lt;/span&gt; is seated properly in its socket&lt;br /&gt;Source: &lt;a href="https://en.wikipedia.org/wiki/Skylake_(microarchitecture)"&gt;Wikipedia&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Well, that just&amp;nbsp;sucks.&lt;/p&gt;
&lt;h2&gt;The limits of one&amp;nbsp;chip&lt;/h2&gt;
&lt;p&gt;Come to think of it, humans are much the same; we only have two hands and two legs, there are limits to how fast we can do things, and limits to how long we can stay awake working. We mostly get around these limitations by learning to&amp;nbsp;delegate.&lt;/p&gt;
&lt;p&gt;In the same way, computer designs evolved to delegate more work to secondary chips, leaving the &lt;span class="caps"&gt;CPU&lt;/span&gt; to focus on computation. We’ll explore the gradual evolution of these architectures, so you can better appreciate the elegance of the Apple M1’s design&amp;nbsp;;)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; CPUs have limited throughput, since there is a max frequency they can operate at, and a limit to the number of wires they can be connected to (throughput = no. of wires × frequency). Later designs of early computers increased the capability of computers by delegating more work to secondary&amp;nbsp;chips.&lt;/p&gt;
&lt;p&gt;The more I learned about computer architecture, the more I see parallels to startups and organisational culture in general. I was really looking at ways of organising information flows, and observing how the computational limitations of different parts influence the design of the whole chip! This is a constant work in progress, which is why we keep seeing new &lt;span class="caps"&gt;CPU&lt;/span&gt; designs emerge each&amp;nbsp;year.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 132: the &lt;span class="caps"&gt;AT&lt;/span&gt; form factor&amp;nbsp;(pre-1995)&lt;/p&gt;
&lt;p&gt;Let’s start from—nah, I wont go all the way back to the beginning, just to the point where computer architecture was already recognisable in its early modern form. Next issue, a big welcome for the &lt;span class="caps"&gt;AT&lt;/span&gt; form&amp;nbsp;factor!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 11"></category><category term="cache"></category></entry><entry><title>Issue 130: Power limits</title><link href="https://ngjunsiang.github.io/laymansguide/issue130.html" rel="alternate"></link><published>2021-07-24T08:00:00+08:00</published><updated>2021-07-24T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-07-24:/laymansguide/issue130.html</id><summary type="html">&lt;p&gt;&lt;span class="caps"&gt;AC&lt;/span&gt; power from the wall uses electric current that alternates directions, while &lt;span class="caps"&gt;DC&lt;/span&gt; power from batteries uses electric current that flows in one direction only. All electronics are &lt;span class="caps"&gt;DC&lt;/span&gt;-only, and require an &lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; adapter to be powered from the wall. The &lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; conversion produces a significant amount of heat; &lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; adapters are usually external unless the device has sufficient space or cooling capacity for&amp;nbsp;it.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; The larger the surface area, the faster an object loses heat. The larger the temperature difference between an object and its surroundings, the faster the object loses heat. Heat is bad for computers, and CPUs will need cooling to be able to process computations quickly. A mobile phone thus typically uses no more than 4 W of power, a laptop can use 25–45 W, and a desktop can usually use 65 W and more. Two popular ways of increasing the cooling capacity of a device is to attach a larger piece of metal to the chip (passive cooling), or use a fan to force air over the heatsink (active&amp;nbsp;cooling).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Point 1:&lt;/strong&gt; A powerful device produces lots of heat.&lt;br /&gt;
&lt;strong&gt;Point 2:&lt;/strong&gt; A device that produces lots of heat needs a large surface area (directly in contact with the heat source) to stay (relatively)&amp;nbsp;cool.&lt;/p&gt;
&lt;p&gt;These are two of the primary factors determining how tiny a computer can be. Can something the size of an iPhone be as powerful as something the size of a Macbook? It depends on how much cooling is available to&amp;nbsp;it!&lt;/p&gt;
&lt;p&gt;One more factor to add in this issue: power. Without power, none of your devices would work &amp;#8230; and that is one more source of heat to be dissipated,&amp;nbsp;incidentally.&lt;/p&gt;
&lt;h2&gt;Batteries and &lt;span class="caps"&gt;DC&lt;/span&gt;&amp;nbsp;power&lt;/h2&gt;
&lt;p&gt;Some devices need to carry their own stored energy (in the form of batteries); the devices are powered by direct current (&lt;span class="caps"&gt;DC&lt;/span&gt;) from batteries. In this form of power transmission, electric current only flows one way—this is why it is important to put batteries in the right way! Electric current comes out from one end, and re-enters from the other end, marked with + and –&amp;nbsp;symbols.&lt;/p&gt;
&lt;p&gt;Batteries seldom provide power at the voltage required by devices and CPUs. For example, smartphone batteries usually have a voltage of about 3.7 V, even though the &lt;span class="caps"&gt;CPU&lt;/span&gt; usually requires about 1 V to operate. This allows the batteries to power the device with a low current&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;, so as to minimise the heating effect of current in the&amp;nbsp;wires.&lt;/p&gt;
&lt;p&gt;That means some power conversion has to take place on the smartphone’s mainboard. Fortunately, &lt;span class="caps"&gt;DC&lt;/span&gt;-to-&lt;span class="caps"&gt;DC&lt;/span&gt; conversion is highly efficient (though not 100%), so it doesn’t contribute much heat in the&amp;nbsp;device.&lt;/p&gt;
&lt;h2&gt;Wall sockets and &lt;span class="caps"&gt;AC&lt;/span&gt;&amp;nbsp;power&lt;/h2&gt;
&lt;p&gt;On the other hand, alternating current (&lt;span class="caps"&gt;AC&lt;/span&gt;) from wall sockets has electric current flowing both ways—the current switches directions 50 or 60 times a second (see &lt;a href="https://www.oaktreeproducts.com/img/product/description/List%20of%20Worldwide%20AC%20Voltages.pdf"&gt;this &lt;span class="caps"&gt;PDF&lt;/span&gt; of Worldwide &lt;span class="caps"&gt;AC&lt;/span&gt; Voltages &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; Frequencies&lt;/a&gt;). Connecting this directly to a device that needs &lt;span class="caps"&gt;DC&lt;/span&gt; is looking for trouble! This &lt;span class="caps"&gt;AC&lt;/span&gt; power source has to be converted to &lt;span class="caps"&gt;DC&lt;/span&gt; through an &lt;strong&gt;&lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; converter&lt;/strong&gt; (a.k.a. “power brick”, “power supply”, “&lt;span class="caps"&gt;AC&lt;/span&gt; adapter”, …), and that process currently only goes up to 90% peak efficiency&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;That means if you have a desktop running at 100W (maybe while gaming or encoding video files), the &lt;em&gt;&lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; converter alone&lt;/em&gt; draws 111W at the wall socket, &lt;strong&gt;wastes 11W&lt;/strong&gt; (in the form of heat), and provides 100W of power to the&amp;nbsp;desktop.&lt;/p&gt;
&lt;p&gt;And heat is the enemy of&amp;nbsp;CPUs.&lt;/p&gt;
&lt;h2&gt;External vs internal power&amp;nbsp;supplies&lt;/h2&gt;
&lt;p&gt;Ughh, power bricks … so many different types, with different connectors, and we never quite know if we can use one laptop’s power brick on another laptop (at least, until &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C power for laptops came along; more in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue127.html"&gt;Issue 127&lt;/a&gt;))&lt;/p&gt;
&lt;p&gt;In a laptop, you really do not want the power supply dumping this heat &lt;em&gt;into the laptop&lt;/em&gt;! The laptop already has enough work to do getting heat from the &lt;span class="caps"&gt;CPU&lt;/span&gt; out of that cramped space into the surroundings. You don’t want to give it more heat to remove, and risk throttling the &lt;span class="caps"&gt;CPU&lt;/span&gt;’s performance (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue129.html"&gt;Issue 129&lt;/a&gt;)). It’s better that the &lt;span class="caps"&gt;AC&lt;/span&gt; converter/power brick remains external to the laptop, dumping that heat into the surroundings directly without heating up the laptop’s internal&amp;nbsp;space.&lt;/p&gt;
&lt;p&gt;&lt;img alt="An Apple power adapter, with the cover removed" src="https://ngjunsiang.github.io/laymansguide/issue130_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;The internals of the Apple &lt;span class="caps"&gt;AC&lt;/span&gt; converter (i.e. power adapter)&lt;br /&gt;Source: &lt;a href="http://www.righto.com/2015/11/macbook-charger-teardown-surprising.html"&gt;Ken&amp;nbsp;Shiriff&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;In larger devices—the Mac Mini, game consoles (e.g. &lt;span class="caps"&gt;PS4&lt;/span&gt; or Xbox One), and larger desktops, there’s plenty of space in the device’s internals, and they have sufficiently powerful cooling systems that can remove this heat. In desktops especially, the power supply may be large enough that it has its own cooling&amp;nbsp;fan!&lt;/p&gt;
&lt;p&gt;&lt;img alt="A desktop power supply, with the cover removed" src="https://ngjunsiang.github.io/laymansguide/issue130_02.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;The internals of a desktop power supply.&lt;br /&gt;Notice the cooling fan mounted on the back with 4 silver screws, and the two silver heatsinks mounted vertically&lt;br /&gt;Source: &lt;a href="https://en.wikipedia.org/wiki/Power_supply_unit_(computer)"&gt;Wikipedia&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;So for large devices, it makes sense to hide the power supply within the device for a sleeker&amp;nbsp;look.&lt;/p&gt;
&lt;p&gt;In fact, for high-power devices, a power adapter is a poor option. Since power adapters don’t have their own cooling fans, they have limited cooling ability, and are liable to overheat easily if they have to provide &amp;gt;100 W to a device (remember that this means they release at least 11 W of heat, while passive cooling can typically dissipate up to 8&amp;nbsp;W).&lt;/p&gt;
&lt;h2&gt;Device&amp;nbsp;categories&lt;/h2&gt;
&lt;p&gt;Putting together the information from &lt;a href="https://ngjunsiang.github.io/laymansguide/issue129.html"&gt;Issue 129&lt;/a&gt;) and this issue, we can deduce that devices seem to sort themselves into form-factor categories depending on how much power they draw, and how much heat they put&amp;nbsp;out:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Devices drawing &amp;gt;100W at peak, and putting out &amp;gt;80 W of&amp;nbsp;heat:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Generally large, directly powered from the wall (by &lt;span class="caps"&gt;AC&lt;/span&gt;), with power supply within the&amp;nbsp;device.&lt;/p&gt;
&lt;p&gt;In rare cases they do use external &lt;span class="caps"&gt;AC&lt;/span&gt; adapters (such as high-power gaming&amp;nbsp;laptops).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Devices drawing 12–65W at peak, putting out 25–45W of&amp;nbsp;heat:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;These devices cannot be passively cooled, and thus require active cooling (i.e. a cooling fan and&amp;nbsp;heatsink).&lt;/p&gt;
&lt;p&gt;To avoid adding heat to the device from the &lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; conversion process, they usually use external &lt;span class="caps"&gt;AC&lt;/span&gt;&amp;nbsp;adapters.&lt;/p&gt;
&lt;p&gt;Even in devices as small as the Nintendo Switch, you can usually spot the cooling vents where the cooling fan blows warm air from the device into the&amp;nbsp;surroundings.&lt;/p&gt;
&lt;p&gt;These may be safely powered by &lt;span class="caps"&gt;USB&lt;/span&gt;&amp;nbsp;Type-C.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Devices drawing &amp;lt;12W at peak, putting out &amp;lt;10W of&amp;nbsp;heat:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;These devices can be passively&amp;nbsp;cooled.&lt;/p&gt;
&lt;p&gt;Large devices, such as tablets, have a larger surface area to dissipate heat and can afford to draw as much as 8–10W, while smaller devices such as smartphones typically have to remain under&amp;nbsp;5W.&lt;/p&gt;
&lt;p&gt;These are usually powered by &lt;span class="caps"&gt;USB&lt;/span&gt; (at a voltage of 5V), though some may draw power at&amp;nbsp;9V.&lt;/p&gt;
&lt;p&gt;The M1 Macbook Air is passively cooled and thus in this category because its M1 processor is configured to limit its maximum heat output to approx. 10W or less; the M1 Macbook Pro has a cooling fan and a higher max heat output configuration, which allows it to perform at greater&amp;nbsp;capacity.&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;If you spot a device in the wild that claims to have performance much greater than its form factor—its shape, size, footprint—suggests, you would be wise to suspect over-optimism or a scam! At least until it is clear how they plan to provide that power and get rid of that heat&amp;nbsp;…&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; &lt;span class="caps"&gt;AC&lt;/span&gt; power from the wall uses electric current that alternates directions, while &lt;span class="caps"&gt;DC&lt;/span&gt; power from batteries uses electric current that flows in one direction only. All electronics are &lt;span class="caps"&gt;DC&lt;/span&gt;-only, and require an &lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; adapter to be powered from the wall. The &lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; conversion produces a significant amount of heat; &lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; adapters are usually external unless the device has sufficient space or cooling capacity for&amp;nbsp;it.&lt;/p&gt;
&lt;p&gt;I’m noticing a pattern: issues where I explain concepts tend to be shorter than issues where I explain limits due to engineering and the physics of reality. I hope I can shorten the latter without sacrificing practical knowledge. Let me know how you’re finding these issues&amp;nbsp;:)&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S11] Issue 131: What do early CPUs and startup founders have in&amp;nbsp;common?&lt;/p&gt;
&lt;p&gt;This season was focused on firmware and computer components; it is part 1 of a set of concepts I need, so as to explain why the Apple M1 processor is a game-changer for personal computers. I explained what a graphics card is and what it does, I explained why some laptops are upgradeable and why some are not, I explained why some devices need cooling fans and others don’t, and I summarised the relationship between device form factors and their power&amp;nbsp;limits.&lt;/p&gt;
&lt;p&gt;Part 2 will extend this exploration inside the computer. I noticed that layfolks’ mental concept of a computer typically includes the idea that there is a &lt;span class="caps"&gt;CPU&lt;/span&gt;, memory, a hard disk/solid state drive, and maybe a graphics card inside a computer. That’s plenty good enough for everyday life; it’s like understanding that all the employees of a company are in a particular building. But it is insufficient for understanding &lt;em&gt;why the M1 is so much faster&lt;/em&gt;; you’re going to need to know where the employees are situated, and what their workflow is&amp;nbsp;like!&lt;/p&gt;
&lt;p&gt;It’s a tempting but misleading story to imagine that Apple simply has much better engineers; I would say that their engineers were instead under the influence of incentives that allowed them to imagine a more coherent architecture. Let’s get into it starting next issue, again beginning from first principles: how exactly does the &lt;span class="caps"&gt;CPU&lt;/span&gt;, memory, and storage disk work&amp;nbsp;together?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Power = Voltage × Current, so to provide the same power with a lower current, you have to provide it at a higher voltage&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;&lt;span class="caps"&gt;AC&lt;/span&gt;-&lt;span class="caps"&gt;DC&lt;/span&gt; converters typically have a range of input power they can convert (e.g. 0–65 W for laptop adapters, 0–500 W for desktops). The efficiency is highest at about 50% of that load, and efficiency drops as the load increases or decreases from that point.&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 10"></category></entry><entry><title>Issue 129: Cooling</title><link href="https://ngjunsiang.github.io/laymansguide/issue129.html" rel="alternate"></link><published>2021-07-17T08:00:00+08:00</published><updated>2021-07-17T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-07-17:/laymansguide/issue129.html</id><summary type="html">&lt;p&gt;The larger the surface area, the faster an object loses heat. The larger the temperature difference between object and surroundings, the faster the object loses heat. Heat is bad for computers, and CPUs will need cooling to be able to process computations quickly. A mobile phone thus typically uses no more than 4 W of power, a laptop can use 25–45 W, and a desktop can usually use 65 W and more. Two popular ways of increasing the cooling capacity of a device is to attach a larger piece of metal to the chip (passive cooling), or use a fan to force air over the heatsink (active&amp;nbsp;cooling).&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Upgradable parts need a slot or socket to be inserted into; these slots/sockets need to be made robust enough, causing them to take up more space than a soldered part. Devices which were designed to be small and portable generally eliminate these as far as possible, opting to have parts directly soldered to the board&amp;nbsp;instead.&lt;/p&gt;
&lt;p&gt;Why do computers need&amp;nbsp;power?&lt;/p&gt;
&lt;p&gt;Other home appliances I can understand. They need to heat up air/water, move air/water around, or extract heat from air/water to move it elsewhere. These things all need energy. But a computer … all it does is move electrons around! All the information in a computer that changes is just electrons moving; that should not need so much power, should&amp;nbsp;it?&lt;/p&gt;
&lt;p&gt;As it turns out, energy-information equivalence theories posit that manipulating information does increase entropy which does involve energy—but this is the Layman’s Guide to Computing, not a physics newsletter. Let’s just say that managing information, in its abstract sense, needs very little&amp;nbsp;energy.&lt;/p&gt;
&lt;h2&gt;Energy usage in&amp;nbsp;computers&lt;/h2&gt;
&lt;p&gt;What happens to all the energy that a computer uses, then? Some miniscule amount of it goes to manipulating information. A tiny amount goes to lighting up LEDs (these devices somehow always have LEDs), and maybe running the cooling fans. The rest of it is wasted as &lt;strong&gt;heat&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The history of computing is also a history of less and less wasted heat. The Cray 2 supercomputer, in 1985, needed 195 kW to produce 1.9 gigaflops (1.9 billion &lt;strong&gt;fl&lt;/strong&gt;oating-point &lt;strong&gt;o&lt;/strong&gt;perations &lt;strong&gt;p&lt;/strong&gt;er &lt;strong&gt;s&lt;/strong&gt;econd; more context in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue123.html"&gt;Issue 123&lt;/a&gt;)) of computational performance. The iPhone &lt;span class="caps"&gt;XS&lt;/span&gt;, in 2016, needed less than 1 W to produce 1&amp;nbsp;gigaflop.&lt;/p&gt;
&lt;p&gt;No, I’m not going into the environmental concerns and carbon footprint of computing. We have more tangible and immediate concerns&amp;nbsp;here.&lt;/p&gt;
&lt;p&gt;Because heat is bad for computers. When microprocessor chips heat up, the semiconductor material they are made of no longer behaves as it should; it gets “leaky”, allowing electrons to go where they shouldn’t. The data it handles starts to get corrupted, and it eventually&amp;nbsp;crashes.&lt;/p&gt;
&lt;h2&gt;Thermal&amp;nbsp;throttling&lt;/h2&gt;
&lt;p&gt;In the past, microprocessors were simpler, and had no thermal controls whatsoever; if you allowed them to run as-is, without any cooling assistance, they would just run until they began smoking and sometimes even catch fire (enjoy this &lt;a href="https://www.youtube.com/watch?v=Xf0VuRG7MN4&amp;amp;t=99s"&gt;2005 video of a &lt;span class="caps"&gt;CPU&lt;/span&gt; doing just that&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Today, CPUs are somewhat more sophisticated. Once they start heating up to their thermal limit, onboard thermal control circuitry will attempt to &lt;strong&gt;throttle&lt;/strong&gt; the &lt;span class="caps"&gt;CPU&lt;/span&gt;’s performance to keep it at a safe temperature. So you will have a laggy and unresponsive &lt;span class="caps"&gt;CPU&lt;/span&gt;, but at least it’s not on&amp;nbsp;fire!&lt;/p&gt;
&lt;p&gt;Still, most CPUs are going to need some sort of heatsink that helps to draw heat away from it, and dissipate the heat into the&amp;nbsp;surroundings.&lt;/p&gt;
&lt;h2&gt;Getting rid of&amp;nbsp;heat&lt;/h2&gt;
&lt;p&gt;There are two things you need to know about heat loss to the&amp;nbsp;surroundings:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The larger the surface area, the faster an object loses&amp;nbsp;heat.&lt;/li&gt;
&lt;li&gt;The larger the temperature difference between object and surroundings, the faster the object loses&amp;nbsp;heat.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;CPUs are kind of at a disadvantage&amp;nbsp;here.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;They are really small; the part that is in contact with the heatsink is usually about 400 sq mm (approx. 2 cm by 2 cm, or 0.64 sq in)&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;li&gt;The thermal limit for most CPUs is only about 100 °C (212 F), compared to most metals which have theirs in the hundreds or even over a thousand&amp;nbsp;°C&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Heatsinks&lt;/h2&gt;
&lt;p&gt;In practice, this means that a bare chip can only run about 4 W before it starts to run into its thermal limits (we call this &lt;strong&gt;overheating&lt;/strong&gt;). Anything more powerful than a basic home router or smart device (such as the Amazon Echo or Google Home Assistant) will need some sort of heatsink to avoid&amp;nbsp;overheating.&lt;/p&gt;
&lt;p&gt;The simplest way to cool a chip is to slap a piece of metal on it to increase the surface area (factor 2). This is known as &lt;strong&gt;passive cooling&lt;/strong&gt;. A paste called &lt;strong&gt;thermal paste&lt;/strong&gt; is applied between the heatsink and chip to improve the transfer of heat. To pack as much surface area as possible into a tiny space, this piece of metal usually has long, thin fins, giving the characteristic look of&amp;nbsp;heatsinks:&lt;/p&gt;
&lt;p&gt;&lt;img alt="A passive heatsink on a chip" src="https://ngjunsiang.github.io/laymansguide/issue129_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;A passive heatsink on the northbridge chip of a computer mainboard&lt;br /&gt;Source: found on &lt;a href="https://superuser.com/questions/1043094/difference-between-active-and-passive-heatsink"&gt;Superuser&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;The effectiveness of passive heatsinks depends on the ambient airflow around it. Some creative setups that manage to get &lt;em&gt;the metal case itself&lt;/em&gt; in contact with the &lt;span class="caps"&gt;CPU&lt;/span&gt; can readily cool up to 45 W, with zero fan&amp;nbsp;noise!&lt;/p&gt;
&lt;p&gt;For mobile phones, tablets, and laptops, such heatsinks would add too much to the device thickness. Instead, the CPUs are usually in direct contact with a larger metal surface, sometimes even the metal back of the device; this is why they feel warm to you in the first place. This allows tablets to use up to approx. 8 W of&amp;nbsp;power.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Logic board cover in an iPad" src="https://ngjunsiang.github.io/laymansguide/issue129_02.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;The logic board cover in an iPad Pro 11 has copper inside; it helps to spread heat to the rest of the device instead of concentrating it all in one spot&lt;br /&gt;Source: &lt;a href="https://www.ifixit.com/Teardown/iPad+Pro+11-Inch+Teardown/115457"&gt;iFixit&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Where there isn’t this luxury of space, another option is to use a slim heatsink, and increase its cooling ability by forcing air through it. This form of cooling is called &lt;strong&gt;active cooling&lt;/strong&gt;, and usually done with a fan of some sort, a popular option for thicker laptops. This allows laptops to run between 25–45 W, and desktop computers to run 65 W and hotter (with larger heatsinks, of&amp;nbsp;course)&lt;/p&gt;
&lt;p&gt;&lt;img alt="An active heatsink on a chip" src="https://ngjunsiang.github.io/laymansguide/issue129_03.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;An active heatsink on the &lt;span class="caps"&gt;CPU&lt;/span&gt; of a computer mainboard&lt;br /&gt;Source: found on &lt;a href="https://superuser.com/questions/1043094/difference-between-active-and-passive-heatsink"&gt;Superuser&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;These numbers are for stereotypical mobile devices, laptops, and desktops; stranger or hybrid designs may have different cooling capacities (e.g. a tiny cube desktop might only have 25–35 W of cooling&amp;nbsp;capacity).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; The larger the surface area, the faster an object loses heat. The larger the temperature difference between object and surroundings, the faster the object loses heat. Heat is bad for computers, and CPUs will need cooling to be able to process computations quickly. A mobile phone thus typically uses no more than 4 W of power, a laptop can use 25–45 W, and a desktop can usually use 65 W and more. Two popular ways of increasing the cooling capacity of a device is to attach a larger piece of metal to the chip (passive cooling), or use a fan to force air over the heatsink (active&amp;nbsp;cooling).&lt;/p&gt;
&lt;p&gt;If you walk away with only one thing from this issue, it’s that heat dissipation constrains the max performance for any device, and heat dissipation is usually constrained by the surface area available for&amp;nbsp;cooling.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 130: Power&amp;nbsp;limits&lt;/p&gt;
&lt;p&gt;Computers turn electrical power to heat, drawing a miniscule amount off for computation. But where does that power come from? In the last issue of Season 10, I’ll touch on a highly underrated hardware component: the power&amp;nbsp;supply.&lt;/p&gt;
&lt;p&gt;This and more, next&amp;nbsp;issue!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Multiple CPUs are produced from a single semiconductor wafer. Keeping CPUs small maximises the yield from manufacturing, and reduces the chance of a manufacturing error on any single &lt;span class="caps"&gt;CPU&lt;/span&gt;. This is going to take a whole ’nother season to explain …&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 10"></category></entry><entry><title>Issue 128: Upgradeability</title><link href="https://ngjunsiang.github.io/laymansguide/issue128.html" rel="alternate"></link><published>2021-07-10T08:00:00+08:00</published><updated>2021-07-10T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-07-10:/laymansguide/issue128.html</id><summary type="html">&lt;p&gt;Upgradable parts need a slot or socket to be inserted into; these slots/sockets need to be made robust enough, causing them to take up more space than a soldered part. Devices which were designed to be small and portable generally eliminate these as far as possible, opting to have parts directly soldered to the board&amp;nbsp;instead.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; &lt;span class="caps"&gt;USB&lt;/span&gt; Power Delivery is a specification that describes how much voltage and current can be supplied by different categories of &lt;span class="caps"&gt;USB&lt;/span&gt; cables. It allows power delivery at different levels for all kids of connected devices, up to 100W. This should help to simplify cable setups that otherwise require multiple kinds of cables between two closely interconnected devices (such as a laptop and an external&amp;nbsp;monitor).&lt;/p&gt;
&lt;p&gt;At some point in the past, computers could be upgraded with all kinds of parts: you could upgrade to a better network card, a better processor, or add more memory, without changing out the entire&amp;nbsp;computer!&lt;/p&gt;
&lt;h2&gt;The short history of personal&amp;nbsp;computers&lt;/h2&gt;
&lt;p&gt;After &lt;span class="caps"&gt;IBM&lt;/span&gt; released the &lt;span class="caps"&gt;IBM&lt;/span&gt; 360, the first mainframe that was meant to be used in both large-scale and small-scale applications, it quickly realised that providing service support for it was going to be a nightmare if each version required its own specialised support. In the 1970s, it was already thinking of a family-of-parts concept that allowed its products to use a set of interchangeable parts to reduce the number of these&amp;nbsp;parts.&lt;/p&gt;
&lt;p&gt;But Intel, founded in 1971, had made a major breakthrough in manufacturing low cost microprocessors. The price of computers dropped steadily in response, allowing the personal computer (&lt;span class="caps"&gt;PC&lt;/span&gt;) to become affordable to home users, and the family-of-parts concept came along for the&amp;nbsp;ride.&lt;/p&gt;
&lt;p&gt;The early computers, when purchased, needed a network card to connect to a network, needed a sound card to produce sound, needed a graphics card to view graphics … you basically needed a card for&amp;nbsp;everything!&lt;/p&gt;
&lt;h2&gt;Upgradeable&amp;nbsp;parts&lt;/h2&gt;
&lt;p&gt;On a desktop, you could upgrade a number of&amp;nbsp;components.&lt;/p&gt;
&lt;p&gt;Desktop CPUs were purchased separately, and had to be installed into a socket on a mainboard (also called a&amp;nbsp;motherboard).&lt;/p&gt;
&lt;p&gt;&lt;img alt="Intel CPU socket" src="https://ngjunsiang.github.io/laymansguide/issue128_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;A &lt;span class="caps"&gt;CPU&lt;/span&gt; socket on a mainboard&lt;br /&gt;Source: &lt;a href="https://www.digitaltrends.com/computing/what-are-intels-lga-processor-sockets/"&gt;Digital&amp;nbsp;Trends&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Desktop computer memory was likewise purchased separately, and slotted&amp;nbsp;in:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Desktop memory slots" src="https://ngjunsiang.github.io/laymansguide/issue128_02.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;Memory slots on a desktop mainboard&lt;br /&gt;Source: &lt;a href="https://bleuwire.com/how-to-determine-memory-slots-motherboard-windows-10/"&gt;Bleuwire&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;On top of that, you still had to buy your own &lt;span class="caps"&gt;DVD&lt;/span&gt;/Bluray drives, storage disks, and other peripherals, to be plugged in laboriously to the mainboard. Many of these features made it into laptops as&amp;nbsp;well.&lt;/p&gt;
&lt;h2&gt;Laptop&amp;nbsp;upgrades&lt;/h2&gt;
&lt;p&gt;Because of the smaller space available, the &lt;span class="caps"&gt;CPU&lt;/span&gt; socket had to go. Most laptops have their CPUs soldered directly to the mainboard, and the &lt;span class="caps"&gt;CPU&lt;/span&gt; cannot be upgraded separately. Buying a laptop with a better &lt;span class="caps"&gt;CPU&lt;/span&gt; often means buying it with a different configuration, mainboard and&amp;nbsp;all.&lt;/p&gt;
&lt;p&gt;But laptops retained the ability to upgrade memory through the use of memory slots. These were oriented flat along the board to reduce&amp;nbsp;space.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Laptop memory slots" src="https://ngjunsiang.github.io/laymansguide/issue128_03.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;Memory slots on a laptop&lt;br /&gt;Source: &lt;a href="https://www.laptopmag.com/articles/ram-upgrade-tutorial"&gt;Laptopmag&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;And they also retained the ability to swap out or replace their disks, whether hard disk or solid state&amp;nbsp;drive.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Laptop hard disk slot" src="https://ngjunsiang.github.io/laymansguide/issue128_04.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;The hard disk slot on a laptop&lt;br /&gt;Source: &lt;a href="https://www.tweaktown.com/tweakipedia/2/upgrading-your-laptop-to-a-new-high-performance-ssd-and-ram/index.html"&gt;TweakTown&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;&lt;img alt="Laptop SSD slots" src="https://ngjunsiang.github.io/laymansguide/issue128_05.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;The &lt;span class="caps"&gt;SSD&lt;/span&gt; slots on a laptop&lt;br /&gt;Source: &lt;a href="https://www.easeus.com/backup-utility/clone-m2-ssd-to-larger-m2-ssd.html"&gt;EaseUS&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;h2&gt;Limitations of upgradable&amp;nbsp;parts&lt;/h2&gt;
&lt;p&gt;A common limitation here is that replaceable components take up more space than directly-soldered ones, because the sockets and slots cannot be manufactured too thin if they are to be robust. As the industry moves towards thin-and-light designs, and hardware support moves to a system that replaces the whole device rather than individual parts, these replaceable parts are already on their way&amp;nbsp;out.&lt;/p&gt;
&lt;p&gt;On the Apple Macbook, Apple has already soldered down the &lt;span class="caps"&gt;CPU&lt;/span&gt;, memory, and &lt;span class="caps"&gt;SSD&lt;/span&gt;, allowing for no upgrades. This is also the case for many other thin-and-light laptop&amp;nbsp;manufacturers.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Upgradable parts need a slot or socket to be inserted into; these slots/sockets need to be made robust enough, causing them to take up more space than a soldered part. Devices which were designed to be small and portable generally eliminate these as far as possible, opting to have parts directly soldered to the board&amp;nbsp;instead.&lt;/p&gt;
&lt;p&gt;There are of course other reasons for this transition, economic as well as financial, which I will not tackle here. I just wanted the chance to show some upgrade slots so if you ever get to see an old laptop opened up, you know what they are&amp;nbsp;for!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 129:&amp;nbsp;Cooling&lt;/p&gt;
&lt;p&gt;Next issue, another dive into another issue: why is my laptop running so hot? Why is it so loud? Why can’t I get a more powerful processor in this tiny &lt;span class="caps"&gt;PC&lt;/span&gt;? Why is my i7 slower than my friend’s&amp;nbsp;i5?&lt;/p&gt;
&lt;p&gt;This and more, next&amp;nbsp;issue!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 10"></category><category term="memory"></category></entry><entry><title>Issue 127: USB Type-C Power Delivery</title><link href="https://ngjunsiang.github.io/laymansguide/issue127.html" rel="alternate"></link><published>2021-07-03T08:00:00+08:00</published><updated>2021-07-03T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-07-03:/laymansguide/issue127.html</id><summary type="html">&lt;p&gt;&lt;span class="caps"&gt;USB&lt;/span&gt; Power Delivery is a specification that describes how much voltage and current can be supplied by different categories of &lt;span class="caps"&gt;USB&lt;/span&gt; cables. It allows power delivery at different levels for all kinds of connected devices, up to 100W. This should help to simplify cable setups that otherwise require multiple kinds of cables between two closely interconnected devices (such as a laptop and an external&amp;nbsp;monitor).&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; &lt;span class="caps"&gt;USB&lt;/span&gt; is a (licensed) technical standard that describes how devices connect to each other through a cable. &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C is a new connector standard that supports &lt;span class="caps"&gt;USB&lt;/span&gt; 3, DisplayPort, &lt;span class="caps"&gt;HDMI&lt;/span&gt;, and Thunderbolt. It is able to carry multiple types of data simultaneously, in limited combinations. In a &lt;span class="caps"&gt;USB&lt;/span&gt; connection, one device acts as the host while the other acts as the device; the host initiates all&amp;nbsp;communication.&lt;/p&gt;
&lt;p&gt;Last week, I differentiated the &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C &lt;strong&gt;cable&lt;/strong&gt; specification from the &lt;span class="caps"&gt;USB3&lt;/span&gt; &lt;strong&gt;data&lt;/strong&gt; specification; the former describes how the cable and connector should be, while the latter describes how to transmit &lt;span class="caps"&gt;USB&lt;/span&gt; data over a Type-C cable. Remember that in addition to &lt;span class="caps"&gt;USB3&lt;/span&gt; data, the Type-C cable can also transmit &lt;span class="caps"&gt;HDMI&lt;/span&gt; or DisplayPort video&amp;nbsp;data!&lt;/p&gt;
&lt;p&gt;What else can a Type-C cable do? Oh, right—provide power to devices. In other words, charge&amp;nbsp;them.&lt;/p&gt;
&lt;h2&gt;What’s wrong with &lt;span class="caps"&gt;USB2&lt;/span&gt;&amp;nbsp;charging?&lt;/h2&gt;
&lt;p&gt;Absolutely nothing! At this point, it was not expected that &lt;span class="caps"&gt;USB&lt;/span&gt; would be needed for powering anything more than some basic peripherals, like keyboards, mice, and anything less power-consuming than a small external hard disk. 5V (volts) of voltage with 0.5A (amperes) could provide 2.5W of power, and that was considered plenty&amp;nbsp;enough.&lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;USB&lt;/span&gt; Implementers’ Forum (&lt;span class="caps"&gt;USB&lt;/span&gt;-&lt;span class="caps"&gt;IF&lt;/span&gt;) didn’t want to set too high a standard for &lt;span class="caps"&gt;USB&lt;/span&gt; devices and peripherals (&lt;em&gt;including&lt;/em&gt; &lt;span class="caps"&gt;USB&lt;/span&gt;-certified cables); original equipment manufacturers (OEMs), the ones who &lt;em&gt;actually had to manufacture the goods&lt;/em&gt;, would complain about the high cost. Nobody likes being undercut by cheap knockoffs that don’t bother applying for the license and following the specs (do you notice the official &lt;span class="caps"&gt;USB&lt;/span&gt; logo when buying?). So for a long time, we had up to 2.5W of power from &lt;span class="caps"&gt;USB&lt;/span&gt;&amp;nbsp;ports.&lt;/p&gt;
&lt;p&gt;And then tablets came along, drawing 8–12W of power to do whatever they have to. Aside from the iPad, which used its own connector, these were charged over &lt;span class="caps"&gt;USB&lt;/span&gt;. So their manufacturers had to come up with &lt;em&gt;kludges&lt;/em&gt; to work around &lt;span class="caps"&gt;USB&lt;/span&gt; limitations. They had Quick Charge, Dash Charge, and all kinds of other standards which were not approved by the &lt;span class="caps"&gt;USB&lt;/span&gt;-&lt;span class="caps"&gt;IF&lt;/span&gt;, just to allow their cables to provide up to 12W of power (3A of current, more in some cases) to their tablets while&amp;nbsp;charging.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;USB&lt;/span&gt; 3.0 bumped the limit up to 0.9A (providing 4.5W), which was nice but far from enough. The hard limit was the cable itself though: drawing anything more than 5A over the usual thin &lt;span class="caps"&gt;USB&lt;/span&gt; cables would cause them to heat up to unsafe levels. Clearly, something more was&amp;nbsp;needed.&lt;/p&gt;
&lt;h2&gt;&lt;span class="caps"&gt;USB&lt;/span&gt; Power&amp;nbsp;Delivery&lt;/h2&gt;
&lt;p&gt;From 2012, the &lt;span class="caps"&gt;USB&lt;/span&gt;-&lt;span class="caps"&gt;IF&lt;/span&gt; finally added a Power Delivery (&lt;span class="caps"&gt;PD&lt;/span&gt;) specification, which allowed power to be delivered over &lt;span class="caps"&gt;USB&lt;/span&gt; cables at &lt;em&gt;different voltages&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;In addition to 5V, which is used by phones, tablets, and their power banks, the &lt;span class="caps"&gt;PD&lt;/span&gt; spec also allows charging at 9V (fast charging for some devices), 15V (for higher-power devices like the Nintendo Switch), and 20V (what most laptops use for &lt;span class="caps"&gt;PD&lt;/span&gt; charging). With a current of up to 5A, this technically allows up to 100W of power to be delivered—sufficient for pretty much all&amp;nbsp;laptops.&lt;/p&gt;
&lt;h3&gt;Which voltage is&amp;nbsp;used?&lt;/h3&gt;
&lt;p&gt;The actual voltage to be delivered by the charging host is &lt;em&gt;negotiated&lt;/em&gt; with the host. When a charging device is connected, it communicates the voltages it can support, the host compares it with the voltages it can supply, and power at a supported voltage is&amp;nbsp;delivered.&lt;/p&gt;
&lt;h3&gt;Can any cable be&amp;nbsp;used?&lt;/h3&gt;
&lt;p&gt;Greater current requires a thicker cable to be used, as thinner cables have more resistance and will heat up to unsafe levels. A cable following the &lt;span class="caps"&gt;USB&lt;/span&gt;-&lt;span class="caps"&gt;PD&lt;/span&gt; specification will negotiate the correct voltage and current in any case, so if your cable is not charging at a level you know is supported by both your devices, do check the cable rating. You may have to buy a higher-rated&amp;nbsp;cable.&lt;/p&gt;
&lt;h2&gt;Are there any advantages to buying a more expensive&amp;nbsp;cable?&lt;/h2&gt;
&lt;p&gt;Users of external monitor screens and docking bays often have to connect multiple cables from those devices to their laptops for power, (&lt;span class="caps"&gt;USB&lt;/span&gt;) data, and video output. With &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C and &lt;span class="caps"&gt;USB&lt;/span&gt;-&lt;span class="caps"&gt;PD&lt;/span&gt; specifications unifying these three requirements into one cable, we will (eventually) be able to connect a laptop to a Type-C monitor using a Type-C cable, and this cable will supply power plus allow the laptop to use all devices connected to the&amp;nbsp;monitor.&lt;/p&gt;
&lt;p&gt;It’s supposed to simplify the physical cable mess, at the cost of having to manage a more complicated specification. Let’s see how that plays out in the next&amp;nbsp;decade.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; &lt;span class="caps"&gt;USB&lt;/span&gt; Power Delivery is a specification that describes how much voltage and current can be supplied by different categories of &lt;span class="caps"&gt;USB&lt;/span&gt; cables. It allows power delivery at different levels for all kinds of connected devices, up to 100W. This should help to simplify cable setups that otherwise require multiple kinds of cables between two closely interconnected devices (such as a laptop and an external&amp;nbsp;monitor).&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 128:&amp;nbsp;Upgradeability&lt;/p&gt;
&lt;p&gt;Millennials and other older computer users may remember the glory days of the desktop, when almost any component could be removed and swapped out for another. Laptops used to enjoy this upgradability to a lesser extent; the laptop memory and hard disk came as separate slotted cards that could be replaced with upgraded versions for increased&amp;nbsp;performance.&lt;/p&gt;
&lt;p&gt;What happened to that trend today? The reason mainly lies in the realm of economics, but I figured I’d use the chance to dig a little deeper and explain what is going on with the hardware that no longer allows this to&amp;nbsp;happen.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 10"></category></entry><entry><title>Issue 126: USB Type-C</title><link href="https://ngjunsiang.github.io/laymansguide/issue126.html" rel="alternate"></link><published>2021-06-26T08:00:00+08:00</published><updated>2021-06-26T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-06-26:/laymansguide/issue126.html</id><summary type="html">&lt;p&gt;&lt;span class="caps"&gt;USB&lt;/span&gt; is a (licensed) technical standard that describes how devices connect to each other through a cable. &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C is a new connector standard that supports &lt;span class="caps"&gt;USB&lt;/span&gt; 3, DisplayPort, &lt;span class="caps"&gt;HDMI&lt;/span&gt;, and Thunderbolt. It is able to carry multiple types of data simultaneously, in limited combinations. In a &lt;span class="caps"&gt;USB&lt;/span&gt; connection, one device acts as the host while the other acts as the device; the host initiates all&amp;nbsp;communication.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Analog formats such as &lt;span class="caps"&gt;VGA&lt;/span&gt; mostly contain the control signals that the &lt;span class="caps"&gt;CRT&lt;/span&gt; needs to operate, while digital formats such as &lt;span class="caps"&gt;HDMI&lt;/span&gt; and DisplayPort contain image data that the device must convert to control signals. Analog signals need a digital-analog-conversion (&lt;span class="caps"&gt;DAC&lt;/span&gt;) chip to be converted to digital signals, hence &lt;span class="caps"&gt;VGA&lt;/span&gt;-&lt;span class="caps"&gt;HDMI&lt;/span&gt; adapters tend to be more costly than DisplayPort-&lt;span class="caps"&gt;HDMI&lt;/span&gt; adapters. Dedicated graphics cards generally support more simultaneous output video streams than integrated graphics&amp;nbsp;cards.&lt;/p&gt;
&lt;p&gt;This week, I attempt to untangle the confusion around &lt;strong&gt;&lt;span class="caps"&gt;USB&lt;/span&gt; Type-C&lt;/strong&gt;, informally also referred to as &lt;span class="caps"&gt;USB&lt;/span&gt;-C.&lt;/p&gt;
&lt;h2&gt;What is &lt;span class="caps"&gt;USB&lt;/span&gt;&amp;nbsp;Type-C?&lt;/h2&gt;
&lt;p&gt;It is a connector standard. It sets standards for &lt;em&gt;this&lt;/em&gt;&amp;nbsp;connector:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Picture of a USB-C plug" src="https://ngjunsiang.github.io/laymansguide/issue126_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;&lt;span class="caps"&gt;USB&lt;/span&gt;-C plug&lt;br /&gt;Image via &lt;a href="https://en.wikipedia.org/wiki/USB-C"&gt;Wikipedia&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;What does a connector standard do? It determines how many pins the connector should have, and what each of the pins should be used for, how the connector should be shaped, how the docking port (where the cable gets plugged into) should be designed, and other similar details. It’s all about the&amp;nbsp;docking.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Pinout diagram of a USB-C plug" src="https://ngjunsiang.github.io/laymansguide/issue126_02.png" /&gt;&lt;br /&gt;
&lt;em&gt;Pinout diagram of a &lt;span class="caps"&gt;USB&lt;/span&gt;-C plug&lt;br /&gt;Image via &lt;a href="https://en.wikipedia.org/wiki/USB-C"&gt;Wikipedia&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;h2&gt;But won’t somebody think about the&amp;nbsp;data?!&lt;/h2&gt;
&lt;p&gt;Ah, now we’re going back in history&amp;nbsp;…&lt;/p&gt;
&lt;p&gt;Universal Serial Bus (&lt;strong&gt;&lt;span class="caps"&gt;USB&lt;/span&gt;&lt;/strong&gt;) is a (set of) industry standards that sets requirements and protocols for—well, how data is transferred from one device to another. It is maintained by the &lt;span class="caps"&gt;USB&lt;/span&gt; Implementers Forum (&lt;span class="caps"&gt;USB&lt;/span&gt;-&lt;span class="caps"&gt;IF&lt;/span&gt;). The first version of the standard was released in 1996, second version (&lt;span class="caps"&gt;USB&lt;/span&gt; 2.0) in 2000, and third version (&lt;span class="caps"&gt;USB&lt;/span&gt; 3.0) in&amp;nbsp;2008.&lt;/p&gt;
&lt;p&gt;While &lt;span class="caps"&gt;USB&lt;/span&gt; 2.0 (or Hi-Speed &lt;span class="caps"&gt;USB&lt;/span&gt;) supported a transfer rate of &lt;em&gt;up to&lt;/em&gt; 60 &lt;span class="caps"&gt;MB&lt;/span&gt;/s, &lt;span class="caps"&gt;USB&lt;/span&gt; 3.0 supports &lt;em&gt;up to&lt;/em&gt; 625 &lt;span class="caps"&gt;MB&lt;/span&gt;/s, allowing for much faster transfers from external disks and other&amp;nbsp;devices.&lt;/p&gt;
&lt;p&gt;We don’t have to worry so much about these versions, because &lt;span class="caps"&gt;USB&lt;/span&gt; is designed to be backward-compatible. That means all devices that support &lt;span class="caps"&gt;USB2&lt;/span&gt; also support &lt;span class="caps"&gt;USB1&lt;/span&gt;, and all devices supporting &lt;span class="caps"&gt;USB3&lt;/span&gt; also support &lt;span class="caps"&gt;USB2&lt;/span&gt;. The primary advantage that each successive &lt;span class="caps"&gt;USB&lt;/span&gt; version has over previous versions is higher throughput, more supported features, and more connectors to confuse (okay, that last isn’t an advantage&amp;nbsp;😛).&lt;/p&gt;
&lt;p&gt;&lt;img alt="Connector shapes for different USB versions" src="https://ngjunsiang.github.io/laymansguide/issue126_03.png" /&gt;&lt;br /&gt;
&lt;em&gt;Comparison of &lt;span class="caps"&gt;USB&lt;/span&gt; connector plugs, excluding &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C plugs&lt;br /&gt;Image via &lt;a href="https://en.wikipedia.org/wiki/USB_hardware"&gt;Wikipedia&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;As you can see, this makes for a lot of confusion, especially when compatibility is mixed: &lt;span class="caps"&gt;USB2&lt;/span&gt; ‘A’ connectors are meant to go into &lt;span class="caps"&gt;USB3&lt;/span&gt; ‘A’ receptacles, but &lt;span class="caps"&gt;USB2&lt;/span&gt; ‘B’ connectors aren’t meant to go into &lt;span class="caps"&gt;USB3&lt;/span&gt; ‘B’ receptacles&amp;nbsp;…!&lt;/p&gt;
&lt;p&gt;So &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C is meant to be the one connector standard to rule them all. It even has rotational symmetry, so it shouldn’t matter which way you plug it&amp;nbsp;in!&lt;/p&gt;
&lt;h2&gt;So what is a &lt;span class="caps"&gt;USB&lt;/span&gt;&amp;nbsp;device?&lt;/h2&gt;
&lt;p&gt;Technically speaking, it is a device whose manufacturer has paid for a &lt;span class="caps"&gt;USB&lt;/span&gt; license, sent their device for certification, and passed the &lt;span class="caps"&gt;USB&lt;/span&gt;-&lt;span class="caps"&gt;IF&lt;/span&gt;’s certification requirements, allowing the manufacturer to put the &lt;span class="caps"&gt;USB&lt;/span&gt; logo on the&amp;nbsp;packaging.&lt;/p&gt;
&lt;p&gt;Practically speaking, it is any device that has a &lt;span class="caps"&gt;USB&lt;/span&gt; port, allows &lt;span class="caps"&gt;USB&lt;/span&gt; devices to be connected to it, or allows itself to be connected to other &lt;span class="caps"&gt;USB&lt;/span&gt; devices, and basically behaves like a &lt;span class="caps"&gt;USB&lt;/span&gt; device. (If it walks like a duck and talks like a duck&amp;nbsp;…)&lt;/p&gt;
&lt;p&gt;Furthermore, it is important to differentiate between a &lt;strong&gt;&lt;span class="caps"&gt;USB&lt;/span&gt; host&lt;/strong&gt; and a &lt;strong&gt;&lt;span class="caps"&gt;USB&lt;/span&gt; device&lt;/strong&gt;. The host acts as the controller of the device, and initiates all communication between the two. For instance, if you attempt to connect two Android phones to each other with a &lt;span class="caps"&gt;USB&lt;/span&gt; cable, one must act as the host and the other as a device, even if both are capable of acting as hosts. The host decides what can be done through the&amp;nbsp;connection.&lt;/p&gt;
&lt;p&gt;This helps to explain why a &lt;span class="caps"&gt;USB&lt;/span&gt;-charging battery pack cannot also be an external storage device &lt;em&gt;at the same time&lt;/em&gt;, i.e. you cannot combine a phone battery pack with an external hard disk and hope to charge your phone + access the external hard disk at the same time. When your phone charges from the battery pack, it acts as the &lt;span class="caps"&gt;USB&lt;/span&gt; device (in charging mode); when it accesses a hard disk, it is acting as the &lt;span class="caps"&gt;USB&lt;/span&gt; host. It cannot do both&amp;nbsp;simultaneously!&lt;/p&gt;
&lt;h2&gt;Data supported over &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C&amp;nbsp;cables&lt;/h2&gt;
&lt;p&gt;In addition to &lt;span class="caps"&gt;USB3&lt;/span&gt;.0 data (and later versions of &lt;span class="caps"&gt;USB&lt;/span&gt; 3), the &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C specification also allows the Type-C connector to carry other kinds of data, if supported by the&amp;nbsp;device:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DisplayPort video data for monitors and computer display devices (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue124.html"&gt;Issue 124&lt;/a&gt;))&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;HDMI&lt;/span&gt; video data for monitors and consumer electronics devices (also covered in Issue&amp;nbsp;124)&lt;/li&gt;
&lt;li&gt;Thunderbolt 3 data, for high-data-transfer&amp;nbsp;devices&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The Type-C specification even supports modes that allow a Type-C cable to carry multiple types of data simultaneously. For instance, a Type-C connector that connects a laptop to a monitor&amp;nbsp;can:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;carry DisplayPort video data, enabling the laptop to use the monitor as an external&amp;nbsp;display,&lt;/li&gt;
&lt;li&gt;carry &lt;span class="caps"&gt;USB&lt;/span&gt; 3 data, enabling the laptop to use &lt;span class="caps"&gt;USB&lt;/span&gt; ports and other features (such as audio inputs/outputs) on the&amp;nbsp;monitor&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;… All this comes with the caveat that one must read the manufacturers’ fine print to see if these features are supported on the respective devices. Just because the &lt;span class="caps"&gt;USB&lt;/span&gt; spec &lt;em&gt;allows&lt;/em&gt; it, doesn’t mean that a particular device &lt;em&gt;implements&lt;/em&gt;&amp;nbsp;it!&lt;/p&gt;
&lt;p&gt;For example, some laptops may have two Type-C ports, but &lt;em&gt;only one&lt;/em&gt; of those ports will support Thunderbolt and DisplayPort; the other port sometimes only supports &lt;span class="caps"&gt;USB3&lt;/span&gt;&amp;nbsp;data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; &lt;span class="caps"&gt;USB&lt;/span&gt; is a (licensed) technical standard that describes how devices connect to each other through a cable. &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C is a new connector standard that supports &lt;span class="caps"&gt;USB&lt;/span&gt; 3, DisplayPort, &lt;span class="caps"&gt;HDMI&lt;/span&gt;, and Thunderbolt. It is able to carry multiple types of data simultaneously, in limited combinations. In a &lt;span class="caps"&gt;USB&lt;/span&gt; connection, one device acts as the host while the other acts as the device; the host initiates all&amp;nbsp;communication.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 127: &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C Power&amp;nbsp;Delivery&lt;/p&gt;
&lt;p&gt;This issue was about how data is handled over &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C. Next issue, how &lt;em&gt;power&lt;/em&gt; is handled over &lt;span class="caps"&gt;USB&lt;/span&gt; Type-C. After all, every day, millions of devices are getting powered over Type-C: from smartphones, to Internet-of-Things (IoT) devices, to full-size laptops. How is a single type of cable able to cover such a wide range, when earlier cable types could&amp;nbsp;not?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 10"></category></entry><entry><title>Issue 125: Analog and digital conversion</title><link href="https://ngjunsiang.github.io/laymansguide/issue125.html" rel="alternate"></link><published>2021-06-19T08:00:00+08:00</published><updated>2021-06-19T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-06-19:/laymansguide/issue125.html</id><summary type="html">&lt;p&gt;Analog formats such as &lt;span class="caps"&gt;VGA&lt;/span&gt; mostly contain the control signals that the &lt;span class="caps"&gt;CRT&lt;/span&gt; needs to operate, while digital formats such as &lt;span class="caps"&gt;HDMI&lt;/span&gt; and DisplayPort contain image data that the device must convert to control signals. Analog signals need a digital-analog-conversion (&lt;span class="caps"&gt;DAC&lt;/span&gt;) chip to be converted to digital signals, hence &lt;span class="caps"&gt;VGA&lt;/span&gt;-&lt;span class="caps"&gt;HDMI&lt;/span&gt; adapters tend to be more costly than DisplayPort-&lt;span class="caps"&gt;HDMI&lt;/span&gt; adapters. Dedicated graphics cards generally support more simultaneous output video streams than integrated graphics&amp;nbsp;cards.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; The &lt;span class="caps"&gt;VGA&lt;/span&gt; video format originated in the time of cathode-ray televisions (CRTs). It was superseded by &lt;span class="caps"&gt;HDMI&lt;/span&gt;, a video format standardised by consumer electronics companies. DisplayPort, on the other hand, is a video format standardised by computer display&amp;nbsp;companies.&lt;/p&gt;
&lt;p&gt;The bulk of the story has been written in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue123.html"&gt;Issue 123&lt;/a&gt;), so this issue will be&amp;nbsp;short.&lt;/p&gt;
&lt;h2&gt;Why two digital formats? &lt;span class="caps"&gt;HDMI&lt;/span&gt; vs&amp;nbsp;DisplayPort&lt;/h2&gt;
&lt;p&gt;&lt;span class="caps"&gt;HDMI&lt;/span&gt; is a consumer electronics standard, and is thus heavily focused on broadcast and home video needs. &lt;span class="caps"&gt;HDMI&lt;/span&gt; primarily supports video and audio data. It also carries some control signals through &lt;span class="caps"&gt;CEC&lt;/span&gt; (for Consumer Electronics Control) capability, enabling a video game console or set-top box to send remote-control commands to a television set via the &lt;span class="caps"&gt;HDMI&lt;/span&gt;&amp;nbsp;connection.&lt;/p&gt;
&lt;p&gt;DisplayPort is a computer display standard, focused on computing needs. DisplayPort supports video data, optionally audio data, and additional data (such as &lt;span class="caps"&gt;USB&lt;/span&gt;). Since 2014, compatible devices can also transmit DisplayPort signal format over &lt;span class="caps"&gt;USB&lt;/span&gt;-C, provided both the transmitting and receiving devices support&amp;nbsp;it.&lt;/p&gt;
&lt;h2&gt;Analog vs digital formats: a&amp;nbsp;recap&lt;/h2&gt;
&lt;p&gt;Digital formats differ from analog formats, because they do not carry the raw signal for the device. Instead, they carry information about the image, encoding the image data (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue043.html"&gt;Issue 43&lt;/a&gt;)) into video form; after all, video is just a series of moving images! The device takes on the responsibility of figuring out how to make the images appear on-screen, which is why digital TVs require significantly more electronics than &lt;span class="caps"&gt;CRT&lt;/span&gt;&amp;nbsp;TVs.&lt;/p&gt;
&lt;h2&gt;Analog to digital&amp;nbsp;conversion&lt;/h2&gt;
&lt;p&gt;An analog signal does not easily convert to a digital signal! Analog-to-digital converters, such as the &lt;span class="caps"&gt;VGA&lt;/span&gt;-&lt;span class="caps"&gt;HDMI&lt;/span&gt; adapters that seem to be needed universally, have to figure out how to process a wave-like (analog) signal, and convert it into the pixel data that constitute an image. These adapters need a digital-analog conversion (&lt;span class="caps"&gt;DAC&lt;/span&gt;) chip to carry out that&amp;nbsp;conversion.&lt;/p&gt;
&lt;p&gt;In contrast with analog signals, digital signals usually carry uncompressed video data. Digital-to-digital converters thus do not need to carry out any conversion—it’s the same image! Most of these converters merely need pins to be mapped to each other, which makes them cheaper (e.g. DisplayPort-&lt;span class="caps"&gt;HDMI&lt;/span&gt;&amp;nbsp;converters).&lt;/p&gt;
&lt;p&gt;A chip that is able to handle multiple formats and produce a robust output is costly, and it is expected that a good adapter will cost quite a bit. That is no guarantee, however, that a costly adapter will always be a good&amp;nbsp;adapter.&lt;/p&gt;
&lt;h2&gt;Annddddd &amp;#8230; back to graphics&amp;nbsp;cards&lt;/h2&gt;
&lt;p&gt;The graphics card is in charge of converting the final rasterised signal (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue122.html"&gt;Issue 122&lt;/a&gt;)) to a video signal, depending on the video format that is required. Naturally, this requires additional chips. Most integrated graphics chips support &lt;span class="caps"&gt;VGA&lt;/span&gt; and &lt;span class="caps"&gt;HDMI&lt;/span&gt;, while DisplayPort support is usually reserved for higher-end&amp;nbsp;devices.&lt;/p&gt;
&lt;p&gt;Higher-end graphics cards offer support for more video formats. Furthermore, they also have the capability to rasterise and output video streams for multiple screens, enabling multi-screen support for those who need it. If you find that you need more than two screens for work or play, you are likely going to need a dedicated video card that supports three or more simultaneous video output&amp;nbsp;ports.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Analog formats such as &lt;span class="caps"&gt;VGA&lt;/span&gt; mostly contain the control signals that the &lt;span class="caps"&gt;CRT&lt;/span&gt; needs to operate, while digital formats such as &lt;span class="caps"&gt;HDMI&lt;/span&gt; and DisplayPort contain image data that the device must convert to control signals. Analog signals need a digital-analog-conversion (&lt;span class="caps"&gt;DAC&lt;/span&gt;) chip to be converted to digital signals, hence &lt;span class="caps"&gt;VGA&lt;/span&gt;-&lt;span class="caps"&gt;HDMI&lt;/span&gt; adapters tend to be more costly than DisplayPort-&lt;span class="caps"&gt;HDMI&lt;/span&gt; adapters. Dedicated graphics cards generally support more simultaneous output video streams than integrated graphics&amp;nbsp;cards.&lt;/p&gt;
&lt;p&gt;I hope this sufficiently explains a question I hear so often: why do &lt;span class="caps"&gt;VGA&lt;/span&gt;-&lt;span class="caps"&gt;HDMI&lt;/span&gt; adapters cost so much? I’m also glad this issue ended up much shorter than I&amp;nbsp;expected.&lt;/p&gt;
&lt;p&gt;In general, if your adapter/cable needs a chip to carry out signal conversion, it’s going to cost more than a plain&amp;nbsp;cable.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 126: &lt;span class="caps"&gt;USB&lt;/span&gt;&amp;nbsp;Type-C&lt;/p&gt;
&lt;p&gt;Next issue, let’s zap some common questions about the latest &lt;span class="caps"&gt;USB&lt;/span&gt; standard! This is going to stretch over two issues. First up: &lt;span class="caps"&gt;USB&lt;/span&gt;-C for&amp;nbsp;data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 10"></category></entry><entry><title>Issue 124: Video formats</title><link href="https://ngjunsiang.github.io/laymansguide/issue124.html" rel="alternate"></link><published>2021-06-12T08:00:00+08:00</published><updated>2021-06-12T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-06-12:/laymansguide/issue124.html</id><summary type="html">&lt;p&gt;The &lt;span class="caps"&gt;VGA&lt;/span&gt; video format originated in the time of cathode-ray televisions (CRTs). It was superseded by &lt;span class="caps"&gt;HDMI&lt;/span&gt;, a video format standardised by consumer electronics companies. DisplayPort, on the other hand, is a video format standardised by computer display&amp;nbsp;companies.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Graphics cards contain lots of tiny cores that are much better at performing the same calculation for lots of decimal numbers. These cores are organised into compute units; a graphics card with more compute units can perform more calculations every second. Graphics cards have their own onboard memory, separate from the &lt;span class="caps"&gt;CPU&lt;/span&gt;. &lt;span class="caps"&gt;GPU&lt;/span&gt; memory is different from computer memory; it is configured for much higher data throughput. Integrated graphics are GPUs that are integrated into a &lt;span class="caps"&gt;CPU&lt;/span&gt; chip; these do not have their own onboard memory, and share memory with the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Ah, the esoteric, tricky, complicated art of shooting electromagnetic radiation into the eyes of humans … entire tomes have been written about this. And I will attempt to summarise the pertinent parts into a single newsletter issue. The&amp;nbsp;hubris!&lt;/p&gt;
&lt;p&gt;It’s really something when you suddenly remember that television has been around since the 1930s, while computers in some recognisable form were a 1970s invention. The first part of the computer to be invented was the screen!&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;How did screens work if computers weren&amp;#8217;t invented? A crash&amp;nbsp;course:&lt;/p&gt;
&lt;h2&gt;Cathode-ray tube (&lt;span class="caps"&gt;CRT&lt;/span&gt;)&amp;nbsp;televisions&lt;/h2&gt;
&lt;p&gt;Early colour television screens had primary-colour (red, blue, and green) phosphor dots embedded in the user-facing portion of the screen. These dots emit coloured light when struck by electrons. At the back of the television, cathodes made of barium oxide are heated, causing them to emit&amp;nbsp;electrons.&lt;/p&gt;
&lt;p&gt;These electrons, when emitted, fly in all directions, but they are shaped into a beam by an electric field (hence the name “cathode &lt;em&gt;ray&lt;/em&gt; tube”, which you might have seen in the form of the acronym &lt;strong&gt;&lt;span class="caps"&gt;CRT&lt;/span&gt;&lt;/strong&gt;).&lt;/p&gt;
&lt;p&gt;&lt;img alt="Cutaway of a CRT, showing the phosphor dot screen" src="https://ngjunsiang.github.io/laymansguide/issue124_01.png" /&gt;&lt;br /&gt;
&lt;small&gt;1: Barium oxide cathode, which is heated to emit electrons (labelled “2”), which are shaped into a a beam by an electric field (labelled “3”)&lt;br /&gt;
4: Deflecting coils, discussed in the next paragraph&lt;br /&gt;
8: Coloured phosphor dots, arranged on a flat screen (labelled “7”).&lt;br /&gt;
Image via &lt;a href="https://en.wikipedia.org/wiki/Cathode-ray_tube"&gt;Wikipedia&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;This electron beam could be aimed at any of the phosphor dots by a set of electromagnetic deflecting coils mounted along the sides of the &lt;span class="caps"&gt;TV&lt;/span&gt;, on the inside surface. One set, oriented vertically (mounted left-right), controlled the horizontal deflection of the electron beam, while another set, oriented horizontally (mounted top-bottom), controlled the vertical&amp;nbsp;deflection.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Graphic showing an electron beam deflected upwards by deflecting coils in a CRT" src="https://ngjunsiang.github.io/laymansguide/issue124_02.png" /&gt;&lt;br /&gt;
&lt;small&gt;The electron beam produced by the cathode is deflected by (electromagnetic) deflecting coils.&lt;br /&gt;
Image via &lt;a href="https://en.wikipedia.org/wiki/Cathode-ray_tube"&gt;Wikipedia&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;To produce an image, the electron beam is manipulated to scan across the screen, one line at a time. Each pass across the screen causes it to strike phosphor dots, emitting light in a line. A variety of techniques (microdeflections, masks, and filters) are used to ensure the correct dots are&amp;nbsp;struck.&lt;/p&gt;
&lt;p&gt;Each line of the screen is laboriously scanned with this technique, about 60 times a second. This means that the screen “updates” with a refresh rate of 60&amp;nbsp;Hz.&lt;/p&gt;
&lt;p&gt;To make this happen, a varying voltage is applied across the two sets of deflecting coils. The required pattern for the deflecting coils has to come from the television signal source; the television signal from the broadcasting station, therefore, closely resembles the pattern required by the deflecting coils. The television itself applies almost no processing to this signal! (Remember that the chips used to do this kind of processing had not been invented&amp;nbsp;yet.)&lt;/p&gt;
&lt;p&gt;These are what we call &lt;strong&gt;analog&lt;/strong&gt; signals. Phonographs and early telephones also used analog&amp;nbsp;signals.&lt;/p&gt;
&lt;h2&gt;Video Graphics Array (&lt;span class="caps"&gt;VGA&lt;/span&gt;)&lt;/h2&gt;
&lt;p&gt;So when the computer was first invented, and these screens were widely available, there was no need to reinvent the screen. Graphics cards (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue123.html"&gt;Issue 123&lt;/a&gt;)) simply had to figure out how to emit analog signals that would work with &lt;span class="caps"&gt;CRT&lt;/span&gt;&amp;nbsp;screens.&lt;/p&gt;
&lt;p&gt;The graphics standard for doing so is called &lt;strong&gt;&lt;span class="caps"&gt;VGA&lt;/span&gt;&lt;/strong&gt; (Video Graphics Array), and was first released by &lt;span class="caps"&gt;IBM&lt;/span&gt; in 1986. An organisation, the Video Electronics Standards Association (&lt;strong&gt;&lt;span class="caps"&gt;VESA&lt;/span&gt;&lt;/strong&gt;), was quickly formed in 1989, spearheaded by Nippon Electric Company (&lt;span class="caps"&gt;NEC&lt;/span&gt;), to extend this standard and allow it to support higher resolutions (up to&amp;nbsp;1080p!).&lt;/p&gt;
&lt;h2&gt;LCDs replaced&amp;nbsp;CRTs&lt;/h2&gt;
&lt;p&gt;As CRTs grow larger, they ran into a few problems. CRTs were big, bulky, and heavy. The larger you made them, the longer you had to make the cathode ray tube, which made them immensely&amp;nbsp;heavy!&lt;/p&gt;
&lt;p&gt;By this point, &lt;span class="caps"&gt;LCD&lt;/span&gt; technology had been developed. Instead of using a scanning electron beam, it consisted of a backlight&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt; behind a liquid crystal layer (hence the term liquid crystal display, &lt;strong&gt;&lt;span class="caps"&gt;LCD&lt;/span&gt;&lt;/strong&gt;).&lt;/p&gt;
&lt;p&gt;The liquid crystal layer consisted of pixels of each primary colour. Each pixel had an adjustable transparency, which depended on the voltage applied across it (high voltage = transparent, low voltage = opaque); a cluster of red, green, and blue pixels formed a single image pixel on screen. By applying different voltages across each primary-colour pixel, we can put an image&amp;nbsp;together.&lt;/p&gt;
&lt;h2&gt;Digital&amp;nbsp;signals&lt;/h2&gt;
&lt;p&gt;CRTs controlled the voltage plates directly to deflect an electron beam, through an analog signal. But LCDs use an internal processor to determine what voltage to apply across each liquid crystal pixel. As the technology improved, lower voltages could be used to reduce power usage. So LCDs need a different kind of signal: a &lt;strong&gt;digital&lt;/strong&gt; one, consisting of the &lt;em&gt;raw image data&lt;/em&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue043.html"&gt;Issue 43&lt;/a&gt;)).&lt;/p&gt;
&lt;h2&gt;The exodus to digital&amp;nbsp;formats&lt;/h2&gt;
&lt;p&gt;As digital television became more feasible due to decreasing microprocessor and &lt;span class="caps"&gt;LCD&lt;/span&gt; screen costs, digital formats sprung up to replace &lt;span class="caps"&gt;VGA&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;An early competitor, Digital Visual Interface (&lt;strong&gt;&lt;span class="caps"&gt;DVI&lt;/span&gt;&lt;/strong&gt;), was launched by a working group convened by some computer makers (Intel, Silicon Image, Compaq, Fujitsu, &lt;span class="caps"&gt;HP&lt;/span&gt;, &lt;span class="caps"&gt;IBM&lt;/span&gt;, &lt;span class="caps"&gt;NEC&lt;/span&gt;). It was very quickly superceded by High-Definition Multimedia Interface (&lt;strong&gt;&lt;span class="caps"&gt;HDMI&lt;/span&gt;&lt;/strong&gt;), an interface which implemented standards set by consumer electronics companies (Hitachi, Sanyo, Silicon Image, Sony, Technicolor,&amp;nbsp;Toshiba).&lt;/p&gt;
&lt;p&gt;&lt;img alt="HDMI and mini-HDMI connectors displayed top-to-bottom" src="https://ngjunsiang.github.io/laymansguide/issue124_03.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;&lt;span class="caps"&gt;HDMI&lt;/span&gt; and mini-&lt;span class="caps"&gt;HDMI&lt;/span&gt; connectors displayed from top to bottom. Both support the &lt;span class="caps"&gt;HDMI&lt;/span&gt; video standard&lt;br /&gt;Image via &lt;a href="https://www.datapro.net/techinfo/hdmi_info.html"&gt;DataPro&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;This was followed by the DisplayPort (&lt;strong&gt;&lt;span class="caps"&gt;DP&lt;/span&gt;&lt;/strong&gt;) standard, developed by &lt;span class="caps"&gt;PC&lt;/span&gt; and chip manufacturers and standardised by &lt;span class="caps"&gt;VESA&lt;/span&gt;, and released in in&amp;nbsp;2006.&lt;/p&gt;
&lt;p&gt;&lt;img alt="DisplayPort, mini-DisplayPort, and Thunderbolt Type-C connectors displayed side-by-side" src="https://ngjunsiang.github.io/laymansguide/issue124_04.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;DisplayPort, mini-DisplayPort, and Thunderbolt Type-C connectors displayed from left to right. All three support the DisplayPort video standard&lt;br /&gt;Image via &lt;a href="https://www.datapro.net/techinfo/displayport_info.html"&gt;DataPro&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; The &lt;span class="caps"&gt;VGA&lt;/span&gt; video format originated in the time of cathode-ray televisions (CRTs). It was superseded by &lt;span class="caps"&gt;HDMI&lt;/span&gt;, a video format standardised by consumer electronics companies. DisplayPort, on the other hand, is a video format standardised by computer display&amp;nbsp;companies.&lt;/p&gt;
&lt;p&gt;Phew. This issue is much longer than I would like; there is so much history to these things! The &lt;span class="caps"&gt;HDMI&lt;/span&gt;-vs-DisplayPort question/complaint I keep hearing is one that only made sense for me in the context of the respective industries they sprung from, and this is something I think most layfolks could definitely&amp;nbsp;understand.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 125: Analog and digital&amp;nbsp;conversion&lt;/p&gt;
&lt;p&gt;Now that I’ve laid out the key differences between &lt;span class="caps"&gt;VGA&lt;/span&gt;, &lt;span class="caps"&gt;HDMI&lt;/span&gt;, and DisplayPort, we can talk about &amp;#8230; video&amp;nbsp;adapters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;To be pedantic, the first part of the computer to be invented was actually the keyboard, but they were non-electronic and were called typewriters then.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;Older LCDs used cold-cathode fluorescent lamps (CCFLs) for backlights, but today LEDs are used instead.&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 10"></category></entry><entry><title>Issue 123: Graphics cards: The Pixel Factory</title><link href="https://ngjunsiang.github.io/laymansguide/issue123.html" rel="alternate"></link><published>2021-06-05T08:00:00+08:00</published><updated>2021-06-05T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-06-05:/laymansguide/issue123.html</id><summary type="html">&lt;p&gt;Graphics cards contain lots of tiny cores that are much better at performing the same calculation for lots of decimal numbers. These cores are organised into compute units; a graphics card with more compute units can perform more calculations every second. Graphics cards have their own onboard memory, separate from the &lt;span class="caps"&gt;CPU&lt;/span&gt;. &lt;span class="caps"&gt;GPU&lt;/span&gt; memory is different from computer memory; it is configured for much higher data throughput. Integrated graphics are GPUs that are integrated into a &lt;span class="caps"&gt;CPU&lt;/span&gt; chip; these do not have their own onboard memory, and share memory with the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Computers are general-purpose machines that usually process integer calculations. The graphics pipeline requires more specialised hardware that can process decimal number calculations. This is why high-performance graphics usually requires a graphics&amp;nbsp;card.&lt;/p&gt;
&lt;p&gt;So why are gamers so agog over &lt;strong&gt;graphics cards&lt;/strong&gt; (also known as video cards)? That’s because they do one thing really well! Unlike CPUs which often have to process an unpredictable workload, the graphics pipeline involves performing the same categories of calculations over and over&amp;nbsp;again.&lt;/p&gt;
&lt;h2&gt;Graphics compute&amp;nbsp;units&lt;/h2&gt;
&lt;p&gt;These calculations, which I gave an overview of in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue122.html"&gt;Issue 122&lt;/a&gt;), take in tables of numbers, crunch them mathematically, and spit out another table of numbers. Since the calculations are predictable, we don&amp;#8217;t need very complicated hardware that enables switching instructions based on the input. We can used specialised cores—clusters of transistors that are custom-fit for the purpose, cram lots of them into a circuit board, and end up with much better performance for the graphics pipeline compared to the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;These cores are organised into groups known as &lt;strong&gt;compute units&lt;/strong&gt;, and graphics card companies often differentiate the lower-end and higher-end cards by the number of compute units they have, which indicate the computation rate (measured in &lt;strong&gt;flops&lt;/strong&gt;, which stand for &lt;strong&gt;fl&lt;/strong&gt;oating-point&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt; &lt;strong&gt;o&lt;/strong&gt;perations &lt;strong&gt;p&lt;/strong&gt;er &lt;strong&gt;s&lt;/strong&gt;econd). Better graphics require more computation, so more flops correlate with better&amp;nbsp;graphics.&lt;/p&gt;
&lt;h2&gt;Graphics&amp;nbsp;memory&lt;/h2&gt;
&lt;p&gt;Graphics cards often come with their own memory chips, based on a different technology (&lt;strong&gt;&lt;span class="caps"&gt;GDDR&lt;/span&gt;&lt;/strong&gt;, standing for “graphics double data rate”, vs &lt;strong&gt;&lt;span class="caps"&gt;DDR&lt;/span&gt;&lt;/strong&gt; for CPUs). Graphics memory chips (&lt;span class="caps"&gt;GDDR&lt;/span&gt;) are optimised for higher bandwidth (more gigabytes transferred per second), while &lt;span class="caps"&gt;CPU&lt;/span&gt; memory chips (&lt;span class="caps"&gt;DDR&lt;/span&gt;) are optimised for lower latency (lower time to response). These are soldered directly onto the graphics card to keep memory readily accessible by the compute&amp;nbsp;units.&lt;/p&gt;
&lt;h2&gt;Power and&amp;nbsp;heat&lt;/h2&gt;
&lt;p&gt;Unlike CPUs, which (for desktops) seldom draw more than 100W by themselves, graphics cards (for desktops) can draw up to 300W. Correspondingly, more of the space on graphics cards are taken up by components that try to keep this immense power under control. Voltage regulation modules help to adjust input voltages to what the compute units and memory&amp;nbsp;requires.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Graphics card without its shroud and cooler" src="https://ngjunsiang.github.io/laymansguide/issue123_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;A graphics card, here exposed without its shroud and cooler.&lt;br /&gt;The graphics chip is in the middle, surrounded by graphics memory chips (smaller, black squares) close to the edges of the board. The larger protruding gray rectangles are voltage regulation modules.&lt;br /&gt;Image from &lt;a href="https://www.gamersnexus.net/guides/2826-differences-between-ddr4-and-gddr5"&gt;Gamersnexus&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;h2&gt;Other&amp;nbsp;uses&lt;/h2&gt;
&lt;p&gt;The graphics card excels at carrying out predictable decimal calculations in a pipeline. Besides graphics, what else can it be used&amp;nbsp;for?&lt;/p&gt;
&lt;p&gt;In research, they have been purposed to perform calculations for simulations, which often involve processing the same calculation in&amp;nbsp;bulk.&lt;/p&gt;
&lt;p&gt;For consumer purposes, they have also been used for video decoding (decompressing videos for playback), and lately even video encoding (compressing videos to a smaller size). These also involve performing the same types of calculations in a predictable&amp;nbsp;pipeline.&lt;/p&gt;
&lt;p&gt;Most recently, they are being used for machine learning (also known as “artificial intelligence”) models, again because those involve predictable pipelines (Do you are see a pattern&amp;nbsp;here?)&lt;/p&gt;
&lt;h2&gt;Integrated&amp;nbsp;graphics&lt;/h2&gt;
&lt;p&gt;Not all computers need a full-size &lt;span class="caps"&gt;GPU&lt;/span&gt; to render graphics on screen. Intel processors, and some of the newer &lt;span class="caps"&gt;AMD&lt;/span&gt; ones, contain what is known as &lt;strong&gt;integrated graphics&lt;/strong&gt;. That means that these CPUs have a graphics processor unit (&lt;span class="caps"&gt;GPU&lt;/span&gt;) integrated into the same chip. This integrated &lt;span class="caps"&gt;GPU&lt;/span&gt; provides basic capabilities which allow typical users to use a computer, and even run low-end graphics programs, without needing to buy a higher-end graphics&amp;nbsp;card.&lt;/p&gt;
&lt;p&gt;Integrated GPUs do not have their own memory. They share computer memory with the &lt;span class="caps"&gt;CPU&lt;/span&gt;. That means that they reserve a (configurable) amount of computer memory for graphics use (typically up to 128 &lt;span class="caps"&gt;MB&lt;/span&gt;), and video card drivers (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue120.html"&gt;Issue 120&lt;/a&gt;)) enable the &lt;span class="caps"&gt;GPU&lt;/span&gt; to use more system memory (&lt;a href="https://www.intel.sg/content/www/xa/en/support/articles/000020962/graphics.html"&gt;up to 50% for Intel integrated graphics&lt;/a&gt;). This means that integrated graphics use slower memory compared to dedicated graphics cards that have their own&amp;nbsp;memory.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Graphics cards contain lots of tiny cores that are much better at performing the same calculation for lots of decimal numbers. These cores are organised into compute units; a graphics card with more compute units can perform more calculations every second. Graphics cards have their own onboard memory, separate from the &lt;span class="caps"&gt;CPU&lt;/span&gt;. &lt;span class="caps"&gt;GPU&lt;/span&gt; memory is different from computer memory; it is configured for much higher data throughput. Integrated graphics are GPUs that are integrated into a &lt;span class="caps"&gt;CPU&lt;/span&gt; chip; these do not have their own onboard memory, and share memory with the &lt;span class="caps"&gt;CPU&lt;/span&gt;.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 124: Video&amp;nbsp;formats&lt;/p&gt;
&lt;p&gt;I haven’t talked about the last part, because this issue is long enough already, and because the next part can fill a whole issue by itself. Next up, the last stage: actually displaying pixels on a&amp;nbsp;screen.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;&lt;del&gt;a video card? [Issue 113]&lt;/del&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;“Floating-point” is a fancy term for decimal, so-called because the way they are represented allows the decimal point to be placed differently, unlike with integers.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 10"></category><category term="memory"></category></entry><entry><title>Issue 122: The great flattening</title><link href="https://ngjunsiang.github.io/laymansguide/issue122.html" rel="alternate"></link><published>2021-05-29T08:00:00+08:00</published><updated>2021-05-29T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-05-29:/laymansguide/issue122.html</id><summary type="html">&lt;p&gt;Computers are general-purpose machines that usually process integer calculations. The graphics pipeline requires more specialised hardware that can process decimal number calculations. This is why high-performance graphics usually requires a graphics&amp;nbsp;card.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; 3D models are represented with vertices (points), edges (line segments between points), and faces in a computer. Images known as textures can be mapped to faces to give the impression of&amp;nbsp;detail.&lt;/p&gt;
&lt;p&gt;Having a model represented in a computer as a large set of numbers is cool, but nobody does 3D modelling like that. We need something to look at! We need a way to convert our model into a flat picture, ideally displayed on our monitors. And this conversion process needs to be fast enough that as we rotate or change the view of our model, the computer can keep up, displaying the changes in&amp;nbsp;real-time.&lt;/p&gt;
&lt;p&gt;This process is called &lt;strong&gt;rendering&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;A pipeline for rendering&amp;nbsp;pictures&lt;/h2&gt;
&lt;p&gt;During rendering, data from the modelling program is processed in a series of steps; these steps together are known as the &lt;strong&gt;graphics pipeline&lt;/strong&gt;. If you’ve done perspective drawing or tried to figure out how to create clever camera tricks, you already have a sense of what the computer needs to do&amp;nbsp;here.&lt;/p&gt;
&lt;p&gt;The 3D cube on screen looks different depending on where our eye is, and which direction it is looking. Our distance to the cube affects how much distortion the view undergoes. Lighting also affects how the cube appears, by making shaded areas appear darker, and lit areas appear&amp;nbsp;brighter.&lt;/p&gt;
&lt;p&gt;Finally, this 3D model needs to be “distorted” into a 2D view so it can be displayed on a screen. (We don’t have 3D holo-projectors yet …&amp;nbsp;😢)&lt;/p&gt;
&lt;p&gt;Lastly, since our screens display images as a grid of pixels, we need to figure out the best way to convert the distorted 2D view into a pixel grid. This part of the pipeline is known as &lt;strong&gt;rasterisation&lt;/strong&gt;. Here, the computer figures out what colour each pixel should be, based on which part of the model actually gets projected here. Hidden parts do not need to be rasterised, and neither do parts of the model which are outside the&amp;nbsp;screen.&lt;/p&gt;
&lt;p&gt;All these steps take place in the graphics&amp;nbsp;pipeline.&lt;/p&gt;
&lt;h2&gt;The&amp;nbsp;hardware&lt;/h2&gt;
&lt;p&gt;The pipeline used to be carried out by the &lt;span class="caps"&gt;CPU&lt;/span&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue053.html"&gt;Issue 53&lt;/a&gt;)), but that isn’t ideal. The &lt;span class="caps"&gt;CPU&lt;/span&gt;’s hardware is optimised for &lt;em&gt;general-purpose processing&lt;/em&gt;: keeping track of integers (i.e. natural numbers like 1, 2, 3, …), adding or subtracting them, and resetting them. It has many more computational units that carry out this&amp;nbsp;calculation.&lt;/p&gt;
&lt;p&gt;Graphics processing, on the other hand, requires a different kind of calculation. The position of vertices do not fit nicely into integers; we have to carry this out using decimal numbers (1.46776, 2.58704, –3.57514, …). The &lt;span class="caps"&gt;CPU&lt;/span&gt; does not encounter these often, and therefore does not have many of these computational&amp;nbsp;units.&lt;/p&gt;
&lt;p&gt;For graphics, we need a different kind of processor, one that is jam-packed with decimal computational units. We need a &lt;strong&gt;graphics card&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Computers are general-purpose machines that usually process integer calculations. The graphics pipeline requires more specialised hardware that can process decimal number calculations. This is why high-performance graphics usually requires a graphics&amp;nbsp;card.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 123: Graphics cards: The Pixel&amp;nbsp;Factory&lt;/p&gt;
&lt;p&gt;So what does a graphics card do, just pop out pixels like nobody’s&amp;nbsp;business?&lt;/p&gt;
&lt;p&gt;Yep! Next week, a quick intro to graphics cards, and why they are so amazing. And, because this is recent news, some coverage on how the M1 differs from most laptops in the way it manages the &lt;span class="caps"&gt;CPU&lt;/span&gt; and &lt;span class="caps"&gt;GPU&lt;/span&gt; (graphics processing&amp;nbsp;unit).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a video card? [Issue&amp;nbsp;113]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 10"></category></entry><entry><title>Issue 121: In graphic detail</title><link href="https://ngjunsiang.github.io/laymansguide/issue121.html" rel="alternate"></link><published>2021-05-22T08:00:00+08:00</published><updated>2021-05-22T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-05-22:/laymansguide/issue121.html</id><summary type="html">&lt;p&gt;3D models are represented with vertices (points), edges (line segments between points), and faces in a computer. Images known as textures can be mapped to faces to give the impression of&amp;nbsp;detail.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Driver files provide information about the driver, and instructions on how to receive information from the device, and encode information to be passed to the device. The operating system may come with generic driver files for the device, but custom driver files might provide better performance or additional&amp;nbsp;features.&lt;/p&gt;
&lt;p&gt;This issue, let’s start from scratch with graphics: how does a machine that only processes 1s and 0s work with graphics? For starters, let’s think: what can we represent about graphics if we only have&amp;nbsp;numbers?&lt;/p&gt;
&lt;h2&gt;Representing graphics using&amp;nbsp;numbers&lt;/h2&gt;
&lt;p&gt;Numbers can be used to represent points (a &lt;strong&gt;vertex&lt;/strong&gt;, in graphics-speak). In 3D space, a point can be represented with 3 numbers, usually written in math&amp;nbsp;as &lt;code&gt;(x, y, z)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;A line (&lt;strong&gt;edge&lt;/strong&gt;, in graphics-speak) can be represented as a segment between two points. So we can represent straight-line segments using two points. Curved-line segments are trickier, but for now a lazy way to represent them is just &amp;#8230; using a series of points 😛 like connect-the-dots&amp;nbsp;puzzles!&lt;/p&gt;
&lt;p&gt;Surfaces are &amp;#8230; a bit trickier. What is the minimum number of points we need to represent a flat surface? Turns out the answer is 3 points: if we pick any 3 points that are not along the same line, we can join them along their edges and they form a triangle, which is a flat&amp;nbsp;surface!&lt;/p&gt;
&lt;p&gt;In graphics-speak, we call these surfaces &lt;strong&gt;faces&lt;/strong&gt;. Four points which form a flat four-pointed face are called quads, and we can do the same for polygons with more points too. But most graphics hardware just deals with triangles and quads, because everything else can be represented using triangles and quads&amp;nbsp;anyway.&lt;/p&gt;
&lt;h2&gt;Making 3D&amp;nbsp;models&lt;/h2&gt;
&lt;p&gt;3D modelling software (also referred to as computer-aided design software, or &lt;span class="caps"&gt;CAD&lt;/span&gt; software) helps us with the process of creating vertices (plural for vertex), edges, and faces. Any object we model digitally is just a collection of vertices, edges, and faces: we call such collections a &lt;strong&gt;mesh&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;Textures&lt;/h2&gt;
&lt;p&gt;With just meshes, we quickly run into the limits of what can be represented. For simplicity of calculation, each face can only have one colour. To make really detailed and realistic models, we need very finely detailed meshes. These are problematic because a lot of calculation is needed to make these models appear on screen; the more faces it has, the more calculation is&amp;nbsp;needed!&lt;/p&gt;
&lt;p&gt;One way to reduce the number of faces in the mesh while still creating a decent model is to &lt;em&gt;use images on the face&lt;/em&gt;. (I explained how images are represented in computers in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue043.html"&gt;Issue 43&lt;/a&gt;)) We will need additional information to describe the scale and rotation of the image on the face, but at least we can use the same image across multiple faces if necessary. Instead of having to model a hundred thousand blades of grass, I could just model a few stalks, and use a grassy texture to complete the&amp;nbsp;impression.&lt;/p&gt;
&lt;h2&gt;Scene&amp;nbsp;modelling&lt;/h2&gt;
&lt;p&gt;Just having models is pretty boring. We will usually be putting multiple models in a scene. Besides models, a scene needs to have lighting, a way to model the sky as the background, and other&amp;nbsp;niceties.&lt;/p&gt;
&lt;p&gt;We can use numbers to describe the position, luminosity (i.e. &lt;strong&gt;brightness&lt;/strong&gt;), hue (i.e. &lt;strong&gt;colour&lt;/strong&gt;), and other properties of lighting in our&amp;nbsp;scene.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; 3D models are represented with vertices (points), edges (line segments between points), and faces in a computer. Images known as textures can be mapped to faces to give the impression of&amp;nbsp;detail.&lt;/p&gt;
&lt;p&gt;Great, we have a way to use numbers to describe a 3D model; that’s nice progress. But how do we turn those numbers into a picture on&amp;nbsp;screen?&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 122: The great&amp;nbsp;flattening&lt;/p&gt;
&lt;p&gt;Numbers, numbers, and more numbers … isn’t the &lt;span class="caps"&gt;CPU&lt;/span&gt; great at numbers? Why do gamers and 3D modellers have the hots for graphics cards instead? Stay tuned next&amp;nbsp;week!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a video card? [Issue&amp;nbsp;113]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 10"></category></entry><entry><title>Issue 120: Drivers, the glue between hardware and firmware</title><link href="https://ngjunsiang.github.io/laymansguide/issue120.html" rel="alternate"></link><published>2021-05-15T08:00:00+08:00</published><updated>2021-05-15T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-05-15:/laymansguide/issue120.html</id><summary type="html">&lt;p&gt;Driver files provide information about the driver, and instructions on how to receive information from the device, and encode information to be passed to the device. The operating system may come with generic driver files for the device, but custom driver files might provide better performance or additional&amp;nbsp;features.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Solid-state disks are much faster than hard disks because they have no moving parts, so no time is wasted waiting for parts to get into the right position. However, they are more expensive than hard disk&amp;nbsp;drives.&lt;/p&gt;
&lt;p&gt;So this issue, we finally get to a question that most folks would have asked at some point after buying a new&amp;nbsp;device:&lt;/p&gt;
&lt;p&gt;“What is a (device) driver and why do I need&amp;nbsp;one?”&lt;/p&gt;
&lt;p&gt;This issue is going to focus on &lt;span class="caps"&gt;USB&lt;/span&gt; devices, since that is by far the largest category of devices that people use. But driver files are needed for all hardware, including disks, monitors, hardware timers, controller chips,&amp;nbsp;…&lt;/p&gt;
&lt;h2&gt;&lt;span class="caps"&gt;USB&lt;/span&gt;&amp;nbsp;Devices&lt;/h2&gt;
&lt;p&gt;The &lt;span class="caps"&gt;USB&lt;/span&gt; specification was first released in 1996, and today it contains 21 categories of devices, each containing even more&amp;nbsp;subcategories.&lt;/p&gt;
&lt;p&gt;Each category of device has its own protocol for sending information to the computer, and receiving information from the&amp;nbsp;computer.&lt;/p&gt;
&lt;p&gt;I don’t think I am over-exaggerating things to say that there are an uncountable number of &lt;span class="caps"&gt;USB&lt;/span&gt; devices in the world today. For this reason, it makes no sense to complain to the &lt;a href="https://en.wikipedia.org/wiki/USB_Implementers_Forum"&gt;&lt;span class="caps"&gt;USB&lt;/span&gt; Implementers’ Forum&lt;/a&gt; (&lt;span class="caps"&gt;USB&lt;/span&gt;-&lt;span class="caps"&gt;IF&lt;/span&gt;), which is responsible for designing the &lt;span class="caps"&gt;USB&lt;/span&gt; specification. Device manufacturers themselves have to be the one responsible for making their device work with our&amp;nbsp;computers.&lt;/p&gt;
&lt;p&gt;They do so by providing driver files with their devices. In a not-so-distant past, every device you bought came with drivers on a &lt;span class="caps"&gt;CD&lt;/span&gt;-&lt;span class="caps"&gt;ROM&lt;/span&gt; or &lt;span class="caps"&gt;DVD&lt;/span&gt;-&lt;span class="caps"&gt;ROM&lt;/span&gt;, which you had to install before use. Today, it is more likely that you’ll download these drivers from the manufacturer’s website. If this is a widely used device (e.g. monitors or input devices like keyboards or mice), it might even make its drivers (and updates to those drivers) available through Windows&amp;nbsp;Update.&lt;/p&gt;
&lt;h2&gt;What is a driver&amp;nbsp;file?&lt;/h2&gt;
&lt;p&gt;Simply put, a driver file tells the&amp;nbsp;computer:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;information about the device (its name, category, and available&amp;nbsp;features)&lt;/li&gt;
&lt;li&gt;how to interpret signals coming from the device (through the &lt;span class="caps"&gt;USB&lt;/span&gt;&amp;nbsp;cable),&lt;/li&gt;
&lt;li&gt;how to encode signals to be sent to the device so that it can understand&amp;nbsp;them.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;span class="caps"&gt;USB&lt;/span&gt;-&lt;span class="caps"&gt;IF&lt;/span&gt; maintains a database of vendors and their products. Companies that wish to have their products recognised should &lt;a href="https://www.usb.org/getting-vendor-id"&gt;get a vendor &lt;span class="caps"&gt;ID&lt;/span&gt;&lt;/a&gt; through the &lt;span class="caps"&gt;USB&lt;/span&gt;-&lt;span class="caps"&gt;IF&lt;/span&gt;; this also allows them to use the &lt;span class="caps"&gt;USB&lt;/span&gt;-&lt;span class="caps"&gt;IF&lt;/span&gt; logo (for &lt;span class="caps"&gt;USB&lt;/span&gt;-certified™) on their product packaging if the product passes&amp;nbsp;certification.&lt;/p&gt;
&lt;p&gt;When a device is inserted, it passes information including its vendor &lt;span class="caps"&gt;ID&lt;/span&gt; and product &lt;span class="caps"&gt;ID&lt;/span&gt; to the computer. Each driver file also includes the vendor &lt;span class="caps"&gt;ID&lt;/span&gt; and product &lt;span class="caps"&gt;ID&lt;/span&gt; it is meant for use with. This allows the computer to verify that the correct driver is installed for the&amp;nbsp;device.&lt;/p&gt;
&lt;p&gt;If you insert a device and Windows says it does not recognise it, you can be sure the problem has something to do with the device file (if the device is otherwise working&amp;nbsp;properly).&lt;/p&gt;
&lt;p&gt;On your computer, you can view a list of your devices and inspect their driver details using Device&amp;nbsp;Manager.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Device Manager in Windows 10" src="https://ngjunsiang.github.io/laymansguide/issue120_01.png" /&gt;&lt;br /&gt;
&lt;em&gt;Device Manager in Windows&amp;nbsp;10&lt;/em&gt;    &lt;/p&gt;
&lt;h2&gt;Generic&amp;nbsp;drivers&lt;/h2&gt;
&lt;p&gt;When you first set up your computer, it is not going to have driver files for devices that are already connected. How are you supposed to use your keyboard, mouse, and monitor, among other&amp;nbsp;things?&lt;/p&gt;
&lt;p&gt;The major operating systems come with &lt;strong&gt;generic driver files&lt;/strong&gt; pre-packaged. These generic driver files support a class of devices (e.g. input devices, pointing devices, display devices, …) for &lt;em&gt;basic features only&lt;/em&gt;. Manufacturers that wish their devices to work upon plugging in (this capability is also known as &lt;strong&gt;Plug-and-Play&lt;/strong&gt;) should aim to support these basic features through the use of generic&amp;nbsp;drivers.&lt;/p&gt;
&lt;p&gt;They can then encourage the customer to install custom drivers which may improve the device performance (e.g. for wifi or &lt;span class="caps"&gt;LAN&lt;/span&gt; networking devices), or provide additional features (e.g. storing mouse button configuration settings, or programmable mouse/keyboard&amp;nbsp;buttons).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Driver files provide information about the driver, and instructions on how to receive information from the device, and encode information to be passed to the device. The operating system may come with generic driver files for the device, but custom driver files might provide better performance or additional&amp;nbsp;features.&lt;/p&gt;
&lt;p&gt;Driver files were one of those mysterious things that made perfect sense once I took the time to think about why they are needed. I hope this issue does that for you&amp;nbsp;too.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 121: In graphic&amp;nbsp;detail&lt;/p&gt;
&lt;p&gt;One part I’ve always wanted to tackle with this season is explaining the process of getting graphics onto your monitor screen. The explanations I’ve found online are either too vague, trying to paper over the details with metaphors, or far too technical, going into exhaustive detail about the graphics rendering&amp;nbsp;pipeline.&lt;/p&gt;
&lt;p&gt;I want to strike more of a middle ground, to help those who have read the former and are trying to bridge the gap to the latter. The next two issues are going to talk about the processes involved in going from model (the computer’s view of things) to graphics (the final rendered&amp;nbsp;display).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;&lt;del&gt;a driver file and why do I need one? [Issue 98]&lt;/del&gt;&lt;/li&gt;
&lt;li&gt;a video card? [Issue&amp;nbsp;113]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 10"></category></entry><entry><title>Issue 119: Solid-state disks, an upgrade from hard disks</title><link href="https://ngjunsiang.github.io/laymansguide/issue119.html" rel="alternate"></link><published>2021-05-08T08:00:00+08:00</published><updated>2021-05-08T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-05-08:/laymansguide/issue119.html</id><summary type="html">&lt;p&gt;Solid-state disks are much faster than hard disks because they have no moving parts, so no time is wasted waiting for parts to get into the right position. However, they are more expensive than hard disk&amp;nbsp;drives.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; A hard disk consists of a read arm, and a set of magnetic platters which store data. To read or write data, the read arm must move to the appropriate track of the rotating platter, and detect the magnetic field (for reading), or attempt to magnetise the domains on the platter (for writing). Operations that require the read arm to access different parts of the magnetic platters intermittently result in slower read&amp;nbsp;speeds.&lt;/p&gt;
&lt;p&gt;Hard disks were, for a long time, the best affordable storage technology we had. But then something better came along: the solid state disk&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt; (&lt;span class="caps"&gt;SSD&lt;/span&gt;).&lt;/p&gt;
&lt;h2&gt;What is a solid state&amp;nbsp;disk?&lt;/h2&gt;
&lt;p&gt;It is, to put it simply, a small circuit board with lots of chips, that plugs into your&amp;nbsp;laptop.&lt;/p&gt;
&lt;p&gt;It has multiple storage chips, quite similar to the ones in your thumb drives/flash drives but much faster, and one controller chip to rule em&amp;nbsp;all.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Solid state disk inserted into laptop slot" src="https://ngjunsiang.github.io/laymansguide/issue119_01.jpg" /&gt;&lt;br /&gt;
&lt;em&gt;A solid state disk, plugged into a laptop slot (but not&amp;nbsp;secured)&lt;/em&gt;    &lt;/p&gt;
&lt;h2&gt;Hmm &amp;#8230; like computer&amp;nbsp;memory?&lt;/h2&gt;
&lt;p&gt;Computer memory also consists of chips on a circuit board, right? But memory gets wiped after the computer loses power &amp;#8230; but that doesn’t happen with SSDs;&amp;nbsp;why?&lt;/p&gt;
&lt;p&gt;&lt;img alt="Computer memory inserted into memory slot" src="https://ngjunsiang.github.io/laymansguide/issue119_02.png" /&gt;&lt;br /&gt;
&lt;em&gt;Computer memory sticks, inserted into the memory slot of a computer&amp;nbsp;motherboard&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Computer memory uses capacitors, which are like micro-sized batteries. They hold a charge when powered, and store either a 1 or 0 state by being charged or uncharged,&amp;nbsp;respectively.&lt;/p&gt;
&lt;p&gt;Solid state storage, on the other hand, uses gated transistors instead of capacitors. They lock a bunch of electrons behind a gate to fill a storage cell (storing a 1), and empty it by forcing the electrons out (strong a 0). This is slower than charging/discharging a capacitor, but hey you don’t lose your data when the power goes&amp;nbsp;out!&lt;/p&gt;
&lt;h2&gt;Solid state disks have no moving&amp;nbsp;parts&lt;/h2&gt;
&lt;p&gt;As you can see, there are no read arms or magnetic platters involved. No waiting for a platter to spin up, no waiting for a read arm to move back and forth &amp;#8230; access is almost instantaneous&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;This is a big deal when reading data from multiple locations; the response is hundreds of times quicker than a hard&amp;nbsp;disk!&lt;/p&gt;
&lt;h2&gt;What’s the&amp;nbsp;drawback?&lt;/h2&gt;
&lt;p&gt;The biggest drawback for now is of course price. Solid state disks cost much more, per &lt;span class="caps"&gt;GB&lt;/span&gt;, than a hard disk drive (&lt;span class="caps"&gt;HDD&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;Another potential drawback is that SSDs have a limited lifespan. This lifespan is not measured in months or years, but in the amount of data&amp;nbsp;written.&lt;/p&gt;
&lt;p&gt;You see, each time electrons are forced through the gate (called a program-erase cycle), the gate gets weaker. Do it tens of thousands of times, and eventually the gate gets too weak to hold electrons. A budget &lt;span class="caps"&gt;SSD&lt;/span&gt; typically has a lifespan of 30,000 program-erase cycles. For a &lt;span class="caps"&gt;256GB&lt;/span&gt; &lt;span class="caps"&gt;SSD&lt;/span&gt;, that’s 7.68 &lt;strong&gt;million&lt;/strong&gt; GBs of data-writing before it&amp;nbsp;fails!&lt;/p&gt;
&lt;p&gt;This was a big concern for early SSDs, which had program-erase cycles in the low thousands. Today, with technology improving the program-erase cycle lifespan of SSDs, most users would not come close to reaching the end-of-life of an &lt;span class="caps"&gt;SSD&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Solid-state disks are much faster than hard disks because they have no moving parts, so no time is wasted waiting for parts to get into the right position. However, they are more expensive than hard disk&amp;nbsp;drives.&lt;/p&gt;
&lt;p&gt;I managed to write this issue without once mentioning write amplification or &lt;span class="caps"&gt;NAND&lt;/span&gt;, and I consider that a significant personal&amp;nbsp;achievement.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 120: Drivers, the glue between hardware and&amp;nbsp;firmware&lt;/p&gt;
&lt;p&gt;So &amp;#8230; hard disk drives and solid state disks work pretty differently under the hood, yet when you plug them into a computer they just &amp;#8230; work? How does that&amp;nbsp;happen?&lt;/p&gt;
&lt;p&gt;For that matter, how do devices just work when we plug them into a&amp;nbsp;computer?&lt;/p&gt;
&lt;p&gt;This next week, when I finally talk about &amp;#8230;&amp;nbsp;drivers!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;li&gt;a video card? [Issue&amp;nbsp;113]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;The name has to do with the fact that its working principles are based on &lt;a href="https://en.wikipedia.org/wiki/Solid-state_physics"&gt;solid state physics&lt;/a&gt;, and not on the solidity of the disk itself.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;Feels almost instantaneous to humans &amp;#8230; but this still takes a few microseconds, which is considered slow for a computer!&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 10"></category></entry><entry><title>Issue 118: When I run two file-copy processes at the same time, why are they much slower?</title><link href="https://ngjunsiang.github.io/laymansguide/issue118.html" rel="alternate"></link><published>2021-05-01T08:00:00+08:00</published><updated>2021-05-01T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-05-01:/laymansguide/issue118.html</id><summary type="html">&lt;p&gt;A hard disk consists of a read arm, and a set of magnetic platters which store data. To read or write data, the read arm must move to the appropriate track of the rotating platter, and detect the magnetic field (for reading), or attempt to magnetise the domains on the platter (for writing). Operations that require the read arm to access different parts of the magnetic platters intermittently result in slower read&amp;nbsp;speeds.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Operating systems use a page file on the storage disk as a complement to physical memory. This allows OSes to behave more performantly than they would if they did not have a page file. Data that is rarely accessed is moved to the pagefile (“paged out”), and can be paged in when it is needed later, albeit with a performance&amp;nbsp;hit.&lt;/p&gt;
&lt;p&gt;It’s kind of funny the moment you realise how much of what an operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;) does is try to mitigate the slowness of hard disks. But why are they so &lt;em&gt;slow&lt;/em&gt;? There’s an actual explanation for that, and along the way it will help us understand a few things about why OSes sometimes behave&amp;nbsp;funny.&lt;/p&gt;
&lt;h2&gt;What is a hard&amp;nbsp;disk?&lt;/h2&gt;
&lt;p&gt;A hard disk is a magnetic platter that stores data. This platter contains trillions of magnetic atoms; yep, they’re atom-sized magnets! These atoms are grouped into clusters called magnetic domains; they are so tiny that 500 of these placed in a straight line would stretch the diameter of a human hair! These domains can align themselves in one of two different ways: let’s call them “up” and “down”. Each bit (as in, 8 bits = 1 byte) is stored as the alignment of a magnetic domain: up represents 1, and down represents&amp;nbsp;0.&lt;/p&gt;
&lt;p&gt;To read data, all you need to do is move a tiny electromagnet over each domain, and use it to see which way the domain is aligned. This can be done by measuring the current flowing through the electromagnet, a detailed explanation of which is beyond the scope of this newsletter (perhaps in a future newsletter titled “Layman’s Guide to Physics” or&amp;nbsp;something).&lt;/p&gt;
&lt;p&gt;To write data, you pass a current through the electromagnet to magnetise the domain below it whichever way you want; just spin the platter and keep changing the current to write a series of 1s or&amp;nbsp;0s.&lt;/p&gt;
&lt;p&gt;Put 3-5 platters together, attach the electromagnet to a moving arm (called the read arm), control the whole thing with some microchips, and you have a hard&amp;nbsp;disk.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Open hard disk" src="https://ngjunsiang.github.io/laymansguide/issue118_01.gif" /&gt;&lt;br /&gt;
&lt;em&gt;A picture of an opened hard disk, showing the read arms and magnetic&amp;nbsp;platters&lt;/em&gt;    &lt;/p&gt;
&lt;h2&gt;Characteristics of a hard&amp;nbsp;disk&lt;/h2&gt;
&lt;p&gt;A hard disk spins at a constant speed, because it is much more complicated to figure out how to read/write stuff when the speed can vary. The larger hard disks, which go into desktops and use 3.5-inch platters, spin at 7200rpm, while the smaller hard disks, which go into laptops and use 2.5-inch platters, spin at&amp;nbsp;5400rpm.&lt;/p&gt;
&lt;p&gt;Data stored near the circumference of a magnetic platter is faster to read than data stored near its centre. For this reason, OSes that are installed on hard disks usually attempt to partition the storage space so that the operating system is stored on domains closer to the&amp;nbsp;circumference.&lt;/p&gt;
&lt;p&gt;The read arm moves really close to the platters during disk operation! The gap between them (called the head gap or flying height) is half the thickness of a human hair. This is why you do not want to drop hard disks while they are in operation; the slightest movement of the read arm towards the magnetic platter causes it to gouge the platter surface and damage it permanently: this is called a head&amp;nbsp;crash.&lt;/p&gt;
&lt;h2&gt;Read and write&amp;nbsp;operations&lt;/h2&gt;
&lt;p&gt;The hard disk is ultimately a mechanical device; each operation involves moving&amp;nbsp;parts.&lt;/p&gt;
&lt;p&gt;Reading from or writing to a domain&amp;nbsp;involves:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Spinning up the platter (if it isn’t already&amp;nbsp;spinning)&lt;/li&gt;
&lt;li&gt;Moving the read arm to the correct&amp;nbsp;position&lt;/li&gt;
&lt;li&gt;Measuring or inducing a&amp;nbsp;current&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This means that each time the hard disk needs to access data from a different region of the disk, there is significant lag time (~5ms; see &lt;a href="https://ngjunsiang.github.io/laymansguide/issue057.html"&gt;Issue 57&lt;/a&gt;)). This is the time needed for all those movement described above. It is thus advantageous to try to put all the data you need in contiguous domains&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;, to minimise read arm&amp;nbsp;movement.&lt;/p&gt;
&lt;p&gt;So when an &lt;span class="caps"&gt;OS&lt;/span&gt; tries to perform two data operations at the same time (instead of sequentially), the read arm has to move a lot more to access data from different regions. And this is why, if you have multiple data operations to perform on a hard disk, you should try to do them sequentially instead of&amp;nbsp;simultaneously!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; A hard disk consists of a read arm, and a set of magnetic platters which store data. To read or write data, the read arm must move to the appropriate track of the rotating platter, and detect the magnetic field (for reading), or attempt to magnetise the domains on the platter (for writing). Operations that require the read arm to access different parts of the magnetic platters intermittently result in slower read&amp;nbsp;speeds.&lt;/p&gt;
&lt;p&gt;This was something that took me a while to figure out; first, to notice that it was actually happening, and second, to read up enough about hard drives and take some (nonworking) ones apart to understand. And now I can explain it to you&amp;nbsp;:)&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 119: Solid-state disks, an upgrade from hard&amp;nbsp;disks&lt;/p&gt;
&lt;p&gt;Next, we look at the technology that has been steadily replacing hard disks as system disks in most desktops and laptops. These have managed to eliminate the latency due to moving parts, and enable much higher read and write&amp;nbsp;speeds.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;li&gt;a video card? [Issue&amp;nbsp;113]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;This process is what millennials might remember as &lt;strong&gt;defragmentation&lt;/strong&gt;, or defragging.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 10"></category></entry><entry><title>Issue 117: Swap space</title><link href="https://ngjunsiang.github.io/laymansguide/issue117.html" rel="alternate"></link><published>2021-04-24T08:00:00+08:00</published><updated>2021-04-24T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-04-24:/laymansguide/issue117.html</id><summary type="html">&lt;p&gt;Operating systems use a page file on the storage disk as a complement to physical memory. This allows OSes to behave more performantly than they would if they did not have a page file. Data that is rarely accessed is moved to the pagefile (“paged out”), and can be paged in when it is needed later, albeit with a performance&amp;nbsp;hit.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Hibernation mode causes the computer to store the data configuration into a hibernation file on disk. When powered up, the &lt;span class="caps"&gt;OS&lt;/span&gt; reads the data configuration from the file back into memory. This lets the system avoid having to do a full shutdown and bootup; it performs a shorter version of these two sequences&amp;nbsp;instead.&lt;/p&gt;
&lt;p&gt;In &lt;a href="https://ngjunsiang.github.io/laymansguide/issue057.html"&gt;Issue 57&lt;/a&gt;), I laid out the transfer speeds and latencies for a few places in the computer where data can be&amp;nbsp;stored:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Hard disk drive (&lt;span class="caps"&gt;HDD&lt;/span&gt;): ≈5 ms response latency, 100 &lt;span class="caps"&gt;MB&lt;/span&gt;/s transfer&amp;nbsp;speed&lt;/li&gt;
&lt;li&gt;Solid state disk (&lt;span class="caps"&gt;SSD&lt;/span&gt;): up to 0.1 ms response latency, 0.5–1+ &lt;span class="caps"&gt;GB&lt;/span&gt;/s transfer&amp;nbsp;speed&lt;/li&gt;
&lt;li&gt;Physical memory (&lt;span class="caps"&gt;RAM&lt;/span&gt;): 0.1 µs (0.0001 ms) response latency, &lt;span class="caps"&gt;20GB&lt;/span&gt;/s transfer&amp;nbsp;speed&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;CPU&lt;/span&gt; register: &amp;lt;1 ns (&amp;lt;0.000001 ms) response&amp;nbsp;latency&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Apart from the &lt;span class="caps"&gt;CPU&lt;/span&gt; itself, physical memory is one of the fastest places to store and retrieve data. It’s just such a pity that there’s a limited amount of it, and you really can’t get more than &lt;span class="caps"&gt;32GB&lt;/span&gt; of it in a single computer (for future readers, this is written in early 2021). If you bought a cheaper computer that only has &lt;span class="caps"&gt;8GB&lt;/span&gt; or only &lt;span class="caps"&gt;4GB&lt;/span&gt;, this quickly limits the number of apps you can have open at any single&amp;nbsp;moment.&lt;/p&gt;
&lt;p&gt;What happens when you run out of&amp;nbsp;memory?&lt;/p&gt;
&lt;h2&gt;Running out of&amp;nbsp;memory&lt;/h2&gt;
&lt;p&gt;When a program needs more memory, it requests it from the operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;), and waits for the &lt;span class="caps"&gt;OS&lt;/span&gt; to give that memory. After all, it can’t do anything else until the memory is&amp;nbsp;available.&lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;OS&lt;/span&gt;, on the other hand, will give that memory if it is available. If not, it will wait for other programs to free up memory before passing that memory to the waiting program. If no program is willing to release memory&amp;nbsp;…&lt;/p&gt;
&lt;p&gt;I guess the computer just hangs&amp;nbsp;🤷&lt;/p&gt;
&lt;h2&gt;Mitigating out-of-memory&amp;nbsp;problems&lt;/h2&gt;
&lt;p&gt;If memory is limited, what can we do to increase&amp;nbsp;it?&lt;/p&gt;
&lt;p&gt;As it turns out, not all parts of memory are constantly being written to or read from. Lots of it is just sitting there, waiting for that one moment when the data is needed. Kind of wasteful if out of &lt;span class="caps"&gt;8GB&lt;/span&gt; of memory, only &lt;span class="caps"&gt;2GB&lt;/span&gt; is actively changing in any&amp;nbsp;hour.&lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;OS&lt;/span&gt; could write that mostly-static &lt;span class="caps"&gt;6GB&lt;/span&gt; to disk and free it up for other programs, maybe? Then when the data is needed, the &lt;span class="caps"&gt;OS&lt;/span&gt; reads it back from disk before passing it to the program. Yes, this would mean a slower response time for programs that have been idle for a while, but surely better than hanging because it ran out of&amp;nbsp;memory?&lt;/p&gt;
&lt;p&gt;This is what all modern OSes do. They write unused in-memory data to a file known as the &lt;strong&gt;page file&lt;/strong&gt;. In Windows, the page file&amp;nbsp;is &lt;code&gt;C:\pagefile.sys&lt;/code&gt;. When data is moved from physical memory to the page file, it is said to be &lt;em&gt;paged out&lt;/em&gt;, and when moved from page file to physical memory, it is &lt;em&gt;paged in&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;In older systems or software, you may also see this page file referred to as a &lt;em&gt;swap file&lt;/em&gt;, or &lt;em&gt;swap space&lt;/em&gt;. On Linux, a subregion of storage space can be set aside as a &lt;em&gt;swap partition&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;Why is it called the page&amp;nbsp;file?&lt;/h2&gt;
&lt;p&gt;Computer memory is organised into pages, each page typically being 4 KiB (notice that disk sectors are also typically 4 KiB (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue106.html"&gt;Issue 106&lt;/a&gt;)) …). The computer may have only &lt;span class="caps"&gt;8GB&lt;/span&gt; of physical memory, but present &lt;span class="caps"&gt;16GB&lt;/span&gt; of virtual memory (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue056.html"&gt;Issue 56&lt;/a&gt;)) to programs. Kind of like how banks only hold some liquidity but present their assets as being much more&amp;nbsp;…&lt;/p&gt;
&lt;p&gt;So the &lt;span class="caps"&gt;8GB&lt;/span&gt; “shortfall” is actually in the pagefile, not in memory. The pagefile essentially acts like (much slower) memory! The &lt;span class="caps"&gt;OS&lt;/span&gt; reads pages from it and writes pages to it, through virtual memory&amp;nbsp;accesses.&lt;/p&gt;
&lt;h2&gt;This sounds like a waste of space, can I choose not to use&amp;nbsp;it?&lt;/h2&gt;
&lt;p&gt;Though most OSes are configured to use a page file by default, you can configure it to not do so, although this is ill-advised (and I won’t explain why here; this is a newsletter aimed at layfolks). Modern OSes are pretty smart at managing page files, understanding that it is much slower than physical computer memory. They have their own algorithms to decide when to move data to a pagefile, and these days they even forecast when data that was paged out will soon be needed by the&amp;nbsp;program.&lt;/p&gt;
&lt;p&gt;OSes need page space for all kinds of reasons: as a kind of “emergency space” when physical memory is full, as working space for optimising data layout in memory, etc. So that disk space is definitely not going to&amp;nbsp;waste!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Operating systems use a page file on the storage disk as a complement to physical memory. This allows OSes to behave more performantly than they would if they did not have a page file. Data that is rarely accessed is moved to the pagefile (“paged out”), and can be paged in when it is needed later, albeit with a performance&amp;nbsp;hit.&lt;/p&gt;
&lt;p&gt;Pagefiles are pretty amazing, even though they used to be a huge pain in older systems, when they were stored on &lt;em&gt;slowwww&lt;/em&gt; hard disks. These days, page files stored on solid state disks are pretty fast! While the performance hit of a page miss is still noticeable (like when you switch to a Chrome tab that you haven’t touched in a while), it is far from the groan-inducing wait it used to&amp;nbsp;be.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S10] Issue 118: When I run two file-copy processes at the same time, why are they much&amp;nbsp;slower?&lt;/p&gt;
&lt;p&gt;Next week, a new season! With operating systems much more fleshed out for you (I hope), I can finally delve into the nuts and bolts of the machine itself: the&amp;nbsp;hardware.&lt;/p&gt;
&lt;p&gt;Since this &lt;em&gt;is&lt;/em&gt; a layman’s guide, I’m going to focus on the interesting bits, the parts that actually answer long-standing questions&amp;nbsp;:)&lt;/p&gt;
&lt;p&gt;Let’s start with a bang: sometimes you are copying a file from one flash drive to your external hard disk. And you need to copy another file from your external hard disk to the same flash drive (or even a different one). You’re scared; can the computer handle it? But this is taking so long; let’s just try it&amp;nbsp;anyway.&lt;/p&gt;
&lt;p&gt;And you notice the speed suddenly plummets. Why does that happen? Even with 117 issues of Layman’s Guide written, I don’t have enough background laid down to explain this. So let’s delve into the hard disk in the first issue of Season&amp;nbsp;10.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;li&gt;a video card? [Issue&amp;nbsp;113]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 09"></category><category term="cpu"></category><category term="memory"></category><category term="operating system"></category></entry><entry><title>Issue 116: Hibernation</title><link href="https://ngjunsiang.github.io/laymansguide/issue116.html" rel="alternate"></link><published>2021-04-17T08:00:00+08:00</published><updated>2021-04-17T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-04-17:/laymansguide/issue116.html</id><summary type="html">&lt;p&gt;Hibernation mode causes the computer to store the data configuration into a hibernation file on disk. When powered up, the &lt;span class="caps"&gt;OS&lt;/span&gt; reads the data configuration from the file back into memory. This lets the system avoid having to do a full shutdown and bootup; it performs a shorter version of these two sequences&amp;nbsp;instead.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; When you shut a computer down, it sends an exit signal to all running programs to get them to do their exit routine. This process can sometimes take a long time. To preserve the data configuration in memory while minimising power draw, a computer can go into standby mode: all hardware except the memory gets powered down, until the computer is woken up from&amp;nbsp;standby.&lt;/p&gt;
&lt;p&gt;Okay, so standby/sleep mode lets you keep the data configuration in memory by putting the computer into a sort of low-power mode, where only the memory still has power while the rest of the computer is shut&amp;nbsp;down.&lt;/p&gt;
&lt;p&gt;If you’re not just going for a short jaunt; you’re maybe getting on a flight and don’t have the laptop charger with you and want your laptop to still be in the same configuration (windows open, Spotify playing, etc) &lt;em&gt;without drawing any power&lt;/em&gt; … is that&amp;nbsp;possible?&lt;/p&gt;
&lt;p&gt;By now, you know the answer is &lt;em&gt;yes&lt;/em&gt;! You just put the computer in &lt;strong&gt;hibernation&lt;/strong&gt;&amp;nbsp;mode!&lt;/p&gt;
&lt;h2&gt;Hibernation&lt;/h2&gt;
&lt;p&gt;How is it possible to preserve data configuration in memory without any power at all? Any time you notice magic like this happening, you should immediately suspect that data storage is involved&amp;nbsp;somehow.&lt;/p&gt;
&lt;p&gt;When you put a computer into hibernate, Windows dumps the contents of computer memory into a hibernation file on your storage disk (check if you&amp;nbsp;have &lt;code&gt;C:\hiberfil.sys&lt;/code&gt; on your computer disk; that’s the one), then &lt;em&gt;shuts down&lt;/em&gt; your computer. Yup, it’s a shutdown, except the programs don’t have to exit (because remember, the data configuration is already preserved). So this is much faster than a normal shutdown, but not as fast as standby which involves no shutdown at&amp;nbsp;all.&lt;/p&gt;
&lt;p&gt;When you press the power button again to bring the system back, the computer still has to go through the bootup sequence (memory was completely wiped, remember). But at the point where control is handed over to the &lt;span class="caps"&gt;OS&lt;/span&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue112.html"&gt;Issue 112&lt;/a&gt;)), the &lt;span class="caps"&gt;OS&lt;/span&gt; does not carry out its usual preparations. Instead, it reads the contents of the hibernation file into memory, then acts as though the system is already booted up! You just log in, and everything is the way it was just before hibernation (keeping fingers crossed&amp;nbsp;…)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Hibernation mode causes the computer to store the data configuration into a hibernation file on disk. When powered up, the &lt;span class="caps"&gt;OS&lt;/span&gt; reads the data configuration from the file back into memory. This lets the system avoid having to do a full shutdown and bootup; it performs a shorter version of these two sequences&amp;nbsp;instead.&lt;/p&gt;
&lt;p&gt;Okay, so that’s one more mystery&amp;nbsp;explained.&lt;/p&gt;
&lt;p&gt;One thing I didn’t think was critical to explain above, but which eventually crops up for most people, is that the hibernation file takes up a lot of space! It needs to contain all the data in memory, so if you have a laptop with &lt;span class="caps"&gt;16GB&lt;/span&gt; of memory, your hibernation file will be around that size too. If you’re wondering why Windows is taking up so much space, this is usually one of those&amp;nbsp;reasons.&lt;/p&gt;
&lt;p&gt;If you&amp;nbsp;found &lt;code&gt;C:\hiberfil.sys&lt;/code&gt;, you might have also&amp;nbsp;found &lt;code&gt;C:\pagefile.sys&lt;/code&gt;, another huge file, though not as huge as the hibernation file. What is this one used for? I’ll explain that in the season finale&amp;nbsp;:)&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 117: Swap&amp;nbsp;space&lt;/p&gt;
&lt;p&gt;Sometimes, you get a laptop with 2 or 4 &lt;span class="caps"&gt;GB&lt;/span&gt; of memory, and you wonder how it’s able to run Chrome with 46 tabs open; shouldn’t you have run out of memory by now? In the next issue, I’ll explain this trick that lets computers pretend they have more&amp;nbsp;memory!&lt;/p&gt;
&lt;p&gt;I wanted to write this issue earlier in the season, but it just never presented an opportune moment. So let’s wrap up with&amp;nbsp;it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;li&gt;a video card? [Issue&amp;nbsp;113]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 09"></category><category term="operating system"></category></entry><entry><title>Issue 115: Shutdown &amp; standby</title><link href="https://ngjunsiang.github.io/laymansguide/issue115.html" rel="alternate"></link><published>2021-04-10T08:00:00+08:00</published><updated>2021-04-10T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-04-10:/laymansguide/issue115.html</id><summary type="html">&lt;p&gt;When you shut a computer down, it sends an exit signal to all running programs to get them to do their exit routine. This process can sometimes take a long time. To preserve the data configuration in memory while minimising power draw, a computer can go into standby mode: all hardware except the memory gets powered down, until the computer is woken up from&amp;nbsp;standby.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Embedded operating systems are unlike user operating systems. They are designed to run the software needed for an appliance’s operation, and are not meant to be used by users directly. Since they are considered somewhere between software and hardware, they are usually referred to as&amp;nbsp;firmware.&lt;/p&gt;
&lt;p&gt;In &lt;a href="https://ngjunsiang.github.io/laymansguide/issue112.html"&gt;Issue 112&lt;/a&gt;), I described the bootup process, and what a process it is! We have sayings in the English language that talk about things taking years to build up, but only seconds to destroy, and that’s certainly the case here. It takes so many steps to get data into computer memory in a way that makes it useable for us humans, and the mere press of a power switch wipes that data configuration once computer memory loses&amp;nbsp;power.&lt;/p&gt;
&lt;p&gt;This is obviously undesirable; your work in progress might not have been saved, data that is being transferred to disks might not be written properly and end up getting corrupted (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue109.html"&gt;Issue 109&lt;/a&gt;)), and you should never switch off a computer at the power socket this&amp;nbsp;way!&lt;/p&gt;
&lt;h2&gt;Forced&amp;nbsp;shutdowns&lt;/h2&gt;
&lt;p&gt;But sometimes, the computer gets itself so stuck that there’s just no other way to do it. On a laptop or smartphone, which has a built-in battery and cant be shut off immediately at the power socket, this is usually done by holding the power button for more than 10 seconds. This will literally cut off power to the mainboard&amp;nbsp;immediately.&lt;/p&gt;
&lt;p&gt;What does a proper shutdown do&amp;nbsp;differently?&lt;/p&gt;
&lt;h2&gt;Shutdown&lt;/h2&gt;
&lt;p&gt;To avoid the Problems described above, we need to give the computer time to wind down. Most programs have a proper exit routine (y’know, the red dot on macOS or the red cross in Windows). This gets the program to write any last bits of data, release any file or database locks (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue082.html"&gt;Issue 82&lt;/a&gt;)), quit, and then release its resources back to the &lt;span class="caps"&gt;OS&lt;/span&gt;. When shutting down, all running programs and services have this exit routine invoked, and they usually quit pretty&amp;nbsp;quickly.&lt;/p&gt;
&lt;p&gt;The exceptions to this usually have to do with disk operations, the slowest of operations on a computer. If a program has lots of data to save to disk, or is waiting for a disk operation to complete, it will remain open while attempting to exit, and prevent the computer from shutting down. If you’re wondering why a folder seems to be stopping the computer from shutting down, this is usually the&amp;nbsp;reason!&lt;/p&gt;
&lt;p&gt;“Wait, I’m not copying anything …” Yep, but File Explorer in Windows does more than just let you copy, paste, and delete. It also generates image and video thumbnails for you, create file indexes to speed up searching, and so on. And these are disk operations&amp;nbsp;too!&lt;/p&gt;
&lt;p&gt;On rare occasions, these disk operations can mean your computer takes up to an hour to shutdown! My best advice, if you’re not in a hurry, is to just leave it alone and let it do its thing. Much safer than having to scan for errors&amp;nbsp;later.&lt;/p&gt;
&lt;h2&gt;Standby/Sleep&lt;/h2&gt;
&lt;p&gt;On older computers, shutting down and booting up took more than 10 seconds, which is quite a killjoy when you are getting ready for a presentation and just need to stuff your laptop into a bag, get up to the conference room, and switch it on again. That feeling when you’re looking at a room full of people looking back at you, waiting for your laptop to boot up … &lt;em&gt;awkwardddddd&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Some of us just keep our laptops open (because closing the lid causes it to shut down, but you can change that in the settings by the way), and hold it that way while running around. And the purpose of that is just to keep the computer memory powered so we don’t lose the precious data configuration of the computer&amp;nbsp;memory!&lt;/p&gt;
&lt;p&gt;What if there was a way to keep power going to computer memory, but have the rest of the computer system powered off, to save on power? That would be perfect for hopping from coffee joint to coffee&amp;nbsp;joint!&lt;/p&gt;
&lt;p&gt;This is exactly what happens when you put a computer into &lt;strong&gt;standby&lt;/strong&gt; (some systems also refer to this as &lt;strong&gt;sleep&lt;/strong&gt;). The computer processor goes into a power state where only the computer memory remains powered (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue112.html"&gt;Issue 112&lt;/a&gt;)). Everything else is powered off—screen, network hardware, storage disks, … until you power the system on again. The memory configuration remains intact, and everything is exactly the way you left it at the moment you put the laptop into standby. Best of all, you didn’t have to go through the long and tedious shutdown-bootup sequence&amp;nbsp;again!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; When you shut a computer down, it sends an exit signal to all running programs to get them to do their exit routine. This process can sometimes take a long time. To preserve the data configuration in memory while minimising power draw, a computer can go into standby mode: all hardware except the memory gets powered down, until the computer is woken up from&amp;nbsp;standby.&lt;/p&gt;
&lt;p&gt;Lots of people don’t know about computer sleep, and resort to workarounds to keep their computer running while they have to move around. But now you&amp;nbsp;know.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 116:&amp;nbsp;Hibernation&lt;/p&gt;
&lt;p&gt;Sleep and hibernate; what’s the difference? As it turns out, a&amp;nbsp;lot!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;li&gt;a video card? [Issue&amp;nbsp;113]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 09"></category><category term="operating system"></category></entry><entry><title>Issue 114: In the beginning (firmware)</title><link href="https://ngjunsiang.github.io/laymansguide/issue114.html" rel="alternate"></link><published>2021-04-03T08:00:00+08:00</published><updated>2021-04-03T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-04-03:/laymansguide/issue114.html</id><summary type="html">&lt;p&gt;Embedded operating systems are unlike user operating systems. They are designed to run the software needed for an appliance’s operation, and are not meant to be used by users directly. Since they are considered somewhere between software and hardware, they are usually referred to as&amp;nbsp;firmware.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; If you can’t get to a &lt;span class="caps"&gt;BIOS&lt;/span&gt; screen, it is likely a hardware problem and has to be solved by a technician. If you can’t get the the &lt;span class="caps"&gt;OS&lt;/span&gt; loading screen, it’s a bootloader problem and needs to be solved with more geekery. If something goes wrong with &lt;span class="caps"&gt;OS&lt;/span&gt; loading, and fails to fix itself on subsequent reboots, it’s probably time for a system refresh or&amp;nbsp;reinstall.&lt;/p&gt;
&lt;p&gt;In &lt;a href="https://ngjunsiang.github.io/laymansguide/issue111.html"&gt;Issue 111&lt;/a&gt;), I described the bootup process. These days, anything more complex than a calculator typically has an operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;) running it. Your &lt;span class="caps"&gt;TV&lt;/span&gt; set-top box, your router, even your (high-end) printer and your car have an operating system running them these&amp;nbsp;days!&lt;/p&gt;
&lt;h2&gt;Embedded operating&amp;nbsp;systems&lt;/h2&gt;
&lt;p&gt;That &lt;span class="caps"&gt;OS&lt;/span&gt; is quite different from the one running on your laptop. It’s not meant for users to interact with directly. It primarily runs services which the appliance provides; you’re not allowed to install your own apps on it unless it has a manufacturer-approved app store or you are hacking&amp;nbsp;it.&lt;/p&gt;
&lt;p&gt;We call these &lt;strong&gt;embedded &lt;span class="caps"&gt;OS&lt;/span&gt;&lt;/strong&gt;es.&lt;/p&gt;
&lt;h2&gt;Hardware and&amp;nbsp;Software&lt;/h2&gt;
&lt;p&gt;We often see the term “hardware” used to refer to physical tools and implements that are needed for a job; they’re “hard” in the sense that they often come fixed into a certain configuration, are not easily changeable, and remain that way for the rest of their&amp;nbsp;lifetime.&lt;/p&gt;
&lt;p&gt;“Software”, on the other hand, is … fuzzier, more malleable, less clearly defined. Almost anything goes for software! This is usually the user-facing side of a&amp;nbsp;system.&lt;/p&gt;
&lt;h2&gt;Firmware&lt;/h2&gt;
&lt;p&gt;We usually consider laptop OSes to be software, because it has an installation process, you get small regular updates on it, you can add and remove features … if it looks like software, talks like software, and behaves like software, you might as well call it&amp;nbsp;software.&lt;/p&gt;
&lt;p&gt;Embedded OSes are kinda different. They come preinstalled; if they don’t, you won’t know how to install it yourself on a brand new, unprogrammed appliance. You don’t get small hotfixes and updates, only big version changes. And you can’t control what features it&amp;nbsp;has.&lt;/p&gt;
&lt;p&gt;If &lt;em&gt;soft&lt;/em&gt;ware sits at the layer nearest the user, and &lt;em&gt;hard&lt;/em&gt;ware sits at the layer nearest the machine, then I guess we’ll call the middle layer &lt;em&gt;firm&lt;/em&gt;ware.&lt;/p&gt;
&lt;p&gt;So when you see the word &lt;strong&gt;firmware&lt;/strong&gt; used in an interface, I guess you can just think of it as the “&lt;span class="caps"&gt;OS&lt;/span&gt; of the device”&amp;nbsp;:)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Embedded operating systems are unlike user operating systems. They are designed to run the software needed for an appliance’s operation, and are not meant to be used by users directly. Since they are considered somewhere between software and hardware, they are usually referred to as&amp;nbsp;firmware.&lt;/p&gt;
&lt;p&gt;Okay, that’s one thing out of the way. Now we can move on to shutdowns, sleeps, and&amp;nbsp;hibernates!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 115: Shutdown &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt;&amp;nbsp;standby&lt;/p&gt;
&lt;p&gt;I often hear a lot of confusion about this in my workplaces, and from folks too. This is often the cause of many problems-that-happen-when-I-press-the-power-button. Let’s see what we can&amp;nbsp;uncover.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;&lt;del&gt;firmware? [Issue 34]&lt;/del&gt;&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;li&gt;a video card? [Issue&amp;nbsp;113]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 09"></category><category term="operating system"></category></entry><entry><title>Issue 113: A computer’s existential crisis (boot failure)</title><link href="https://ngjunsiang.github.io/laymansguide/issue113.html" rel="alternate"></link><published>2021-03-27T08:00:00+08:00</published><updated>2021-03-27T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-03-27:/laymansguide/issue113.html</id><summary type="html">&lt;p&gt;f you can’t get to a &lt;span class="caps"&gt;BIOS&lt;/span&gt; screen, it is likely a hardware problem and has to be solved by a technician. If you can’t get the &lt;span class="caps"&gt;OS&lt;/span&gt; loading screen, it’s a bootloader problem and needs to be solved with more geekery. If something goes wrong with &lt;span class="caps"&gt;OS&lt;/span&gt; loading, and fails to fix itself on subsequent reboots, it’s probably time for a system refresh or&amp;nbsp;reinstall.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; When a computer is booted up, it runs the &lt;span class="caps"&gt;BIOS&lt;/span&gt; from a chip on the motherboard. The chip checks that core parts are present, checks for a storage disk containing a bootloader, loads it into memory, and hands over control. The bootloader loads the operating system kernel. The operating system kernel then does whatever it needs to do to get the system ready for&amp;nbsp;use.&lt;/p&gt;
&lt;p&gt;In this issue, doom and&amp;nbsp;gloom.&lt;/p&gt;
&lt;h2&gt;Nothing&amp;nbsp;happens&lt;/h2&gt;
&lt;p&gt;Sometimes, you press the power button and nothing happens. Absolute&amp;nbsp;stillness.&lt;/p&gt;
&lt;p&gt;This is not a software problem; something is wrong with the computer’s power source (the power supply unit of a desktop, or the battery/charging system of a laptop), and you have to bring it to a&amp;nbsp;technician.&lt;/p&gt;
&lt;h2&gt;Something happens, and then nothing&amp;nbsp;happens&lt;/h2&gt;
&lt;p&gt;If you hear fans spinning, the power source works. But if nothing appears on screen after that, and then the system seems to reboot again, it may mean the &lt;span class="caps"&gt;BIOS&lt;/span&gt; has failed; if you attempted to upgrade the &lt;span class="caps"&gt;BIOS&lt;/span&gt; recently, that might be a possible cause. A friendly geek might be able to fix this for&amp;nbsp;you.&lt;/p&gt;
&lt;p&gt;If you hear fans spinning for a long time, and things &lt;em&gt;seem&lt;/em&gt; to be happening, but nothing is showing on screen, your video card (future issue, I promise) may be borked. Can’t do anything other than sending it in to a&amp;nbsp;technician.&lt;/p&gt;
&lt;p&gt;If you have attempted a driver upgrade recently (again, future issue), that might be a possible&amp;nbsp;cause.&lt;/p&gt;
&lt;p&gt;When the &lt;span class="caps"&gt;BIOS&lt;/span&gt; works, you should see the startup screen—always a comforting&amp;nbsp;sight.&lt;/p&gt;
&lt;h2&gt;Bootloader&amp;nbsp;issues&lt;/h2&gt;
&lt;p&gt;Bootloaders are reliable as anything, so seldom anything goes wrong&amp;nbsp;here.&lt;/p&gt;
&lt;p&gt;The most common bootloader problem is that it does not detect any OSes to boot—may be the case the first time you attempt dual-boot, or get advanced enough to start playing with bootloaders (Windows and Mac don’t let you do that, so these are almost always Linux users). This is … something that can only be solved with more geekery, and is beyond the scope of this newsletter. Sorry&amp;nbsp;😬&lt;/p&gt;
&lt;h2&gt;&lt;span class="caps"&gt;OS&lt;/span&gt; booting&amp;nbsp;problems&lt;/h2&gt;
&lt;p&gt;The &lt;span class="caps"&gt;OS&lt;/span&gt;, being the most complex part of the bootup process, is often where things go&amp;nbsp;wrong.&lt;/p&gt;
&lt;p&gt;If nothing has changed in the system since the last bootup, usually things go smoothly. So it’s almost always changes to the system that cause&amp;nbsp;this.&lt;/p&gt;
&lt;p&gt;Sometimes you installed a program that dabbles in the system innards—a system-optimising program maybe—and it attempts to change some settings but without propagating them to all the required places. This leaves the system in an inconsistent&amp;nbsp;state.&lt;/p&gt;
&lt;p&gt;Sometimes you attempted a Windows Upgrade&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;, possibly a major one, and it did not go as hoped. Windows usually will attempt to undo the damage it wrought, and may or may not succeed in leaving your system in a bootable&amp;nbsp;state.&lt;/p&gt;
&lt;p&gt;Sometimes some system files get corrupted—remember that this can happen if you force a computer to power off before it has finished its shutdown properly. If the &lt;span class="caps"&gt;OS&lt;/span&gt; can’t find that file, it sometimes initiates a search for it in the system disk, which can take … really&amp;nbsp;long.&lt;/p&gt;
&lt;p&gt;In my opinion, the best way to resolve these issues, especially if they are recurring, is with a system refresh or&amp;nbsp;reinstall.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; If you can’t get to a &lt;span class="caps"&gt;BIOS&lt;/span&gt; screen, it is likely a hardware problem and has to be solved by a technician. If you can’t get the &lt;span class="caps"&gt;OS&lt;/span&gt; loading screen, it’s a bootloader problem and needs to be solved with more geekery. If something goes wrong with &lt;span class="caps"&gt;OS&lt;/span&gt; loading, and fails to fix itself on subsequent reboots, it’s probably time for a system refresh or&amp;nbsp;reinstall.&lt;/p&gt;
&lt;p&gt;I told you, gloom and doom. Modern OSes are getting more sensible at not requiring human intervention, so when they fail to resolve their own issues, there is often little a layperson can do on their&amp;nbsp;own.&lt;/p&gt;
&lt;p&gt;The best thing you can do for yourself and your data is to ensure you have a copy of your critical data elsewhere. Always have backups of things you are working&amp;nbsp;on.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 114: In the beginning&amp;nbsp;(firmware)&lt;/p&gt;
&lt;p&gt;I tried to find a place to talk about firmware in this and the previous issue, but couldn’t do so without taking the story in weird directions. So we’ll take a short detour to do that next issue, before I talk about shutdowns, sleeps, and&amp;nbsp;hibernates.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;li&gt;a video card? [Issue&amp;nbsp;113]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;These days it’s more like you attempted to stop a Windows Upgrade, but did not manage to do so or it is unskippable.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 09"></category><category term="operating system"></category></entry><entry><title>Issue 112: Bootstrapping into existence (bootup)</title><link href="https://ngjunsiang.github.io/laymansguide/issue112.html" rel="alternate"></link><published>2021-03-20T08:00:00+08:00</published><updated>2021-03-20T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-03-20:/laymansguide/issue112.html</id><summary type="html">&lt;p&gt;When a computer is booted up, it runs the &lt;span class="caps"&gt;BIOS&lt;/span&gt; from a chip on the motherboard. The chip checks that core parts are present, checks for a storage disk containing a bootloader, loads it into memory, and hands over control. The bootloader loads the operating system kernel. The operating system kernel then does whatever it needs to do to get the system ready for&amp;nbsp;use.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Moving a file (within the same disk region) merely updates its file table record, and this happens really quickly. Copying a file, or moving it to a different disk/region, involves copying the contents and then updating the file table record, and is considerably slower. Deleting a file only requires that its file table record be removed, and is a very fast operation (if it does not involve the Recycle&amp;nbsp;Bin).&lt;/p&gt;
&lt;p&gt;We humans wake up in the morning and magically there is information in our heads. We open our eyes, trust that all our limbs and body parts are there, and we just get up and make&amp;nbsp;coffee.&lt;/p&gt;
&lt;p&gt;Computers, they are different in that regard. After they light up the power &lt;span class="caps"&gt;LED&lt;/span&gt;, there is just … nothing. Computer memory needs power to hold its information&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;, so once you really power down your computer, all data in memory is gone. Your operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;), your open programs, your file data … all that now resides only in your hard disk. Whatever was not written to hard disk would have been lost by now (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue108.html"&gt;Issue 108&lt;/a&gt;)).&lt;/p&gt;
&lt;p&gt;Before anything can be done, the computer needs to read all that information back in to memory. But there’s &lt;em&gt;nothing&lt;/em&gt; in memory … you need a program loaded in memory to do that, but that program is still residing in file storage—it’s a Catch-22! The &lt;span class="caps"&gt;OS&lt;/span&gt; has to &lt;em&gt;bootstrap&lt;/em&gt; itself into existence first, in a process known as the &lt;strong&gt;bootup&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;Bootup&lt;/h2&gt;
&lt;p&gt;The way to sidestep the bootup paradox is to have a very tiny chip perform the bootstrap. This tiny chip is already wired up in a particular way from birth; it contains a program called the &lt;span class="caps"&gt;BIOS&lt;/span&gt; (short for Basic Input/Output System)&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt;, which does only three&amp;nbsp;things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check that the core parts are present: &lt;span class="caps"&gt;CPU&lt;/span&gt;, memory, video card, keyboard, storage disk.&lt;br /&gt;
   At the &lt;span class="caps"&gt;BIOS&lt;/span&gt; screen&lt;sup id="fnref:3"&gt;&lt;a class="footnote-ref" href="#fn:3"&gt;3&lt;/a&gt;&lt;/sup&gt;, you can press a button to configure &lt;span class="caps"&gt;BIOS&lt;/span&gt;&amp;nbsp;settings.&lt;/li&gt;
&lt;li&gt;The &lt;span class="caps"&gt;BIOS&lt;/span&gt; checks the available disks to see if they are “bootable”, i.e. contain information in a specific part of the disk that contains a &lt;strong&gt;bootloader&lt;/strong&gt;. In modern BIOSes, you can override this process by specifying a valid disk containing the&amp;nbsp;bootloader.&lt;/li&gt;
&lt;li&gt;The &lt;span class="caps"&gt;BIOS&lt;/span&gt; loads the bootloader into memory, and runs&amp;nbsp;it.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;The&amp;nbsp;bootloader&lt;/h2&gt;
&lt;p&gt;The &lt;span class="caps"&gt;BIOS&lt;/span&gt; is not optimised—it’s only a &lt;em&gt;basic&lt;/em&gt; system after all—and would take forever to try to get the &lt;span class="caps"&gt;OS&lt;/span&gt; running on its own. So, with its limited resources, the &lt;span class="caps"&gt;BIOS&lt;/span&gt; calls in bigger guns: the &lt;strong&gt;bootloader&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The bootloader is not the &lt;span class="caps"&gt;OS&lt;/span&gt;! It has only one job: to &lt;em&gt;load&lt;/em&gt; the &lt;span class="caps"&gt;OS&lt;/span&gt; during the &lt;em&gt;boot&lt;/em&gt; process, and carry out whatever is necessary before&amp;nbsp;that.&lt;/p&gt;
&lt;p&gt;Usually, the first thing that needs to be loaded is the filesystem (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue106.html"&gt;Issue 106&lt;/a&gt;)). Without that, no program will know how to read in data from the storage disks! At this point, if the bootloader detects disk errors or uncompleted operations in the journal (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue110.html"&gt;Issue 110&lt;/a&gt;)), it may attempt to scan for errors or complete those operations before proceeding with the rest of the bootup&amp;nbsp;process.&lt;/p&gt;
&lt;p&gt;Some systems may contain multiple OSes: Mac users may want to run Windows using Parallels Desktop, Windows users may want to dabble in Linux, and many Linux users dual-boot Windows as well. The bootloader, with the help of the filesystem, detects other operating systems on the storage disks, and offers the user a choice of which one to boot. If there is only one &lt;span class="caps"&gt;OS&lt;/span&gt; found, this step might be&amp;nbsp;skipped.&lt;/p&gt;
&lt;p&gt;Once an operating system is selected, the bootloader loads the &lt;span class="caps"&gt;OS&lt;/span&gt; &lt;strong&gt;kernel&lt;/strong&gt;, which contains its core instructions, and hands control over. The bootup sequence is not yet complete, but the bootloader has completed its&amp;nbsp;job.&lt;/p&gt;
&lt;h2&gt;The operating system&amp;nbsp;startup&lt;/h2&gt;
&lt;p&gt;The operating system is not ready to accept user input at this point yet. It still has to mount other storage disks and their subregions, check that other hardware is available and working properly (such as hardware timers, which are very important for an operating system), start up various necessary services, start up programs which asked to be started up when the system boots (like the annoying Adobe Updater, or your sound card drivers and utility),&amp;nbsp;etc.&lt;/p&gt;
&lt;p&gt;Finally, when it is ready, it displays the login&amp;nbsp;screen.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; When a computer is booted up, it runs the &lt;span class="caps"&gt;BIOS&lt;/span&gt; from a chip on the motherboard. The chip checks that core parts are present, checks for a storage disk containing a bootloader, loads it into memory, and hands over control. The bootloader loads the operating system kernel. The operating system kernel then does whatever it needs to do to get the system ready for&amp;nbsp;use.&lt;/p&gt;
&lt;p&gt;This a prelude to the next issue, which is where I attempt to explain, as simply as possible, the usual things that go wrong in the bootup&amp;nbsp;process.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 113: A computer’s existential crisis (boot&amp;nbsp;failure)&lt;/p&gt;
&lt;p&gt;We’ve all been there. The computer doesn’t get to the login screen. We are shocked that our daily driver, our churner of spreadsheets, can do this to us. Worse, it has left us in an environment we are wholly unfamiliar with: a blinking cursor in a sea of black, cryptic text explaining nothing, and the unbearable pressure of a system silently begging us: fix&amp;nbsp;me.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;del&gt;booting up? [Issue 15]&lt;/del&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Memory comprises lots of capacitors wired together, and capacitors slowly leak charge when they are not powered. Memory chips need periodic “refreshing” every few milliseconds to avoid losing data due to this charge leakage.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;Modern BIOSes are no longer stored permanently on a chip, but written to flash storage (similar to a thumbdrive) that is soldered directly to the motherboard.&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:3"&gt;
&lt;p&gt;Today, this process is hidden by a branded startup screen on machines that come with Windows pre-installed. You can usually disable that screen if you like to see this bootstrap process happening.&amp;#160;&lt;a class="footnote-backref" href="#fnref:3" title="Jump back to footnote 3 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 09"></category><category term="memory"></category><category term="operating system"></category></entry><entry><title>Issue 111: Copying, moving, and deleting files</title><link href="https://ngjunsiang.github.io/laymansguide/issue111.html" rel="alternate"></link><published>2021-03-13T08:00:00+08:00</published><updated>2021-03-13T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-03-13:/laymansguide/issue111.html</id><summary type="html">&lt;p&gt;Moving a file (within the same disk region) merely updates its file table record, and this happens really quickly. Copying a file, or moving it to a different disk/region, involves copying the contents and then updating the file table record, and is considerably slower. Deleting a file only requires that its file table record be removed, and is a very fast operation (if it does not involve the Recycle&amp;nbsp;Bin).&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Filesystem journals are a record of changes made to the disk, so as to enable those changes to be rolled back, or to be completed properly in case of sudden&amp;nbsp;interruption.&lt;/p&gt;
&lt;p&gt;Okay, that’s enough of a look at the nitty-gritty of moving data around. In this issue, let’s turn our attention to the file table, where interesting things happen as&amp;nbsp;well.&lt;/p&gt;
&lt;h2&gt;What happens when we copy a&amp;nbsp;file?&lt;/h2&gt;
&lt;p&gt;A copy of the data is made, and moved to an available location (series of sectors) on the disk, and a new file table record is created. No magic&amp;nbsp;yet.&lt;/p&gt;
&lt;h2&gt;What happens when we move a&amp;nbsp;file?&lt;/h2&gt;
&lt;p&gt;Okay, this is slightly more complex, but not&amp;nbsp;difficult.&lt;/p&gt;
&lt;p&gt;First of all, if we are moving the data within the same disk &amp;#8230; do we really need to move the data? In layman terms, when we “move” a file, we are not actually changing its physical location in the disk. We are basically updating a file so that its stated location, which used to be something&amp;nbsp;like &lt;code&gt;C:\Users\myusername\Desktop\video.mp4&lt;/code&gt;, will&amp;nbsp;become &lt;code&gt;C:\Users\myusername\Videos\video.mp4&lt;/code&gt;. This is information &lt;em&gt;about&lt;/em&gt; the data, and is not stored with the data—it is actually stored in the file&amp;nbsp;table!&lt;/p&gt;
&lt;p&gt;So when we “move” a file within the same disk region (i.e. within the warehouse, in our earlier analogy), we are actually just updating the file table record, without copying or writing any of its contents. This is why it can seem so amazingly fast to “move” a multi-gigabyte&amp;nbsp;file.&lt;/p&gt;
&lt;p&gt;But if we are “moving” the data to a different disk, or to a different region on the same disk (e.g. moving something from&amp;nbsp;the &lt;code&gt;C:\&lt;/code&gt; region to&amp;nbsp;the &lt;code&gt;D:\&lt;/code&gt; region), it is like moving cargo from one warehouse to another. Each warehouse keeps its own file table, so we can’t merely update the file table and keep the contents on the same pallet on the rack. The cargo needs to be moved to a different warehouse, and the file table record copied over as well. For data, this means the contents are copied to the new location, the file table is updated, and if both happen without error, the original contents and file table record are&amp;nbsp;deleted.&lt;/p&gt;
&lt;h2&gt;What happens when we delete a&amp;nbsp;file?&lt;/h2&gt;
&lt;p&gt;Ah, fun times. Okay, so I suppose most of you keep your computers on the default setting, where pressing&amp;nbsp;the &lt;code&gt;Delete&lt;/code&gt; key doesn’t actually delete a file irretrievably, but simply puts it in the Recycle&amp;nbsp;Bin.&lt;/p&gt;
&lt;p&gt;And when you are deleting a lot of files &amp;#8230; you notice this can take a long time. Obviously the contents are still around (so that you can still restore them), but the file table records are now &lt;em&gt;hidden&lt;/em&gt; from the user, so that they appear to be deleted. If you are deleting 100,000 files, that is 100,000 different file table records to&amp;nbsp;update.&lt;/p&gt;
&lt;p&gt;You can, of course, change this setting (and I won’t teach you how to do that here, but it’s just a Google search away). If you disable Recycle Bin, or use&amp;nbsp;the &lt;code&gt;Shift+Delete&lt;/code&gt; hotkey combination to force a permanent deletion, something different happens instead. Intuitively, it seems like the &lt;span class="caps"&gt;OS&lt;/span&gt; should actually clear out the (digital) space previously occupied by the file contents. But this offers no advantage; to change&amp;nbsp;a &lt;code&gt;1&lt;/code&gt; to&amp;nbsp;a &lt;code&gt;0&lt;/code&gt;, you have to &lt;em&gt;write&lt;/em&gt; a 0 to it; there is no &lt;em&gt;erasing&lt;/em&gt; in the world of&amp;nbsp;storage!&lt;/p&gt;
&lt;p&gt;The faster way is to simply tell the filesystem that this file no longer exists, and the space it used to take up can now be overwritten. And that is done &amp;#8230; by simply deleting the file table record. This is a simpler operation than updating, which is why permanent deletes are so much faster than Recycle-Bin&amp;nbsp;deletes.&lt;/p&gt;
&lt;p&gt;This is also why, if you accidentally permanently deleted a file, you still have a chance of recovering it with file recovery software! The recovery software ignores the file table, and instead scans the entire disk for its actual contents, trying to see if there are any valid fragments of files left. If the space has not been overwritten by anything else, you just might be lucky enough to recover its contents. The chances of this decrease as you use the disk and write over parts of the disk which contained the file data, so this has to be done as soon as&amp;nbsp;possible.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Moving a file (within the same disk region) merely updates its file table record, and this happens really quickly. Copying a file, or moving it to a different disk/region, involves copying the contents and then updating the file table record, and is considerably slower. Deleting a file only requires that its file table record be removed, and is a very fast operation (if it does not involve the Recycle&amp;nbsp;Bin).&lt;/p&gt;
&lt;p&gt;I’ve been waiting a long time to explain this, and can finally get it out on screen.&amp;nbsp;Phew!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 112: Bootstrapping into&amp;nbsp;existence&lt;/p&gt;
&lt;p&gt;Now I can move on to something closer to the hardware, and talk about what happens when the power button is&amp;nbsp;pressed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 09"></category></entry><entry><title>Issue 110: Safeguarding against data corruption with a journal</title><link href="https://ngjunsiang.github.io/laymansguide/issue110.html" rel="alternate"></link><published>2021-03-06T08:00:00+08:00</published><updated>2021-03-06T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-03-06:/laymansguide/issue110.html</id><summary type="html">&lt;p&gt;Filesystem journals are a record of changes made to the disk, so as to enable those changes to be rolled back, or to be completed properly in case of sudden&amp;nbsp;interruption.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Fast writes dump the data to a write cache (in computer memory), then update the file table to look like the file is already written to disk. However, if power is cut before all data is properly moved from the write cache to disk, the data in memory is lost, and file corruption usually&amp;nbsp;results.&lt;/p&gt;
&lt;p&gt;Last issue, I showed how a write cache can “speed up” file write operations by allowing data to be dumped to a write cache in memory first. The &lt;span class="caps"&gt;OS&lt;/span&gt; “completes” the operation, making it look the file has been successfully written to disk, when in actuality parts of it are still in memory waiting to be written. This works fine as long as we don’t suffer catastrophic power loss before everything gets put on disk; once power is cut, all the contents of the write cache in memory are&amp;nbsp;lost!&lt;/p&gt;
&lt;p&gt;All right, we are into worst-case scenarios here. How do we recover from something like&amp;nbsp;this?&lt;/p&gt;
&lt;p&gt;The first thing we’ll need to know is what had happened; what data made it through and was written, and what data didn’t make it. We need a record of changes to the&amp;nbsp;disk.&lt;/p&gt;
&lt;h2&gt;File&amp;nbsp;journals&lt;/h2&gt;
&lt;p&gt;This record, in a filesystem, is called a journal. Keeping a journal requires more &lt;span class="caps"&gt;CPU&lt;/span&gt; cycles and can slow down file write operations somewhat, but as disk performance increases, this is increasingly considered a worthwhile tradeoff for data&amp;nbsp;reliability.&lt;/p&gt;
&lt;p&gt;The default filesystems on major OSes today are all journalled, and &lt;span class="caps"&gt;NTFS&lt;/span&gt; is no exception. I’m not going into details of how this is done, and instead will list the things that a journal enables Windows 10 to&amp;nbsp;do.&lt;/p&gt;
&lt;h2&gt;Disk recovery with a&amp;nbsp;journal&lt;/h2&gt;
&lt;p&gt;So what happens when power is cut and a computer reboots? It goes into recovery mode, where it checks the log for incomplete operations, and attempts to complete them. Any files that were “lost” (i.e. file table records that have been separated or desynced from the actual data) get moved to a separate folder, and the disk is considered okay for use&amp;nbsp;again.&lt;/p&gt;
&lt;h2&gt;Disk transaction&amp;nbsp;rollbacks&lt;/h2&gt;
&lt;p&gt;When installing a new application, sometimes you encounter that odd screen where the installation fails because something that needed to happen couldn’t happen. Oh great, the application could not complete installing, and now we have to remove it &amp;#8230; how do we undo all the things that were&amp;nbsp;done?&lt;/p&gt;
&lt;p&gt;The journal lets you see a list of all the changes that were made to the disk from the start of the installation, so that you can perform the equivalent steps to reverse their&amp;nbsp;effect.&lt;/p&gt;
&lt;h2&gt;System restore with&amp;nbsp;journals&lt;/h2&gt;
&lt;p&gt;The same thing happens with some Windows updates. Windows stores a lot of the old data as backup, in case you ever want to roll back some system changes, or return to an older version of Windows (minus some breaking updates, for example). And again, the filesystem journal lets you do that, by identifying which changes were responsible for the&amp;nbsp;update.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Filesystem journals are a record of changes made to the disk, so as to enable those changes to be rolled back, or to be completed properly in case of sudden&amp;nbsp;interruption.&lt;/p&gt;
&lt;p&gt;Journals are serious magic, and are what enable OSes to recover gracefully from a crash. They are kind of divorced from everyday experience, because it is usually unwise to mess with them directly, but I hope the past few issues explain why it is important to not get impatient with your computer when it seems a little slow moving data&amp;nbsp;round.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 111: Copying, moving, and deleting&amp;nbsp;files&lt;/p&gt;
&lt;p&gt;I am done with the more technical stuff! Now we can really move on to more everyday experiences, like copying, deleting, and moving files&amp;nbsp;:)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 09"></category><category term="memory"></category></entry><entry><title>Issue 109: Speeding up data operations</title><link href="https://ngjunsiang.github.io/laymansguide/issue109.html" rel="alternate"></link><published>2021-02-27T08:00:00+08:00</published><updated>2021-02-27T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-02-27:/laymansguide/issue109.html</id><summary type="html">&lt;p&gt;Fast writes dump the data to a write cache (in computer memory), then update the file table to look like the file is already written to disk. However, if power is cut before all data is properly moved from the write cache to disk, the data in memory is lost, and file corruption usually&amp;nbsp;results.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Safe writes ensure that all the data is written to disk sectors properly first before updating the file table. The result is that write operations take a longer time to&amp;nbsp;complete.&lt;/p&gt;
&lt;p&gt;If there’s anything to take away from the previous issue, it’s that &lt;em&gt;doing things the right way takes time&lt;/em&gt;. And sometimes we are okay with taking shortcuts to get something done faster, the &lt;em&gt;left&lt;/em&gt;&amp;nbsp;way.&lt;/p&gt;
&lt;h2&gt;The write&amp;nbsp;cache&lt;/h2&gt;
&lt;p&gt;If you have a shipment coming in that urgently needs to be dumped into the warehouse quickly (e.g. because the ship needs to leave quickly for another delivery, or to free up space on the docks), how do we speed this&amp;nbsp;up?&lt;/p&gt;
&lt;p&gt;What usually happens is the goods will be offloaded onto some empty space &lt;strong&gt;outside the warehouse&lt;/strong&gt;. The ship will leave first, and &lt;em&gt;assume&lt;/em&gt; that the warehouse will be able to get the goods in just&amp;nbsp;fine.&lt;/p&gt;
&lt;p&gt;What is the &lt;span class="caps"&gt;OS&lt;/span&gt; equivalent of this? Hard disk writes are actually pretty slow; they can get up to 100 &lt;span class="caps"&gt;MB&lt;/span&gt;/s, but usually hit a sustained speed of 60 &lt;span class="caps"&gt;MB&lt;/span&gt;/s for system disks, and about 20 &lt;span class="caps"&gt;MB&lt;/span&gt;/s over &lt;span class="caps"&gt;USB&lt;/span&gt;. The operating system therefore sets up a “dumping space” in memory; files can be read from other disks at up to 200 &lt;span class="caps"&gt;MB&lt;/span&gt;/s, and written to memory at &lt;strong&gt;a few &lt;span class="caps"&gt;GB&lt;/span&gt;/s&lt;/strong&gt;!&lt;/p&gt;
&lt;p&gt;This space is known as the &lt;strong&gt;write cache&lt;/strong&gt;. (I’ve previously covered the idea of caches in Issues &lt;a href="https://ngjunsiang.github.io/laymansguide/issue039.html"&gt;39&lt;/a&gt;) and &lt;a href="https://ngjunsiang.github.io/laymansguide/issue057.html"&gt;57&lt;/a&gt;), and the write cache works on a similar&amp;nbsp;idea.)&lt;/p&gt;
&lt;h2&gt;Speeding up file access &amp;#8230; and its&amp;nbsp;drawbacks&lt;/h2&gt;
&lt;p&gt;So how do we speed up the process of copying data to a disk? We could dump the data into the write cache (assuming there is enough space), then update the file table first, and let the &lt;span class="caps"&gt;OS&lt;/span&gt; slowly copy the data from the write cache into the disk. Anyone who needs access to the file records will then be able to go about their merry way, and if they need data from the file, they can just copy it from the disk cache instead of the disk. Easy&amp;nbsp;peasy!&lt;/p&gt;
&lt;p&gt;This was what happened in Windows &lt;span class="caps"&gt;XP&lt;/span&gt;. File copying appears to be speeded up, and users are&amp;nbsp;happy.&lt;/p&gt;
&lt;p&gt;But remember what happens when power to the computer is accidentally cut, or if a program suddenly hangs, or the computer sometimes runs out of&amp;nbsp;memory.&lt;/p&gt;
&lt;p&gt;Yup &amp;#8230; it shuts down (or reboots), and the contents of memory are wiped clean. And any data that has not been written to disk is &lt;em&gt;lost forever&lt;/em&gt;. And &amp;#8230; the file table has &lt;strong&gt;already been updated to look like there is valid data&lt;/strong&gt;!&lt;/p&gt;
&lt;p&gt;This is when things look really&amp;nbsp;bad.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Fast writes dump the data to a write cache (in computer memory), then update the file table to look like the file is already written to disk. However, if power is cut before all data is properly moved from the write cache to disk, the data in memory is lost, and file corruption usually&amp;nbsp;results.&lt;/p&gt;
&lt;p&gt;Well okay &amp;#8230; it’s not a lost cause right? Yup, we do have ways to mitigate this kind of damage, and one such way is called a&amp;nbsp;journal.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 110: Safeguarding against data corruption with a&amp;nbsp;journal&lt;/p&gt;
&lt;p&gt;Woot, answered another sometime-in-the-future question, albeit in a slightly different fashion from how I thought I would do&amp;nbsp;it.&lt;/p&gt;
&lt;p&gt;Next issue, I explain how we can still do things the fast way, &lt;em&gt;carefully&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;li&gt;&lt;del&gt;why does computer memory exist when apps can read directly from the hard disk? [Issue 105]&lt;/del&gt;&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 09"></category><category term="cache"></category><category term="memory"></category></entry><entry><title>Issue 108: Safeguarding data operations</title><link href="https://ngjunsiang.github.io/laymansguide/issue108.html" rel="alternate"></link><published>2021-02-20T08:00:00+08:00</published><updated>2021-02-20T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-02-20:/laymansguide/issue108.html</id><summary type="html">&lt;p&gt;Safe writes ensure that all the data is written to disk sectors properly first before updating the file table. The result is that write operations take a longer time to&amp;nbsp;complete.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; When write operations are interrupted prematurely, filesystem corruption often&amp;nbsp;results.&lt;/p&gt;
&lt;p&gt;When a batch of data (shipment of cargo)) arrives at our warehouse, there are two things that need&amp;nbsp;doing:&lt;/p&gt;
&lt;p&gt;1) The data needs to be written into sectors (the smallest unit of storage that the disk handles),
2) The file table needs to be&amp;nbsp;updated.&lt;/p&gt;
&lt;h2&gt;Writing data the safe&amp;nbsp;way&lt;/h2&gt;
&lt;p&gt;If the write operation gets interrupted, it is preferable that our file table is not updated; this way, we will not find any reference to the data in the file table, and we can attempt the write operation&amp;nbsp;again.&lt;/p&gt;
&lt;p&gt;This will appear strange in a warehouse analogy; the cargo, damaged or intact, is still occupying space on the racks! But remember that when we are talking about a hard disk storing data, there is no &lt;em&gt;physical cargo&lt;/em&gt;; the data exists as a specific arrangement of electrons/atoms. We can override the existing arrangement of electrons/atoms without having to reset it&amp;nbsp;first.&lt;/p&gt;
&lt;h2&gt;Drawbacks of safe&amp;nbsp;writing&lt;/h2&gt;
&lt;p&gt;This way of storing data first before updating the file table is advantageous in its security; if the write operation is interrupted halfway, we are less likely to suffer filesystem&amp;nbsp;corruption.&lt;/p&gt;
&lt;p&gt;But for large batches of data (or large shipments of cargo), this means a long wait &amp;#8230; and in the meantime, nothing else can happen! Hard drives only have one writing needle, which is like a warehouse only having one forklift. If you have other applications waiting for that file (similar to other employees waiting for the file record to appear in the file table), they will be twiddling their thumbs until the last sector of data is written, the last pallet of cargo loaded onto the&amp;nbsp;racks.&lt;/p&gt;
&lt;p&gt;But this is the &lt;em&gt;right thing to do&lt;/em&gt;, isn’t it? It doesn’t matter; &lt;a href="https://blog.codinghorror.com/actual-performance-perceived-performance/"&gt;Windows Vista did it this way&lt;/a&gt;, waiting for write operations to complete before updating the file metadata in the file table, and the result is that &lt;em&gt;people complained that it was slower&lt;/em&gt;. People do not like things&amp;nbsp;slow!&lt;/p&gt;
&lt;p&gt;Why did Windows &lt;span class="caps"&gt;XP&lt;/span&gt; feel faster, then? Because it did it the other way&amp;nbsp;round!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Safe writes ensure that all the data is written to disk sectors properly first before updating the file table. The result is that write operations take a longer time to&amp;nbsp;complete.&lt;/p&gt;
&lt;p&gt;Actually, Windows 10 uses a system similar to this for mounting portable devices (&lt;span class="caps"&gt;USB&lt;/span&gt; hard drives, flash drives, etc) by default. They sped it up in other ways. So if you accidentally unplug a drive before it is completely done writing &amp;#8230; usually it won’t completely screw things&amp;nbsp;up.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 109: Speeding up data&amp;nbsp;operations&lt;/p&gt;
&lt;p&gt;If you want, there are settings you can adjust to make Windows 10 access a portable device &lt;em&gt;the fast way&lt;/em&gt;. I don’t teach it here, because this is not the newsletter for it. But we’ll see how &lt;em&gt;the fast way&lt;/em&gt; works in the next&amp;nbsp;issue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;li&gt;why does computer memory exist when apps can read directly from the hard disk? [Issue&amp;nbsp;105]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 09"></category></entry><entry><title>Issue 107: The challenges of storage</title><link href="https://ngjunsiang.github.io/laymansguide/issue107.html" rel="alternate"></link><published>2021-02-13T08:00:00+08:00</published><updated>2021-02-13T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-02-13:/laymansguide/issue107.html</id><summary type="html">&lt;p&gt;When write operations are interrupted prematurely, filesystem corruption often&amp;nbsp;results.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; A hard disk is organised into sectors, which are the smallest unit of storage. The &lt;span class="caps"&gt;OS&lt;/span&gt;’s filesystem determines how and where to store each file on the hard disk. The filesystem manages the file metadata in a file table, separate from the actual contents of the&amp;nbsp;file.&lt;/p&gt;
&lt;p&gt;Last issue I painted the picture of a warehouse, where cargo gets stored in racks, their contents and details are stored in the file table (in the warehouse office), and we can easily look up the details of each shipment of cargo without having to inspect rack by&amp;nbsp;rack.&lt;/p&gt;
&lt;p&gt;Let’s screw things up here, shall&amp;nbsp;we?&lt;/p&gt;
&lt;h2&gt;Read/write&amp;nbsp;failure&lt;/h2&gt;
&lt;p&gt;As mechanical devices, hard disks are prone to failure; I’ll go into detail why in a future season on hardware. Remember that when this hard drive is housed in a computer, that computer can get disturbed by shocks or jolts. If it is housed in an external &lt;span class="caps"&gt;USB&lt;/span&gt; enclosure, sometimes we do terrible things to that enclosure &amp;#8230; like when we drop them (usually accidentally&amp;nbsp;😬).&lt;/p&gt;
&lt;p&gt;This is like the warehouse experiencing an earthquake. Sometimes, a small earthquake may be uneventful &amp;#8230; other times, we might even get away with a moderate earthquake, if nothing is happening in the warehouse. But if it happens while a forklift is carrying out a precarious loading operation &amp;#8230;&amp;nbsp;🙈&lt;/p&gt;
&lt;p&gt;Well, that’s terrible. We’ll have to write off that shipment. Luckily, we can analogy here; data can always be re-copied again. The problem is &amp;#8230; when a shipment comes in, we have to load the shipment onto the rack, &lt;em&gt;and&lt;/em&gt; update the file table, so that its contents match whatever is on the racks. Which should we do first? If the loading is uneventful, it doesn’t really matter; after 5 minutes or so, both the file table and the racks will be in sync. But if the earthquake happens during this loading, they won’t&amp;nbsp;be!&lt;/p&gt;
&lt;p&gt;We don’t need an earthquake to screw things up. Remember that a hard drive, or even a flash drive, needs power to run. When we unplug a hard disk or flash drive while it is still operating, it’s like cutting power to the entire warehouse (including its forklifts) at the same time. If nothing is happening in the warehouse, it is safe to do so. But if there are ongoing operations, well you don’t need me to tell you that’s not a good&amp;nbsp;idea.&lt;/p&gt;
&lt;h2&gt;Reads are usually&amp;nbsp;safe&lt;/h2&gt;
&lt;p&gt;Okay, we’ll have to step away from the warehouse analogy for a bit when we talk about reading data from a hard drive; this is usually safe, because unlike cargo, reading data does not destroy the existing copy. We can mount a filesystem (i.e. gain access to it) for reading only, which will protect it from accidentally having its data&amp;nbsp;overwritten.&lt;/p&gt;
&lt;p&gt;It is the writes that always get us. When a write operation does not go smoothly, the result is usually filesystem corruption; the file table is no longer up to date, or worse, some parts of it might be improperly written, resulting in the company no longer knowing what is on the warehouse&amp;nbsp;racks.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; When write operations are interrupted prematurely, filesystem corruption often&amp;nbsp;results.&lt;/p&gt;
&lt;p&gt;There are two ways we can try to get data onto the disk (get cargo into the warehouse): Write the data first, then update the file table, or vice-versa. Which way is better? We’ll compare the pros and cons in the next&amp;nbsp;issue.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 108: Safeguarding data&amp;nbsp;operations&lt;/p&gt;
&lt;p&gt;That remove-safely thing we all hate to do when we unplug our hard drives? Yeah I’m getting to that&amp;nbsp;part.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;li&gt;why does computer memory exist when apps can read directly from the hard disk? [Issue&amp;nbsp;105]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 09"></category></entry><entry><title>Issue 106: Organising storage</title><link href="https://ngjunsiang.github.io/laymansguide/issue106.html" rel="alternate"></link><published>2021-02-06T08:00:00+08:00</published><updated>2021-02-06T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-02-06:/laymansguide/issue106.html</id><summary type="html">&lt;p&gt;A hard disk is organised into sectors, which are the smallest unit of storage. The &lt;span class="caps"&gt;OS&lt;/span&gt;’s filesystem determines how and where to store each file on the hard disk. The filesystem manages the file metadata in a file table, separate from the actual contents of the&amp;nbsp;file.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; The &lt;span class="caps"&gt;OS&lt;/span&gt; takes care of booting up, login and user management, window management, memory allocation, storage interfaces, background services, peripheral management, and much more. Access to these services, where allowed, is provided in the form of software libraries that developers can&amp;nbsp;use.&lt;/p&gt;
&lt;p&gt;So far, what we understand about storage is that apps send data to an operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;), which then stores it on storage devices (such as hard disks, solid state disks, etc) for later retrieval. And in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue099.html"&gt;Issues 99&lt;/a&gt;) &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; &lt;a href="https://ngjunsiang.github.io/laymansguide/issue100.html"&gt;100&lt;/a&gt;), I explained that eventually everything gets stored as a&amp;nbsp;file.&lt;/p&gt;
&lt;p&gt;What exactly is a&amp;nbsp;file?&lt;/p&gt;
&lt;h2&gt;Files in operating&amp;nbsp;systems&lt;/h2&gt;
&lt;p&gt;Let’s start from what we know about files. When we open File Explorer in Windows and open any folder (aka &lt;strong&gt;directory&lt;/strong&gt;), we see a bunch of things with colourful icons, that we know of as files. And we also see things with folder icons that we know as folders (or&amp;nbsp;directories).&lt;/p&gt;
&lt;p&gt;They also have additional details displayed alongside them, such as the date/time (henceforth referred to as datetime) it was created, the last datetime it was modified, and the last datetime it was accessed. You might also get additional information, such as what type of file it is, its size, the user who owns that file, etc—in other words, the &lt;strong&gt;metadata&lt;/strong&gt; of the file. That’s a fancy word to refer to data (datetime, filetype, size, …) &lt;em&gt;about&lt;/em&gt; the data (the&amp;nbsp;files).&lt;/p&gt;
&lt;p&gt;Now, a brief introduction to the hardware, where the actual files get&amp;nbsp;stored.&lt;/p&gt;
&lt;h2&gt;Introducing&amp;nbsp;storage&lt;/h2&gt;
&lt;p&gt;When you buy a hard drive (or solid state drive) and connect it up to your computer (or laptop), the computer does not have fine-grained control over every bit of storage. Instead, the hard drive gets presented as a huge volume of space, like a&amp;nbsp;warehouse.&lt;/p&gt;
&lt;p&gt;This volume is organised into &lt;strong&gt;sectors&lt;/strong&gt; (these days, a modern hard drive sector is 4KiB), which are the smallest unit of storage on a hard disk. If you have to store 1KiB of data, it will still take up an entire sector; the rest of the space is filled with zeroes. This is similar to the way a warehouse is managed by pallets, and not by single cardboard boxes. If your shipment does not fill the entire pallet, the rest of the space is&amp;nbsp;“wasted”.&lt;/p&gt;
&lt;p&gt;Each sector has an address (yes, just like memory!). To store data on the hard disk, the &lt;span class="caps"&gt;OS&lt;/span&gt; has to “tell” the hard disk a) what data to store, and b) the address at which to store the&amp;nbsp;data.&lt;/p&gt;
&lt;p&gt;The hard drive itself does not inherently have any system for managing your files or folders; it can’t tell you&amp;nbsp;where &lt;code&gt;draft1.docx&lt;/code&gt; is stored, nor tell you what the size is. It only takes care of storing data at addresses, and retrieving data from&amp;nbsp;addresses!&lt;/p&gt;
&lt;h2&gt;Managing&amp;nbsp;storage&lt;/h2&gt;
&lt;p&gt;And this is where the operating system comes in. An operating system installing itself is like a company occupying a warehouse. You’ve got to impose some kind of order on the&amp;nbsp;space!&lt;/p&gt;
&lt;p&gt;In an operating system, this job is delegated to the &lt;strong&gt;file system&lt;/strong&gt;, a sort of facility director who manages the storage space. Windows uses &lt;a href="https://en.wikipedia.org/wiki/NTFS"&gt;&lt;span class="caps"&gt;NT&lt;/span&gt; File System&lt;/a&gt; (&lt;strong&gt;&lt;span class="caps"&gt;NTFS&lt;/span&gt;&lt;/strong&gt;) for its own space, MacOS uses &lt;a href="https://en.wikipedia.org/wiki/High_Performance_File_System"&gt;High Performance File System&lt;/a&gt; (&lt;strong&gt;&lt;span class="caps"&gt;HPFS&lt;/span&gt;&lt;/strong&gt;), while portable storage devices (e.g. &lt;span class="caps"&gt;USB&lt;/span&gt; drives) often use &lt;a href="https://en.wikipedia.org/wiki/File_Allocation_Table"&gt;File Allocation Table&lt;/a&gt; (&lt;span class="caps"&gt;FAT&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;An operating system that is not “aware” of other filesystems will not be able to read storage devices formatted with those filesystems. This is why you can’t just take out a disk from a Mac system and expect it to open in Windows when plugged in. Windows can read &lt;span class="caps"&gt;HPFS&lt;/span&gt; disks, but cannot write data to them. Both OSes can handle &lt;span class="caps"&gt;FAT&lt;/span&gt; (phew!), which is why it is possible to pass files from a Windows user to a Mac user with a portable flash&amp;nbsp;disk.&lt;/p&gt;
&lt;h2&gt;How filesystems&amp;nbsp;work&lt;/h2&gt;
&lt;p&gt;Intuitively, we might imagine that the metadata for each file gets stored with its data. This is like storing the shipment details of each package with the package itself. But when you need to find a particular shipment in the warehouse, you can’t be checking every single&amp;nbsp;rack!&lt;/p&gt;
&lt;p&gt;Instead, you would store shipment records in a master file, usually in some kind of separate office, which would list the shipment details alongside the location of the cargo (e.g. by rack number and level). The equivalent of this master file on a filesystem is the &lt;strong&gt;file table&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In &lt;span class="caps"&gt;NTFS&lt;/span&gt;, this is the Master File Table (&lt;span class="caps"&gt;MFT&lt;/span&gt;). The &lt;span class="caps"&gt;MFT&lt;/span&gt; lists all the files on the disk (by their full path,&amp;nbsp;e.g. &lt;code&gt;C:\Windows\system32\notepad.exe&lt;/code&gt;), along with its metadata. This makes it easy for Windows to show you the data quickly when you open any folder in File Explorer; it can get all this data from the &lt;span class="caps"&gt;MFT&lt;/span&gt;&amp;nbsp;easily!&lt;/p&gt;
&lt;p&gt;Sounds peachy. What can go wrong with this&amp;nbsp;picture?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; A hard disk is organised into sectors, which are the smallest unit of storage. The &lt;span class="caps"&gt;OS&lt;/span&gt;’s filesystem determines how and where to store each file on the hard disk. The filesystem manages the file metadata in a file table, separate from the actual contents of the&amp;nbsp;file.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 107: The challenges of&amp;nbsp;storage&lt;/p&gt;
&lt;p&gt;This is where I talk about all the ways we make life difficult for computers, but easier for ourselves. Like unplugging &lt;span class="caps"&gt;USB&lt;/span&gt; drives before removing them safely&amp;nbsp;…&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;&lt;del&gt;How do apps know where a file starts and ends? [Issue 49]&lt;/del&gt;&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;li&gt;why does computer memory exist when apps can read directly from the hard disk? [Issue&amp;nbsp;105]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 09"></category><category term="operating system"></category></entry><entry><title>Issue 105: Operating Systems</title><link href="https://ngjunsiang.github.io/laymansguide/issue105.html" rel="alternate"></link><published>2021-01-30T08:00:00+08:00</published><updated>2021-01-30T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-01-30:/laymansguide/issue105.html</id><summary type="html">&lt;p&gt;The &lt;span class="caps"&gt;OS&lt;/span&gt; takes care of booting up, login and user management, window management, memory allocation, storage interfaces, background services, peripheral management, and much more. Access to these services, where allowed, is provided in the form of software libraries that developers can&amp;nbsp;use.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Shared secrets allow secured access to resources, such as databases or other services. These shared secrets are typically kept on a server controlled by the app developer. For mobile apps, they are usually stored with the operating system, inaccessible to other&amp;nbsp;apps.&lt;/p&gt;
&lt;p&gt;Last season, I explained the differences between web apps, mobile apps, and laptop apps. Web apps operate in a browser environment, mobile apps operate in a mobile operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;) environment, and laptop apps operate in a desktop &lt;span class="caps"&gt;OS&lt;/span&gt;&amp;nbsp;environment.&lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;OS&lt;/span&gt; is that complex piece of software between the apps and the hardware that runs it all. The hardware speaks its own special code, while apps are run in machine code written for the &lt;span class="caps"&gt;CPU&lt;/span&gt;. The &lt;span class="caps"&gt;OS&lt;/span&gt; makes sure that everything runs&amp;nbsp;smoothly.&lt;/p&gt;
&lt;p&gt;What does &lt;strong&gt;everything&lt;/strong&gt;&amp;nbsp;entail?&lt;/p&gt;
&lt;h2&gt;What an operating system&amp;nbsp;does&lt;/h2&gt;
&lt;h3&gt;1.&amp;nbsp;Boot-up&lt;/h3&gt;
&lt;p&gt;When the machine is powered on, how does it know what hardware is available? This hardware discovery is done by the bootloader (which for the purposes of explanation we can consider as part of the &lt;span class="caps"&gt;OS&lt;/span&gt;). It checks for the available hardware resources, and compiles a list of them for the operating system. Added some memory? Added a new hard drive? The bootloader should detect&amp;nbsp;it.&lt;/p&gt;
&lt;h3&gt;2. Login and user&amp;nbsp;management&lt;/h3&gt;
&lt;p&gt;While a personal computer (&lt;span class="caps"&gt;PC&lt;/span&gt;) might have only one user, many PCs are used by more than one person. Multiple login accounts allow each user to have their own space on the computer for their&amp;nbsp;files.&lt;/p&gt;
&lt;p&gt;In addition to the user accounts, an operating system also creates multiple system accounts to manage services on the &lt;span class="caps"&gt;OS&lt;/span&gt;.&lt;/p&gt;
&lt;h3&gt;3. Window&amp;nbsp;management&lt;/h3&gt;
&lt;p&gt;Ever wondered how apps on a &lt;span class="caps"&gt;PC&lt;/span&gt; can have their windows styled so similarly? That’s because the &lt;span class="caps"&gt;OS&lt;/span&gt; provides a standardised window style for apps, with standardised actions that can be performed (maximise, minimise, close, resize, …). Apps can choose not to use this standardised window style, but then they are on their own when it comes to window styling, and they will have to write their own code for all of these&amp;nbsp;actions.&lt;/p&gt;
&lt;h3&gt;4. Memory allocation and&amp;nbsp;deallocation&lt;/h3&gt;
&lt;p&gt;Applications primarily run off computer memory, which is a hundred times faster than the hard disk (I’ll explain more in a future season). When an application is first launched, it is allocated a small amount of memory space for its data. If it wants more, it can’t just reach for the memory space and grab more chunks as needed; other apps are using memory as well, and it might inadvertently overwrite data stored by other&amp;nbsp;apps.&lt;/p&gt;
&lt;p&gt;So instead, access to memory is mediated by the &lt;span class="caps"&gt;OS&lt;/span&gt;. All requests for more memory are sent to the &lt;span class="caps"&gt;OS&lt;/span&gt;, which will return addresses for available chunks of memory. And any memory that the app frees up is released back into the common pool, for use by other&amp;nbsp;apps.&lt;/p&gt;
&lt;h3&gt;5. Storage&amp;nbsp;interfaces&lt;/h3&gt;
&lt;p&gt;Applications do not need to know how the user has their computer set up: what kind of disks they have, or which disk they put the &lt;span class="caps"&gt;OS&lt;/span&gt; on. But what if they need to allow the user to save a file, or load an existing file? The &lt;span class="caps"&gt;OS&lt;/span&gt; provides standard interfaces for doing so—you would likely have seen the “Select A File” interface on any &lt;span class="caps"&gt;OS&lt;/span&gt;. The &lt;span class="caps"&gt;OS&lt;/span&gt; takes care of file reading and writing, sparing the application developers from having to worry about the&amp;nbsp;details.&lt;/p&gt;
&lt;h3&gt;6. Background&amp;nbsp;services&lt;/h3&gt;
&lt;p&gt;Applications run by the user typically expect input only from one user; if you are running Microsoft Word at the same time as your sister, each instance of Microsoft Word expects to interact with either you or sister. However, there are special system applications that often have to deal with input from multiple users. For example, antivirus software or Windows Printer Management need to run for all users at the same time, and should not be terminated like a typical&amp;nbsp;program.&lt;/p&gt;
&lt;p&gt;These applications thus run differently: they run as background services, which are special applications which do not interact with the desktop. Background services are managed differently from applications, and typically can only be terminated by the system&amp;nbsp;administrator.&lt;/p&gt;
&lt;h3&gt;7. Peripheral&amp;nbsp;management&lt;/h3&gt;
&lt;p&gt;Do you use Bluetooth wireless headphones? &lt;span class="caps"&gt;USB&lt;/span&gt; devices of any sort? Did you just buy a new gamepad? The &lt;span class="caps"&gt;OS&lt;/span&gt; detects them and takes care of driver installation (usually). You can see all detected devices and installed drivers in the Device Manager, under Control Panel, in Windows&amp;nbsp;10.&lt;/p&gt;
&lt;p&gt;I’ve just covered the main ones that most users would use on a regular basis. The &lt;span class="caps"&gt;OS&lt;/span&gt; manages much more stuff, including &lt;span class="caps"&gt;OS&lt;/span&gt; updates, per-user settings, etc—remember the Registry Editor? (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue100.html"&gt;Issue 100&lt;/a&gt;)) For a look at what else the &lt;span class="caps"&gt;OS&lt;/span&gt; manages, take a peek in the Control Panel or System Preferences of your &lt;span class="caps"&gt;OS&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;These form a supporting network of services that applications can draw on. Instead of having to work out the full details of implementation for each of these features, they can use the &lt;span class="caps"&gt;OS&lt;/span&gt;’s provided functions to do the work for them. These functions are typically bundled in a standard library (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue017.html"&gt;Issue 17&lt;/a&gt;)) provided by the &lt;span class="caps"&gt;OS&lt;/span&gt; manufacturer. For example, &lt;a href="https://dotnet.microsoft.com/"&gt;.&lt;span class="caps"&gt;NET&lt;/span&gt;&lt;/a&gt; is a collection of libraries for Windows applications that developers can use to simplify their&amp;nbsp;work.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; The &lt;span class="caps"&gt;OS&lt;/span&gt; takes care of booting up, login and user management, window management, memory allocation, storage interfaces, background services, peripheral management, and much more. Access to these services, where allowed, is provided in the form of software libraries that developers can&amp;nbsp;use.&lt;/p&gt;
&lt;p&gt;I’ve touched on per-user data storage in Season 8, and on memory allocation in Season 5, so I will skip those for the rest of this season. Window management and background services isn’t going to be interesting for a layman’s guide, so let’s skip those too. Instead, I will try to set the stage for a next season about hardware, by explaining more about how storage and computer peripherals work, and how the operating system simplifies access to them. and then I’ll get to booting up—how a computer gets ready from the moment you press the power&amp;nbsp;button.&lt;/p&gt;
&lt;p&gt;If I still have issues left after that, I might geek out a bit and talk about fonts&amp;nbsp;;)&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 106: Organising&amp;nbsp;storage&lt;/p&gt;
&lt;p&gt;First up: How exactly is data organised in the hard disk? How does an operating system manage it&amp;nbsp;all?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;li&gt;why does computer memory exist when apps can read directly from the hard disk? [Issue&amp;nbsp;105]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 09"></category><category term="memory"></category><category term="operating system"></category></entry><entry><title>Issue 104: Storing sensitive data</title><link href="https://ngjunsiang.github.io/laymansguide/issue104.html" rel="alternate"></link><published>2021-01-23T08:00:00+08:00</published><updated>2021-01-23T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-01-23:/laymansguide/issue104.html</id><summary type="html"></summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; A race condition happens when threads depend on instructions happening with coincidental timing for success. When instructions are not executed with appropriate timing, one or more threads can get stuck waiting on a response that never&amp;nbsp;comes.&lt;/p&gt;
&lt;p&gt;To wrap up this season on how apps work, I’m going to try answering a question I had on my mind as I was still new to computers: where do my secrets get stored? If I don’t want them to be stored, what are my&amp;nbsp;options?&lt;/p&gt;
&lt;p&gt;I will answer that &lt;em&gt;from an app’s perspective&lt;/em&gt; in this&amp;nbsp;issue.&lt;/p&gt;
&lt;h2&gt;Why would I want to keep secrets from my&amp;nbsp;users?&lt;/h2&gt;
&lt;p&gt;You just wrote an app. Your app syncs data to a cloud database (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue090.html"&gt;Issue 90&lt;/a&gt;)). But the cloud database has many other app developers using it as well—how would it know it is you and not some other malicious hacker? It recognises you via a &lt;strong&gt;shared secret&lt;/strong&gt;: a token or an &lt;span class="caps"&gt;API&lt;/span&gt; key that you can see after you log in to your dashboard on their&amp;nbsp;website.&lt;/p&gt;
&lt;p&gt;After your users install the app, every request sent by your app to the cloud database has to be authenticated using this &lt;em&gt;shared secret&lt;/em&gt;. That means you are going to have to get this shared secret onto the app somehow. But this has to happen without the user being able to see it or access it, otherwise a savvy user could use that key to gain access to your cloud&amp;nbsp;database.&lt;/p&gt;
&lt;h2&gt;Storing secrets on the&amp;nbsp;web&lt;/h2&gt;
&lt;p&gt;The code that is loaded by the user’s browser runs under their control, so putting the shared secret anywhere in that code is a bad idea. Any savvy user who knows how to view the script’s source can potentially find&amp;nbsp;it!&lt;/p&gt;
&lt;p&gt;A much safer option is to store the secret with the code that runs on &lt;em&gt;your server&lt;/em&gt;. But not in the server’s source code! If you are most developers, you would be using some kind of version control system (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue019.html"&gt;Issue 19&lt;/a&gt;)) that maintains a copy of your source code and all its changes. If you are using Github or some other public platform for this, you have to be very careful that the shared secret is not visible (or otherwise guessable) just by reading the source&amp;nbsp;code.&lt;/p&gt;
&lt;p&gt;For a simple shared secret, such as a short string of characters, app developers usually use &lt;strong&gt;environment variables&lt;/strong&gt;. These are pieces of information that are kept in memory only, accessible by the app, and are set by the operating system whenever the app starts up. The server where you run your code will let you configure the environment variables that your app needs, keeping them out of sight of the&amp;nbsp;users.&lt;/p&gt;
&lt;h2&gt;Storing secrets in a mobile&amp;nbsp;app&lt;/h2&gt;
&lt;p&gt;Mobile apps are supported by a host of services provided by the operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;), typically managed by Google or Apple. They each offer a way for you to store a shared secret with the &lt;span class="caps"&gt;OS&lt;/span&gt;. Your app can use this shared secret from the &lt;span class="caps"&gt;OS&lt;/span&gt; to encrypt information so that other apps are not able to access it. When your app starts up, it requests the secret from the &lt;span class="caps"&gt;OS&lt;/span&gt;, and uses it to decrypt the secret&amp;nbsp;again.&lt;/p&gt;
&lt;h2&gt;Storing secrets in a laptop&amp;nbsp;app&lt;/h2&gt;
&lt;p&gt;If you are developing a laptop app that does not rely on a connection to your server (i.e. a “standalone” app), your options are somewhat more limited. Since all your app code and resources will be in the user’s machine and thus accessible to the user, your best bet is to find some way to obfuscate it and hope no one finds it&amp;nbsp;easily.&lt;/p&gt;
&lt;p&gt;This is one reason why so many apps require an online connection: it is much easier to hide secrets on a machine you own and control! With a server connection, you can require the app to retrieve the secret from the server, and delete the temporary copy of the secret after&amp;nbsp;use.&lt;/p&gt;
&lt;h2&gt;Storing&amp;nbsp;passwords&lt;/h2&gt;
&lt;p&gt;If your users log in with an email and password (which is almost every online service ever), you don‘t actually store their passwords; that is terrible security practice, even if you do it in a database! A nifty piece of software technology, known as a &lt;strong&gt;hash function&lt;/strong&gt;, takes that password (regardless of length) and turns it into a unique&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt; &lt;strong&gt;hash&lt;/strong&gt; with a fixed number of&amp;nbsp;characters.&lt;/p&gt;
&lt;p&gt;Examples of&amp;nbsp;hashes:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="mf"&gt;661&lt;/span&gt;&lt;span class="n"&gt;c425549bc70b98e908325b8c64f82&lt;/span&gt;
&lt;span class="mf"&gt;056&lt;/span&gt;&lt;span class="n"&gt;cd6eb540ace37e64572c64c778d45&lt;/span&gt;
&lt;span class="mf"&gt;239&lt;/span&gt;&lt;span class="n"&gt;b1ddbb45caf82408cb89f13816185&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What you do, then, is to store the password &lt;em&gt;hash&lt;/em&gt; instead of the password. When a user sends a username and password, you hash their attempted password, compare it with the stored hash, and see if they are the&amp;nbsp;same.&lt;/p&gt;
&lt;p&gt;The hashes are designed to be difficult to reverse. The state-of-the-art algorithm used today can generate hashes that would take millions of years to reverse using hardware currently available. But there are techniques that can reverse hashes of some older algorithms in as little as 30 minutes, so if you are a developer, please find out which one to&amp;nbsp;use!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue&amp;nbsp;summary:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Shared secrets allow secured access to resources, such as databases or other services. These shared secrets are typically kept on a server controlled by the app developer. For mobile apps, they are usually stored with the operating system, inaccessible to other&amp;nbsp;apps.&lt;/p&gt;
&lt;p&gt;Phew, we had enough issues here to cover the main parts. And I managed to answer one of the sometime-in-the-future questions! Actually, I also answered another one on software installation earlier, in issues &lt;a href="https://ngjunsiang.github.io/laymansguide/issue099.html"&gt;99&lt;/a&gt;) and &lt;a href="https://ngjunsiang.github.io/laymansguide/issue100.html"&gt;100&lt;/a&gt;), so I’m going to go ahead and strike it&amp;nbsp;off.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S9] Issue 105: Operating&amp;nbsp;Systems&lt;/p&gt;
&lt;p&gt;This wraps up another season of Layman’s Guide on how apps work. Next season, I am going to zoom out and look at the environment that apps operate in: the operating system. Yep, I’m going to tackle the most complex pieces of software ever to be written, and try to explain them in terms that laypeople can understand&amp;nbsp;😅&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;&lt;del&gt;involved in installing a piece of software? [Issue 48]&lt;/del&gt;&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;&lt;del&gt;a password hash? [Issue 63]&lt;/del&gt;&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;In practice, there is a very low chance that two different passwords may end up giving the same hash. This technology is still being improved!&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 08"></category><category term="app"></category></entry><entry><title>Issue 103: Why apps hang even with multiple threads</title><link href="https://ngjunsiang.github.io/laymansguide/issue103.html" rel="alternate"></link><published>2021-01-16T08:00:00+08:00</published><updated>2021-01-16T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-01-16:/laymansguide/issue103.html</id><summary type="html">&lt;p&gt;A race condition happens when threads depend on instructions happening with coincidental timing for success. When instructions are not executed with appropriate timing, one or more threads can get stuck waiting on a response that never&amp;nbsp;comes.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Applications are assigned a thread by the &lt;span class="caps"&gt;OS&lt;/span&gt; for running a sequence of instructions. The instructions are executed sequentially, and the app cannot proceed if it gets stuck on any&amp;nbsp;instruction.&lt;/p&gt;
&lt;h2&gt;Multithreading&lt;/h2&gt;
&lt;p&gt;An app can hang if its sole thread gets stuck. In some cases, an app can be written to make use of multiple threads. This is possible when a computer has more than one processing core, or if an operating system is designed to divide computing time among multiple&amp;nbsp;threads.&lt;/p&gt;
&lt;h2&gt;Race&amp;nbsp;conditions&lt;/h2&gt;
&lt;p&gt;Trying to design apps to use multiple threads is hard! Apps running into an error is one thing; a more subtle form of failure is known as a &lt;strong&gt;race condition&lt;/strong&gt;. This happens when the success of two or more tasks depend on near-perfect timing which the threads have little control&amp;nbsp;over.&lt;/p&gt;
&lt;h2&gt;Main and secondary&amp;nbsp;threads&lt;/h2&gt;
&lt;p&gt;A common pattern is to have the app’s graphical interface and main code run in its own thread, with any subsidiary tasks (such as opening files) running in a secondary thread. If the task in the secondary thread is taking too long, the main thread can still issue instructions to terminate the secondary thread’s task, and thereby restore order and&amp;nbsp;control.&lt;/p&gt;
&lt;p&gt;Sounds fair enough. How might this&amp;nbsp;fail?&lt;/p&gt;
&lt;p&gt;Lets take an example: the main thread has a task that involves sending a signal to secondary thread, and then waiting for a response from it. Secondary thread has a task that involves sending a signal to main thread, and then waiting for a response from it. Both tasks complete successfully when they are carried out independently. But what if the main and secondary threads both run those tasks near-simultaneously, before the other thread has a chance to respond? They both get stuck waiting for a response. The app has just&amp;nbsp;hung!&lt;/p&gt;
&lt;h2&gt;Multiple worker&amp;nbsp;threads&lt;/h2&gt;
&lt;p&gt;Another pattern is to split the job up into multiple parts, and have multiple threads each take a part of the job. When they have all completed, the completed parts are then stitched back together into the finished&amp;nbsp;result.&lt;/p&gt;
&lt;p&gt;But this has its own ways of failing&amp;nbsp;too.&lt;/p&gt;
&lt;p&gt;The threads have to coordinate their job status, and often do so by updating a common set of data. Thread 1 might request access to that data to update it. To ensure that the data doesn’t change before it is done, it will usually request a lock (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue082.html"&gt;Issue 82&lt;/a&gt;)) on it, to prevent other threads from modifying it at the same&amp;nbsp;time.&lt;/p&gt;
&lt;p&gt;One way this can fail in practice is if two or more worker threads request a lock simultaneously. They both get a lock, because at the moment their requests are processed, nothing else has locked the resource. But now they can’t proceed to modify the data because it has been locked by another thread that isn’t&amp;nbsp;them.&lt;/p&gt;
&lt;p&gt;This situation is known as a &lt;strong&gt;deadlock&lt;/strong&gt;. This and similar situations are just one out of many ways that apps can&amp;nbsp;hang.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; A race condition happens when threads depend on instructions happening with coincidental timing for success. When instructions are not executed with appropriate timing, one or more threads can get stuck waiting on a response that never&amp;nbsp;comes.&lt;/p&gt;
&lt;p&gt;Yep, multithreaded programming is&amp;nbsp;hard.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 104: Storing sensitive&amp;nbsp;data&lt;/p&gt;
&lt;p&gt;To wrap up this season on apps, I’ll look at one last question: how do apps keep our data&amp;nbsp;secure&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 08"></category><category term="app"></category><category term="operating system"></category></entry><entry><title>Issue 102: Threading</title><link href="https://ngjunsiang.github.io/laymansguide/issue102.html" rel="alternate"></link><published>2021-01-09T08:00:00+08:00</published><updated>2021-01-09T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-01-09:/laymansguide/issue102.html</id><summary type="html">&lt;p&gt;Applications are assigned a thread by the &lt;span class="caps"&gt;OS&lt;/span&gt; for running a sequence of instructions. The instructions are executed sequentially, and the app cannot proceed if it gets stuck on any&amp;nbsp;instruction.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; An app crashes when it encounters a situation it can’t handle, or when it attempts to perform an operation that is disallowed by the operating&amp;nbsp;system.&lt;/p&gt;
&lt;p&gt;This post is a prelude to talking about app hangs. Hangs are both simple yet complicated to talk about, but there’s a piece of the puzzle that has to come into the picture first. That piece is about how apps&amp;nbsp;work.&lt;/p&gt;
&lt;p&gt;In a computer, the operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;) has to coordinate the requested actions of so many different apps. How does it know which action came from which&amp;nbsp;app?&lt;/p&gt;
&lt;p&gt;Through a mechanism known as&amp;nbsp;threads.&lt;/p&gt;
&lt;h2&gt;Threading&lt;/h2&gt;
&lt;p&gt;When you run an app, the &lt;span class="caps"&gt;OS&lt;/span&gt; creates a separate thread. A thread is a sequence of programmed instructions, like a thread of thought. Or a thread of bureaucracy. The &lt;span class="caps"&gt;OS&lt;/span&gt; completes each instruction in the thread, and if it gets stuck on any single task, it cannot move&amp;nbsp;on.&lt;/p&gt;
&lt;p&gt;Sometimes, this is good and necessary, like when you need input from the user (don’t be doing anything else until I tell you what I need!). Other times, it is unnecessary&amp;nbsp;waiting.&lt;/p&gt;
&lt;h2&gt;What causes threads to get&amp;nbsp;stuck?&lt;/h2&gt;
&lt;p&gt;Some of us really hate math, but not computers! The math is hardly ever what causes threads to&amp;nbsp;stop.&lt;/p&gt;
&lt;p&gt;Like in the workplace, it is often other &lt;del&gt;people&lt;/del&gt; devices.&lt;/p&gt;
&lt;p&gt;When an app (running in a thread) tries to open a file to read data from it, the operating system has to look up the virtual memory  address (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue055.html"&gt;Issue 55&lt;/a&gt;)), follow it to the hard disk or solid state disk, and then wait for the disk to respond with the&amp;nbsp;data.&lt;/p&gt;
&lt;p&gt;And in that moment, &lt;em&gt;lots&lt;/em&gt; of things can go&amp;nbsp;wrong.&lt;/p&gt;
&lt;p&gt;If the disk is failing, and unable to read the sector where the data resides, it will usually keep attempting to do so. Meanwhile, back in the operating system, the thread is stuck. It cannot move on, because the previous instruction to open the file has not completed. It can’t even decide to abort the currently-running instruction—telling the app to stop &lt;strong&gt;is already another instruction&lt;/strong&gt; which has to&amp;nbsp;wait!&lt;/p&gt;
&lt;p&gt;The only thing to do now is wait for the &lt;span class="caps"&gt;OS&lt;/span&gt; to realise that this thread is taking too long to do its thing, and forcibly terminate the thread. This is known as a thread &lt;strong&gt;timeout&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Is there any way to work around this? Yep! The termination instruction has to come from a separate thread. This means the app has to run multiple&amp;nbsp;threads.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Applications are assigned a thread by the &lt;span class="caps"&gt;OS&lt;/span&gt; for running a sequence of instructions. The instructions are executed sequentially, and the app cannot proceed if it gets stuck on any&amp;nbsp;instruction.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 103: Why apps hang even with multiple&amp;nbsp;threads&lt;/p&gt;
&lt;p&gt;Processors today already have multiple cores, and many apps can already run on multiple threads. Why do they still hang? I’ll answer this in the next issue&amp;nbsp;:)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 08"></category><category term="app"></category><category term="operating system"></category></entry><entry><title>Issue 101: Why apps crash</title><link href="https://ngjunsiang.github.io/laymansguide/issue101.html" rel="alternate"></link><published>2021-01-02T08:00:00+08:00</published><updated>2021-01-02T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2021-01-02:/laymansguide/issue101.html</id><summary type="html">&lt;p&gt;An app crashes when it encounters a situation it can’t handle, or when it attempts to perform an operation that is disallowed by the operating&amp;nbsp;system.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Windows systems categorise data into two types: files, and settings. Files are stored under an appropriate subfolder&amp;nbsp;in &lt;code&gt;C:\&lt;/code&gt;, while other storage devices and network locations are stored elsewhere or given their own drive letters. Settings are managed through the Windows Registry, which is stored&amp;nbsp;in &lt;code&gt;C:\Windows\System32\Config\&lt;/code&gt; and &lt;code&gt;C:\Windows\Users\Name\&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Besides general slowness, two of the most frustrating experiences we have with computers is when they crash, and when they&amp;nbsp;hang.&lt;/p&gt;
&lt;p&gt;You mean there’s a difference? Sometimes we use the two terms interchangeably, but they are really not the&amp;nbsp;same.&lt;/p&gt;
&lt;p&gt;Remember: applications are just a list of computer instructions telling the computer what to do: where to get the data, how to process it, and what to return. When the instructions make perfect sense, everything goes well. But sometimes they&amp;nbsp;don’t.&lt;/p&gt;
&lt;h2&gt;Crashing&lt;/h2&gt;
&lt;p&gt;A crash happens when the app receives &lt;strong&gt;(a)&lt;/strong&gt; a response that it does not know how to handle, or &lt;strong&gt;(b)&lt;/strong&gt; is not allowed to carry&amp;nbsp;out.&lt;/p&gt;
&lt;h3&gt;Unhandled&amp;nbsp;responses&lt;/h3&gt;
&lt;p&gt;A common error made by many programming newbies (including me) is failing to account for all the ways that things can go wrong. For example, if I am writing a simple app to read a text file and perform some calculations, an obvious step in the app is sending a request to the operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;) to open the text&amp;nbsp;file.&lt;/p&gt;
&lt;p&gt;Even that simple step is fraught with many possible failures! The text file may have been locked by another app (which is writing data to the file), or the user running the app might not have permission to open the file (especially if it is in another user’s home directory), or&amp;nbsp;&amp;#8230;&lt;/p&gt;
&lt;p&gt;Well, a whole bunch of things can go wrong. And when they do, the &lt;span class="caps"&gt;OS&lt;/span&gt; throws an error. If the app does not have any code to handle that error &amp;#8230; game over, it cannot proceed and it crashes&amp;nbsp;abruptly.&lt;/p&gt;
&lt;p&gt;This is a lot more common than you think, even for experienced programmers, especially when a process that isn’t expected to throw an error actually does it. And sometimes it just can’t be helped: when your computer runs out of memory, and an app requests for more memory but doesn’t get it, and it just cant go on without that memory &amp;#8230; it&amp;nbsp;crashes.&lt;/p&gt;
&lt;h3&gt;Illegal&amp;nbsp;instructions&lt;/h3&gt;
&lt;p&gt;Memory in the computer is managed by the &lt;span class="caps"&gt;OS&lt;/span&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue065.html"&gt;Issue 65&lt;/a&gt;)), which partitions it into different zones. The memory used by &lt;span class="caps"&gt;OS&lt;/span&gt; processes is protected from access by other apps (for your privacy and protection), and memory used by an app cannot be used by another app, unless it is shared memory&amp;nbsp;space.&lt;/p&gt;
&lt;p&gt;So when an app sends an instruction requesting to access memory space it does not have authorisation to, or when the &lt;span class="caps"&gt;OS&lt;/span&gt; itself attempts to access an address that it can’t (especially addresses that point to hardware devices) &amp;#8230; it crashes. An app crash just brings you back to your desktop, but an &lt;span class="caps"&gt;OS&lt;/span&gt; crash usually leads to the famous Blue Screen Of Death (&lt;span class="caps"&gt;BSOD&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;These days, OSes are better at handling crashes. If the crash occurs in the window management system (the part that lets apps create windows on screen and icons in the taskbar), Windows can often just restart it without restarting or touching the rest of the &lt;span class="caps"&gt;OS&lt;/span&gt;. But if it happens in a critical part that can’t be restarted by itself, then &amp;#8230; &lt;span class="caps"&gt;BSOD&lt;/span&gt;&amp;nbsp;:)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; An app crashes when it encounters a situation it can’t handle, or when it attempts to perform an operation that is disallowed by the operating&amp;nbsp;system.&lt;/p&gt;
&lt;p&gt;Definitely oversimplified for ease of understanding, but I see no point going into the technical details unless a future issue calls for&amp;nbsp;it.&lt;/p&gt;
&lt;p&gt;Before going into app hangs, I’ll need to talk about threads first. If you have heard of multithreading before, yep I am going to talk about that next&amp;nbsp;issue!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 102:&amp;nbsp;Threading&lt;/p&gt;
&lt;p&gt;“Many hands make light work” is true for computers as well, and I’ll go into more detail about how a computer uses its many hands to speed up the work it does :) Before that, let’s examine the simple case of an app doing only one thing at a time: the single-threaded&amp;nbsp;app.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 08"></category><category term="app"></category><category term="memory"></category><category term="operating system"></category></entry><entry><title>Issue 100: Where does all the app data go? A look at Windows systems</title><link href="https://ngjunsiang.github.io/laymansguide/issue100.html" rel="alternate"></link><published>2020-12-26T08:00:00+08:00</published><updated>2020-12-26T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-12-26:/laymansguide/issue100.html</id><summary type="html">&lt;p&gt;Windows systems categorise data into two types: files, and settings. Files are stored under an appropriate subfolder&amp;nbsp;in &lt;code&gt;C:\&lt;/code&gt;, while other storage devices and network locations are stored elsewhere or given their own drive letters. Settings are managed through the Windows Registry, which is stored&amp;nbsp;in &lt;code&gt;C:\Windows\System32\Config\&lt;/code&gt; and &lt;code&gt;C:\Windows\Users\Name\&lt;/code&gt;.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; MacOS, Linux, and other similar systems treat everything as a file, organised into appropriate&amp;nbsp;subfolders.&lt;/p&gt;
&lt;p&gt;Previous issue: Mac- and Linux-like systems. Since the key points are so short, let’s&amp;nbsp;summarise:&lt;/p&gt;
&lt;p&gt;On Mac-like systems, the top-level folders&amp;nbsp;are&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;an &lt;code&gt;/Applications&lt;/code&gt; folder for&amp;nbsp;apps&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;/Library&lt;/code&gt; folder for shared files (see &lt;a href="https://ngjunsiang.github.io/laymansguide/issue017.html"&gt;Issue 17&lt;/a&gt;), but on Mac this extends to things like sounds, profile pics, colors,&amp;nbsp;&amp;#8230;)&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;/System&lt;/code&gt; folder for, well, you know&amp;nbsp;what.&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;/Network&lt;/code&gt; folder for accessing resources on the network (such as shared&amp;nbsp;folders)&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;/Users&lt;/code&gt; folder for accessing user folders and&amp;nbsp;files&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;/Drives&lt;/code&gt; folder for accessing other storage devices (e.g. &lt;span class="caps"&gt;USB&lt;/span&gt;&amp;nbsp;drives)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Linux systems are similarly divided, but into differently named&amp;nbsp;folders.&lt;/p&gt;
&lt;p&gt;This issue: (A rant on)&amp;nbsp;Windows.&lt;/p&gt;
&lt;h2&gt;Windows-like systems (who am I kidding, there’s only&amp;nbsp;Windows)&lt;/h2&gt;
&lt;p&gt;Unlike Mac-like systems, where all data comes in the form of a file, Windows systems recognise two types of data: settings, and&amp;nbsp;files.&lt;/p&gt;
&lt;h3&gt;Files in Windows&amp;nbsp;systems&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Apps get put&amp;nbsp;in &lt;code&gt;C:\Program Files&lt;/code&gt; or &lt;code&gt;C:\Program Files (x86)&lt;/code&gt;, for 64-bit and 32-bit programs respectively&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;li&gt;Library? Shared files? Ha! (I’ll talk about this further&amp;nbsp;down)&lt;/li&gt;
&lt;li&gt;System files go&amp;nbsp;into &lt;code&gt;C:\Windows&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Network resources, well &amp;#8230; don’t really have a &amp;#8230; well they are a different category of location that does not start with a drive letter and instead starts&amp;nbsp;with &lt;code&gt;\\&lt;/code&gt;, unless you assign these locations to a drive letter, then they have a drive letter.&amp;nbsp;Sorta.&lt;/li&gt;
&lt;li&gt;User files go&amp;nbsp;into &lt;code&gt;C:\Users&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Other storage devices are auto-detected and assigned a drive letter, though not always&amp;nbsp;consistently.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And now we talk about&amp;nbsp;settings.&lt;/p&gt;
&lt;h3&gt;Settings in Windows&amp;nbsp;systems&lt;/h3&gt;
&lt;p&gt;Settings are stored in&amp;nbsp;the &lt;code&gt;C:\Windows\System32\Config\&lt;/code&gt; and &lt;code&gt;C:\Windows\Users\Name\&lt;/code&gt; folder, which technically makes them system files, which &amp;#8230; wait, how are apps supposed to access them&amp;nbsp;then?&lt;/p&gt;
&lt;p&gt;App developers are supposed to do it through a system library, which provides variables named&amp;nbsp;like &lt;code&gt;ApplicationData.LocalSettings&lt;/code&gt;, &lt;code&gt;ApplicationDataCompositeValue&lt;/code&gt;,&amp;nbsp;and &lt;code&gt;RoamingSettings&lt;/code&gt;. These variables let developers store and retrieve settings, which all end up stored in a system known as the &lt;strong&gt;Windows Registry&lt;/strong&gt;. And Administrators can edit them using something known as the Registry&amp;nbsp;Editor.&lt;/p&gt;
&lt;p&gt;The Windows Registry consists of 5 top-level areas (known as hives), each one beginning with the&amp;nbsp;word &lt;code&gt;HKEY_&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;HKEY_CLASSES_ROOT&lt;/code&gt; is for storing application settings, and file extension information (e.g. which app to use to open each type of file&amp;nbsp;extension)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HKEY_CURRENT_USER&lt;/code&gt; is for storing settings and configuration specific to the current (logged in)&amp;nbsp;user&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HKEY_LOCAL_MACHINE&lt;/code&gt; is for storing settings and configuration common to all users (e.g. default&amp;nbsp;settings)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HKEY_USERS&lt;/code&gt; is for storing settings and configuration of each user.&amp;nbsp;The &lt;code&gt;HKEY_CURRENT_USER&lt;/code&gt; data for all users is stored here, and copied&amp;nbsp;to &lt;code&gt;HKEY_CURRENT_USER&lt;/code&gt; when they log&amp;nbsp;in.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HKEY_CURRENT_CONFIG&lt;/code&gt; is for storing information about the computer’s configuration and&amp;nbsp;resources&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The usual way of finding out how to modify a particular setting for X is to google “registry setting for X” and proceed from&amp;nbsp;there.&lt;/p&gt;
&lt;p&gt;Logos, backgrounds, buttons, and other application data? They go&amp;nbsp;into &lt;code&gt;C:\Program Files&lt;/code&gt; (or &lt;code&gt;C:\Program Files (x86)&lt;/code&gt; if still 32-bit) for traditional Windows apps, or&amp;nbsp;into &lt;code&gt;C:\Program Files\WindowsApps&lt;/code&gt; for Windows App Store apps. What if other apps also need to use them? Then they go into &lt;del&gt;&lt;code&gt;C:\Library&lt;/code&gt;, just kidding, if only it were so easy&lt;/del&gt; &lt;code&gt;C:\Program Files\Common Files&lt;/code&gt;, but you’ll notice it’s pretty empty. Usually, they’ll be stored within the app’s folder, and you have to find out where to edit the Windows Registry so other programs know where to find them (apparently you can look&amp;nbsp;in &lt;code&gt;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs&lt;/code&gt;). Whoa, wait, what happened to apps not messing around in each other’s&amp;nbsp;folders—&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;OKAY&lt;/span&gt; &lt;span class="caps"&gt;MOVING&lt;/span&gt; &lt;span class="caps"&gt;ON&lt;/span&gt;—What if a user installs a program that they don’t want other users using? It goes&amp;nbsp;into &lt;code&gt;C:\Users\username\AppData\&lt;/code&gt;. User settings? They go into—nah, they don’t go into a file, they’re supposed to be settings so they go into the Windows Registry somewhere&amp;nbsp;under &lt;code&gt;HKEY_CURRENT_USER&lt;/code&gt;. Temporary files? They go&amp;nbsp;into &lt;code&gt;C:\Windows\Temp&lt;/code&gt;; isn’t that a system folder? Well yes, but if you put it&amp;nbsp;in &lt;code&gt;C:\Temp&lt;/code&gt; folks will complain and Disk Cleanup will not find&amp;nbsp;it.&lt;/p&gt;
&lt;p&gt;So to uninstall a Windows app, you run its uninstaller. Which may or may not work perfectly. Or it might remove its files but still appear in the Program List because it did a terrible job cleaning up its settings in Windows Registry. So you reinstall the program, this time using a third-party app that helps you track app installations and registry changes, so that it detects what new files/settings it creates, and then when you uninstall the program you do it through the third-party app so that it hopefully removes all traces once and for&amp;nbsp;all.&lt;/p&gt;
&lt;p&gt;Phew. And that’s all I&amp;nbsp;hope.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Windows systems categorise data into two types: files, and settings. Files are stored under an appropriate subfolder&amp;nbsp;in &lt;code&gt;C:\&lt;/code&gt;, while other storage devices and network locations are stored elsewhere or given their own drive letters. Settings are managed through the Windows Registry, which is stored&amp;nbsp;in &lt;code&gt;C:\Windows\System32\Config\&lt;/code&gt; and &lt;code&gt;C:\Windows\Users\Name\&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Okay so this ran much longer than I expected. In fact, it ran so long that I split it into two issues. I promised to explain computers as simply and jargon-free as possible, and I hope I have managed to do that. I am definitely biased, and that I do not apologise for, because this newsletter issue would be half its original length if *muttering* &lt;em&gt;some&lt;/em&gt; operating systems would just follow sensible principles that &lt;em&gt;other&lt;/em&gt; operating systems have no problem following&amp;nbsp;&amp;#8230;&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 101: Why do apps&amp;nbsp;crash?&lt;/p&gt;
&lt;p&gt;Moving on from app files and settings, the next few issues will explore common app problems. Coming up next issue: why do apps&amp;nbsp;crash?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;I really was hoping not to have to explain 32-bit vs 64-bit programs ever since &lt;a href="https://ngjunsiang.github.io/laymansguide/issue055.html"&gt;Issue 55&lt;/a&gt;), so for now let’s just say 32-bit programs are for 32-bit CPUs and 64-bit programs are for 64-bit CPUs. Unfortunately many old 32-bit apps have not caught up with the times and converted themselves to 64-bit apps, so Windows has to do hacky stuff to make old 32-bit apps work on modern 64-bit CPUs.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 08"></category><category term="app"></category></entry><entry><title>Issue 99: Where does all the app data go? A look at Mac-like systems</title><link href="https://ngjunsiang.github.io/laymansguide/issue099.html" rel="alternate"></link><published>2020-12-19T08:00:00+08:00</published><updated>2020-12-19T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-12-19:/laymansguide/issue099.html</id><summary type="html">&lt;p&gt;MacOS, Linux, and other similar systems treat everything as a file, organised into appropriate&amp;nbsp;subfolders.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Apps generally handle three categories of files: its own (permanent) app files, (shared) user files, and (ephemeral) temporary&amp;nbsp;files.&lt;/p&gt;
&lt;p&gt;What we are here to find out is: where do these apps keep their data, and how can we get rid of them (if we really want&amp;nbsp;to)?&lt;/p&gt;
&lt;p&gt;Web apps and mobile apps will not be discussed here, because they are much more heavily sandboxed, everything gets confined into the app’s little prison, and we generally don’t have these concerns when it comes to&amp;nbsp;them.&lt;/p&gt;
&lt;h2&gt;Just one little niggle&amp;nbsp;&amp;#8230;&lt;/h2&gt;
&lt;p&gt;I am so sorry to burden you with this otherwise unrelated information, but since there are a significant number of Windows users and a significant number of MacOS users, I had to bring this up at some&amp;nbsp;point.&lt;/p&gt;
&lt;p&gt;Windows and Mac manage this differently, so we are going to have to talk about two different kinds of systems. I will spend more time on the Windows system, because it needs more&amp;nbsp;time.&lt;/p&gt;
&lt;p&gt;Let’s get the easy one out of the way first: this issue deals with&amp;nbsp;&amp;#8230;&lt;/p&gt;
&lt;h2&gt;Mac-like systems&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/h2&gt;
&lt;p&gt;On MacOS (and Linux) systems, everything is a file. All files get stashed into some kind of&amp;nbsp;folder.&lt;/p&gt;
&lt;p&gt;I personally prefer this because you have everything sorted into sensible top-level folders&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt;. Mac&amp;nbsp;has:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;an &lt;code&gt;/Applications&lt;/code&gt; folder for&amp;nbsp;apps&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;/Library&lt;/code&gt; folder for shared files (see &lt;a href="https://ngjunsiang.github.io/laymansguide/issue017.html"&gt;Issue 17&lt;/a&gt;), but on Mac this extends to things like sounds, profile pics, colors,&amp;nbsp;&amp;#8230;)&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;/System&lt;/code&gt; folder for, well, you know&amp;nbsp;what.&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;/Network&lt;/code&gt; folder for accessing resources on the network (such as shared&amp;nbsp;folders)&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;/Users&lt;/code&gt; folder for accessing user folders and&amp;nbsp;files&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;/Drives&lt;/code&gt; folder for accessing other storage devices (e.g. &lt;span class="caps"&gt;USB&lt;/span&gt;&amp;nbsp;drives)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;and then, similar to Linux, it&amp;nbsp;has &lt;code&gt;/bin&lt;/code&gt;, &lt;code&gt;/etc&lt;/code&gt; and other weird-looking folders that we don’t need to worry about at this point. Just treat them similar to system files and try not to touch them. The apps that we install generally do not clutter up these folders&amp;nbsp;unnecessarily.&lt;/p&gt;
&lt;p&gt;Logos, backgrounds, buttons, and other application data? They go&amp;nbsp;into &lt;code&gt;/Applications&lt;/code&gt;. What if other apps also need to use them? Then they go&amp;nbsp;into &lt;code&gt;/Library&lt;/code&gt;. What if a user installs a program that they don’t want other users using? It goes&amp;nbsp;into &lt;code&gt;/Users/username/Applications&lt;/code&gt;. User settings? They go&amp;nbsp;into &lt;code&gt;/Users/username/Library&lt;/code&gt; (under a subfolder for the app). Temporary files? They go&amp;nbsp;into &lt;code&gt;/Library/Caches&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Linux systems are similarly divided, but into differently named folders. Everything is still a file, and belongs in some folder&amp;nbsp;somewhere.&lt;/p&gt;
&lt;p&gt;So to uninstall an app, you remove its files&amp;nbsp;from &lt;code&gt;/Applications&lt;/code&gt; or &lt;code&gt;/Users/username/Applications&lt;/code&gt; and&amp;nbsp;from &lt;code&gt;/Library&lt;/code&gt;, and that’s usually it. Apps are usually quite good at doing that themselves, so you don’t need to&amp;nbsp;worry.&lt;/p&gt;
&lt;p&gt;And then we deal with Windows systems in the next&amp;nbsp;issue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; MacOS, Linux, and other similar systems treat everything as a file, organised into appropriate&amp;nbsp;subfolders.&lt;/p&gt;
&lt;p&gt;I’m keeping this issue short because the next issue will be much longer. *Ominous music&amp;nbsp;plays*&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 100: Where does all the app data go? A look at Windows&amp;nbsp;systems&lt;/p&gt;
&lt;p&gt;The reasons for the difference between Mac-like systems and Windows systems is, again, historical, but I better prepare you because you are not going to like the next&amp;nbsp;issue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;The technical term is &amp;#8220;Unix-like systems&amp;#8221;, but we don’t need to know that, even if all the Unix fanfolks are pointing pitchforks at me now.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;There’s lots of things that make less sense once we get into more detail, but fortunately we don’t do that here.&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 08"></category><category term="app"></category></entry><entry><title>Issue 98: Temporary files</title><link href="https://ngjunsiang.github.io/laymansguide/issue098.html" rel="alternate"></link><published>2020-12-12T08:00:00+08:00</published><updated>2020-12-12T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-12-12:/laymansguide/issue098.html</id><summary type="html">&lt;p&gt;Apps generally handle three categories of files: its own (permanent) app files, (shared) user files, and (ephemeral) temporary&amp;nbsp;files.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; A laptop app can do practically anything, if it is running through the Administrator/root account. Sandboxing is carried out through permission&amp;nbsp;control.&lt;/p&gt;
&lt;p&gt;Thus far, I’ve summarised the salient differences between web apps, mobile apps, and laptop apps (my own terminology). I think we can move on to talking about their&amp;nbsp;similarities.&lt;/p&gt;
&lt;p&gt;There is a category of app that needs access to your device storage. It might be Youtube trying to download (part of) a stream onto your device for playback. Or it might be Tiktok trying to help you record a video for uploading. These apps need storage access so they can stash all the data into files, rather than hogging device memory with it. The same way we stash things into drawers and cabinets when we don’t need them, so they don’t clutter the space around us. And then we forget about them until we run out of space&amp;nbsp;😅&lt;/p&gt;
&lt;p&gt;You almost never see where those files appear. They get hidden &amp;#8230; &lt;em&gt;somewhere&lt;/em&gt; *gesticulates&amp;nbsp;around*.&lt;/p&gt;
&lt;h2&gt;Temporary&amp;nbsp;files&lt;/h2&gt;
&lt;p&gt;These files are short-lived; they typically don’t stick around for more than a few days. For that reason, they are known as &lt;strong&gt;temporary files&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In a webapp, the browser generally stores these files into one of its allocated folders, somewhere&amp;nbsp;in &lt;code&gt;C:\Users\[USERNAME]\AppData\Local\Google\Chrome\User Data\Default\Cache&lt;/code&gt; or the like. They get cleared when you clear your browser cache. On a mobile or laptop app, the operating system designates a space for temporary files,&amp;nbsp;in &lt;code&gt;C:\Windows\TEMP&lt;/code&gt; or &lt;code&gt;AppData\Local\Temp&lt;/code&gt; for Windows,&amp;nbsp;or &lt;code&gt;/tmp&lt;/code&gt; on Linux. You can clear those files through Disk Cleanup in&amp;nbsp;Windows.&lt;/p&gt;
&lt;p&gt;In general, temporary files are things you are not supposed to think about too much. The operating system has ways to clear them regularly. Apps are supposed to use these standardised locations to stash temporary files, and attempting to place them anywhere else is considered impolite, like leaving your stuff lying around in an office or otherwise public&amp;nbsp;space.&lt;/p&gt;
&lt;h2&gt;User&amp;nbsp;files&lt;/h2&gt;
&lt;p&gt;But then, sometimes the app produces useful data that you want to keep around! Your journal which you keep in a Word document, photos of your cat or dog, and the copious, copious video files&amp;nbsp;&amp;#8230;&lt;/p&gt;
&lt;p&gt;Webapps have no space for you to do that. You are just supposed to save them onto your phone or laptop; the browser has no way for different users to stash their own&amp;nbsp;files.&lt;/p&gt;
&lt;p&gt;Most smartphones assume they are going to be used by a single user, and you just stash those files directly into phone storage. Not the best system, but it is what it&amp;nbsp;is.&lt;/p&gt;
&lt;p&gt;Laptops are where it gets a bit more interesting. Most laptop operating systems (OSes) assume they might be used by multiple users, each on their own account (hence the login screen), and therefore allocate separate spaces where each user may keep their stuff, inaccessible to other users except Administrators (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue097.html"&gt;Issue 97&lt;/a&gt;)).&lt;/p&gt;
&lt;p&gt;On Windows, these users each have their own folder&amp;nbsp;in &lt;code&gt;C:\Users\&lt;/code&gt;; on MacOS, that’s&amp;nbsp;in &lt;code&gt;/Users/&lt;/code&gt;; on Linux, it’s&amp;nbsp;typically &lt;code&gt;/home/&lt;/code&gt;. (Don’t ask about&amp;nbsp;the &lt;code&gt;\&lt;/code&gt;s&amp;nbsp;vs &lt;code&gt;/&lt;/code&gt;s; its one of those things that’s just the way history happened and has no real technical reason behind&amp;nbsp;it.)&lt;/p&gt;
&lt;h2&gt;App&amp;nbsp;files&lt;/h2&gt;
&lt;p&gt;Of course, each app needs to have &lt;em&gt;its own space&lt;/em&gt; to keep &lt;em&gt;its own files&lt;/em&gt;, which allow it to do what it&amp;nbsp;does.&lt;/p&gt;
&lt;p&gt;Webapps get their own folder somewhere&amp;nbsp;in &lt;code&gt;C:\Users\[USERNAME]\AppData\Local\Google\Chrome\...&lt;/code&gt;, they can only see what is in that folder, and they cannot see what is in the parent folder, or sibling folders. It’s sandboxing, again (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue092.html"&gt;Issue 92&lt;/a&gt;))!&lt;/p&gt;
&lt;p&gt;Mobile apps get stored&amp;nbsp;into &lt;code&gt;/data/app&lt;/code&gt; or some similar folder, and you’re not supposed to think too hard about where, because of *handwaving* &lt;em&gt;sandboxing&lt;/em&gt;. The same idea applies: The app is not supposed to know, or be able to see, where other apps store their data! Eyes on the app’s own data only, and the user’s data (you did give it permission to access storage, right?), and any temporary data which it has&amp;nbsp;created.&lt;/p&gt;
&lt;p&gt;Laptop apps get stored&amp;nbsp;in &lt;code&gt;C:\Program Files&lt;/code&gt;, and interestingly enough have some kind of civil arrangement where they agree not to delete each others’ files, although antivirus programs have this passive-aggressive low-key thing where they like to mark each others’ program files as potential malware&amp;nbsp;*shrug*.&lt;/p&gt;
&lt;h2&gt;System&amp;nbsp;files&lt;/h2&gt;
&lt;p&gt;These files were around &lt;del&gt;in the beginning of time&lt;/del&gt; when the &lt;span class="caps"&gt;OS&lt;/span&gt; was installed; that means when you bought your laptop, they were already there, and any sensible system would prevent non-Administrators from mucking around with them. Windows stashes them&amp;nbsp;in &lt;code&gt;C:\Windows&lt;/code&gt;, while MacOS and Linux store them&amp;nbsp;in &lt;code&gt;/bin/&lt;/code&gt;, &lt;code&gt;/lib&lt;/code&gt;, and various similarly opaque&amp;nbsp;folders.&lt;/p&gt;
&lt;p&gt;I might go into more detail about these, possibly in a future season when I talk about operating systems, but for now we are done talking about categories of files.&amp;nbsp;Phew!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Apps generally handle three categories of files: its own (permanent) app files, (shared) user files, and (ephemeral) temporary&amp;nbsp;files.&lt;/p&gt;
&lt;p&gt;In reality, there are a whole bunch of different filetypes and other little details that apps need to worry about, but this is a newsletter for layfellas so let’s start&amp;nbsp;simple.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 99: Where does all the app data go? A look at Mac-like&amp;nbsp;systems&lt;/p&gt;
&lt;p&gt;I separated temporary files and user files in this issue. Temp files are files that come and go, like stray cats, while user files are shared with other apps as well and there’s really not very much that you can predict about them except hope users don’t do anything too crazy. System files are strictly off-limits so don’t even think about&amp;nbsp;that.&lt;/p&gt;
&lt;p&gt;But meanwhile, as an app developer, even after you exclude the above categories of files, there is still a whole bunch of questions you kinda need to worry about at some&amp;nbsp;point:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;where do I store my logos and backgrounds and buttons and other&amp;nbsp;data?&lt;/li&gt;
&lt;li&gt;where do I keep my&amp;nbsp;settings?&lt;/li&gt;
&lt;li&gt;where do I keep user&amp;nbsp;settings?&lt;/li&gt;
&lt;li&gt;if my program is meant to make a &lt;span class="caps"&gt;USB&lt;/span&gt; device useable, where do I drop the driver files? (Yep, that’s gonna need its own&amp;nbsp;issue)&lt;/li&gt;
&lt;li&gt;what if someone uninstalls my program but I want them to be able to keep their settings around in case they decide to reinstall and then it can feel exactly the way they left&amp;nbsp;off?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Yeah I have no idea who actually thinks about that last question either, but it’s something that seems to get asked in every software uninstallation&amp;nbsp;*shrug*.&lt;/p&gt;
&lt;p&gt;So let’s get into that in the next&amp;nbsp;issue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;li&gt;a driver file and why do I need one? [Issue&amp;nbsp;98]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 08"></category><category term="cache"></category></entry><entry><title>Issue 97: Laptop apps</title><link href="https://ngjunsiang.github.io/laymansguide/issue097.html" rel="alternate"></link><published>2020-12-05T08:00:00+08:00</published><updated>2020-12-05T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-12-05:/laymansguide/issue097.html</id><summary type="html">&lt;p&gt;A laptop app can do practically anything, if it is running through the Administrator/root account. Sandboxing is carried out through permission&amp;nbsp;control.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Mobile apps are sandboxed by the operating system. As a result, they have to bundle all the libraries they need, and are not allowed to share libraries with other apps. This results in mobile apps with huge&amp;nbsp;filesizes.&lt;/p&gt;
&lt;p&gt;This issue is going to be short, because laptop apps are … well, almost unlimited in what they can&amp;nbsp;do.&lt;/p&gt;
&lt;p&gt;Web apps are sandboxed by the web browser. Mobile apps are sandboxed by the mobile operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;). Laptop apps are sandboxed by hte desktop &lt;span class="caps"&gt;OS&lt;/span&gt; (yup, Windows on laptop and desktop is practically the&amp;nbsp;same).&lt;/p&gt;
&lt;p&gt;The main difference here lies in the difference between a mobile and desktop &lt;span class="caps"&gt;OS&lt;/span&gt;. Mobile OSes do not allow mobile apps to share libraries with other apps, and restrict their privileges (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue096.html"&gt;Issue 96&lt;/a&gt;)). Desktop OSes, on the other hand, let you do anything that is computationally possible … if you have&amp;nbsp;permission.&lt;/p&gt;
&lt;h2&gt;The Admin&amp;nbsp;account&lt;/h2&gt;
&lt;p&gt;A desktop &lt;span class="caps"&gt;OS&lt;/span&gt; often has an all-powerful user, known as the Administrator (Windows/MacOS), or root user (Linux). This user does not need permission to do anything. But with great power comes great responsibility, and with an admin account it is all too easy to do something that renders the computer&amp;nbsp;unuseable.&lt;/p&gt;
&lt;p&gt;So lower-privilege accounts exist—these are the user accounts. Logging in as a user gives you limited privileges: often you cannot change &lt;span class="caps"&gt;OS&lt;/span&gt; files, install or remove apps, or do anything risky. This is, for the most part, how desktop OSes sandbox the computer environment from damage by other&amp;nbsp;apps.&lt;/p&gt;
&lt;h2&gt;What an admin can&amp;nbsp;do&lt;/h2&gt;
&lt;p&gt;So what happens when you run an app on an admin&amp;nbsp;account?&lt;/p&gt;
&lt;p&gt;This app&amp;nbsp;can:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Edit, delete, rename &lt;span class="caps"&gt;OS&lt;/span&gt;-related&amp;nbsp;files&lt;/li&gt;
&lt;li&gt;Create new “virtual” (emulated) hardware devices, and manage drivers for&amp;nbsp;it&lt;/li&gt;
&lt;li&gt;Send data to any device, or receive data from any&amp;nbsp;device&lt;/li&gt;
&lt;li&gt;Make changes to storage devices, including the disk where the &lt;span class="caps"&gt;OS&lt;/span&gt; itself is installed (but not the partition&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt; where the &lt;span class="caps"&gt;OS&lt;/span&gt; is&amp;nbsp;installed)&lt;/li&gt;
&lt;li&gt;Run programs in the&amp;nbsp;background&lt;/li&gt;
&lt;li&gt;Send data over the network to any &lt;span class="caps"&gt;IP&lt;/span&gt; address, over any port (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue033.html"&gt;Issue 33&lt;/a&gt;))&lt;/li&gt;
&lt;li&gt;Prevent other programs from doing so (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue034.html"&gt;Issue 34&lt;/a&gt;))&lt;/li&gt;
&lt;li&gt;Install libraries that can be used by other&amp;nbsp;programs&lt;/li&gt;
&lt;li&gt;Access &lt;span class="caps"&gt;OS&lt;/span&gt; settings and make changes that affect &lt;span class="caps"&gt;OS&lt;/span&gt;&amp;nbsp;operation&lt;/li&gt;
&lt;li&gt;&amp;#8230; and many more&amp;nbsp;things!&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;User Account&amp;nbsp;Control&lt;/h2&gt;
&lt;p&gt;What if a user needs access to some of these permissions (but not all)? Does that mean they need to become an&amp;nbsp;Admin?&lt;/p&gt;
&lt;p&gt;Windows, and other OSes as well, usually have some way to give users limited permissions for some tasks. Windows uses User Account Control, which pops up a dialog box to alert the user. If the user gives permission for the app to proceed, then it is able to do so. If it is running on a user account, it can only perform tasks that the user account is allowed to perform. Linux uses the concept of groups; for a user to have permission to access bluetooth, for example, the linux &lt;span class="caps"&gt;OS&lt;/span&gt; often requires the user to be added to&amp;nbsp;the &lt;code&gt;bluetooth&lt;/code&gt; group in the &lt;span class="caps"&gt;OS&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; A laptop app can do practically anything, if it is running through the Administrator/root account. Sandboxing is carried out through permission&amp;nbsp;control.&lt;/p&gt;
&lt;p&gt;This is the reason why you should still buy a machine with a desktop operating system if you plan to be doing anything really productive; the sandboxing systems of web and mobile apps ultimately still impose a significant limit what you can do with the device. This is intentional; it is done for your safety! But if you want your device to do more, you’ll often need to override these “safety limits”, and that is where desktop operating systems come&amp;nbsp;in.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 98: Temporary&amp;nbsp;files&lt;/p&gt;
&lt;p&gt;Next issue is going to round off this mini-arc on how different kinds of apps operate. In the process of doing whatever it is they do, apps often generate temporary files that can be safely removed. How does this work for web apps and mobile apps? And for laptop&amp;nbsp;apps?&lt;/p&gt;
&lt;p&gt;From there, I’ll expand to talking about how apps store their data on and retrieve their data from the operating&amp;nbsp;system.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;I’ll talk about partitions in a future issue, when I move on to hardware devices&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 08"></category><category term="app"></category><category term="operating system"></category></entry><entry><title>Issue 96: Why are mobile apps so large in size?</title><link href="https://ngjunsiang.github.io/laymansguide/issue096.html" rel="alternate"></link><published>2020-11-28T08:00:00+08:00</published><updated>2020-11-28T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-11-28:/laymansguide/issue096.html</id><summary type="html">&lt;p&gt;Mobile apps are sandboxed by the operating system. As a result, they have to bundle all the libraries they need, and are not allowed to share libraries with other apps. This results in mobile apps with huge&amp;nbsp;filesizes.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Mobile apps, unlike web apps, can bundle resources and libraries to be installed to a mobile device. They can also request access to storage, and typically have a higher memory limit than web&amp;nbsp;apps.&lt;/p&gt;
&lt;p&gt;In the process of creating an app, a developer often needs to use libraries (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue017.html"&gt;Issue 17&lt;/a&gt;)), which are ready-packaged chunks of code she can run without having to write the code herself. Some provide core functions (e.g. sending information via the Internet, or checking if a data connection is available), while some provide optional features (e.g. mobile payments, or user feedback&amp;nbsp;systems).&lt;/p&gt;
&lt;p&gt;Libraries that provide core functions are typically provided by the operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;), which ultimately controls the device’s resources, but all else have to be pulled in by the app,&amp;nbsp;somehow.&lt;/p&gt;
&lt;h2&gt;Web apps vs mobile&amp;nbsp;apps&lt;/h2&gt;
&lt;p&gt;Web apps, which I covered in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue094.html"&gt;Issue 94&lt;/a&gt;), pull in any resources they need through web requests. This includes any libraries that they need. The browser allows it to do this, but prevents access to most parts of the operating system, and allows limited access to camera, sound, storage, etc. The sandboxing features of the browser make web apps generally safer to&amp;nbsp;access.&lt;/p&gt;
&lt;p&gt;A mobile app, on the other hand, is sandboxed by the operating system. Most of the resources it needs have to be present at the time of running the app, and that includes libraries. In an unsandboxed environment, commonly used libraries (e.g. mobile payment libraries) could be installed in the &lt;span class="caps"&gt;OS&lt;/span&gt; and shared by the apps. But this opens up a means of unauthorised access to multiple apps: hack this library successfully, and all other apps on the &lt;span class="caps"&gt;OS&lt;/span&gt; are also&amp;nbsp;affected!&lt;/p&gt;
&lt;p&gt;The sandboxing system in a mobile device does not allow this. Each app must bundle all the libraries it requires, to be installed into storage after downloading. This way, if an app has one or more libraries compromised, it would at least not expose the user’s data in other&amp;nbsp;apps.&lt;/p&gt;
&lt;h2&gt;Libraries in a mobile&amp;nbsp;app&lt;/h2&gt;
&lt;p&gt;The tradeoff to separating all these mobile apps and preventing sharing, is that each app now comes with its own copy of all the libraries it needs. And the file size can really add up—you already see it in the huge app sizes. I unbundled the installation package of a popular shopping app, Lazada, just to see what is inside&amp;nbsp;it.&lt;/p&gt;
&lt;p&gt;The list is way too long to post as an image, or even as text; it has over 300,000 code functions bundled inside! I should note that I am not an Android developer and can’t tell you very much about whether these libraries are absolutely necessary, but here are some noteworthy libraries included that I can make an educated guess&amp;nbsp;about:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Libraries to translate code from one programming language to another (often to translate an easier language into a faster or better-supported&amp;nbsp;language)&lt;/li&gt;
&lt;li&gt;Compatibility fallback &lt;span class="amp"&gt;&amp;amp;&lt;/span&gt; device evaluation libraries (probably for devices on older Android&amp;nbsp;versions)&lt;/li&gt;
&lt;li&gt;Layout libraries (for calculating placement of window frames in devices with different screen&amp;nbsp;sizes)&lt;/li&gt;
&lt;li&gt;Graphics, media, augmented reality (&lt;span class="caps"&gt;AR&lt;/span&gt;) libraries (for graphics rendering, video playback, capturing images from camera,&amp;nbsp;etc)&lt;/li&gt;
&lt;li&gt;System, network, version detection and updating,&amp;nbsp;etc&lt;/li&gt;
&lt;li&gt;Animation libraries (I see one from&amp;nbsp;AirBnB)&lt;/li&gt;
&lt;li&gt;Analytics libraries (to track user and ad engagement, and do A/B&amp;nbsp;testing)&lt;/li&gt;
&lt;li&gt;Debug, crash reporting, logging libraries (for troubleshooting app&amp;nbsp;crashes)&lt;/li&gt;
&lt;li&gt;Integration libraries (for login using &lt;span class="caps"&gt;FB&lt;/span&gt; and other&amp;nbsp;accounts)&lt;/li&gt;
&lt;li&gt;Maps,&amp;nbsp;location&lt;/li&gt;
&lt;li&gt;Search, image search, &lt;span class="caps"&gt;QR&lt;/span&gt; code, user feed, and related&amp;nbsp;services&lt;/li&gt;
&lt;li&gt;Database access (it seems to use Google Firebase, in addition to&amp;nbsp;others)&lt;/li&gt;
&lt;li&gt;Mobile&amp;nbsp;payments&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;and then there is the app itself, which contains code&amp;nbsp;for:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Address validation, checkout,&amp;nbsp;delivery&lt;/li&gt;
&lt;li&gt;User feed and homepage, login management,&amp;nbsp;recommendations&lt;/li&gt;
&lt;li&gt;Search&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;and many others which I don’t know&amp;nbsp;about.&lt;/p&gt;
&lt;h2&gt;Why aren’t web apps so huge&amp;nbsp;then?&lt;/h2&gt;
&lt;p&gt;They are! But they don’t need many of these (e.g. code translation and compatibility), and most of the libraries in 1–13 would have been loaded separately from the main page (see &lt;a href="https://ngjunsiang.github.io/laymansguide/issue078.html"&gt;Issue 78&lt;/a&gt;) for a visual example). Much of the functionality would not need to be loaded or installed upfront, only when it is required (e.g. map&amp;nbsp;display).&lt;/p&gt;
&lt;p&gt;For a web app, many more functions would also have been offloaded to Lazada’s servers, such as address validation. On a mobile app, this code is included upon installation to reduce data&amp;nbsp;usage.&lt;/p&gt;
&lt;p&gt;More importantly, you have little idea how large a web app really is, since you are never shown its filesize anywhere&amp;nbsp;;)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Mobile apps are sandboxed by the operating system. As a result, they have to bundle all the libraries they need, and are not allowed to share libraries with other apps. This results in mobile apps with huge&amp;nbsp;filesizes.&lt;/p&gt;
&lt;p&gt;It’s worth thinking about what this says whenever we hear about so much data being transmitted over the internet. Much of this data is actually duplicated data (for security of inefficiency reasons), or metadata (for data management), or overhead data (because of the way the data is packaged). Just like Amazon&amp;nbsp;packaging!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 97: Laptop&amp;nbsp;apps&lt;/p&gt;
&lt;p&gt;Finally we can move on to more fully explore the complexity of apps that integrate more closely with the operating system: laptop&amp;nbsp;apps!&lt;/p&gt;
&lt;p&gt;This should be enough of a primer before I go on to talk about where all this app data goes, and then about app installation and uninstallation (and hence strike out another “sometime in the future” question, woohoo!&amp;nbsp;🙌)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 08"></category><category term="app"></category></entry><entry><title>Issue 95: What’s in a mobile app?</title><link href="https://ngjunsiang.github.io/laymansguide/issue095.html" rel="alternate"></link><published>2020-11-21T08:00:00+08:00</published><updated>2020-11-21T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-11-21:/laymansguide/issue095.html</id><summary type="html">&lt;p&gt;Mobile apps, unlike web apps, can bundle resources and libraries to be installed to a mobile device. They can also request access to storage, and typically have a higher memory limit than web&amp;nbsp;apps.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Web apps require the browser to request memory on their behalf, and thus their memory usage shows up under the browser process in the &lt;span class="caps"&gt;OS&lt;/span&gt; Task Manager. Web apps use this data to store a more convenient (but larger) representation of the webpage document, and to store the data needed by the&amp;nbsp;app.&lt;/p&gt;
&lt;p&gt;“Why use the mobile app when there’s already a&amp;nbsp;website?”&lt;/p&gt;
&lt;p&gt;“Why even have a mobile app that looks almost exactly like the&amp;nbsp;website?”&lt;/p&gt;
&lt;p&gt;I’m not going to answer from an aesthetic or user experience point of view, you’re all experts in your own preferences :) Instead, I’ll focus on whats actually under the hood in this newsletter&amp;nbsp;issue.&lt;/p&gt;
&lt;p&gt;If you haven’t read &lt;a href="https://ngjunsiang.github.io/laymansguide/issue093.html"&gt;Issue 93&lt;/a&gt;) on web apps (a.k.a. websites), I’d recommend you do that for the full context, because in this issue I’ll highlight some key differences between web apps and mobile&amp;nbsp;apps.&lt;/p&gt;
&lt;h2&gt;Resources&lt;/h2&gt;
&lt;p&gt;Web apps have to request every single image, video, non-text object on the page via a web request. Caching (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue039.html"&gt;Issue 39&lt;/a&gt;))—storing these resources for offline use—can reduce subsequent load times, but the first load will still be the most&amp;nbsp;painful.&lt;/p&gt;
&lt;p&gt;A mobile app can package the most common, unchanging resources (logos, button images, backgrounds, etc) into the mobile app itself, so they can be loaded directly in the app, without having to make a web request and wait for the response. This lets it load faster (theoretically … in practice, many apps still have to retrieve other data from the server, so the loading speed improvement is&amp;nbsp;marginal)&lt;/p&gt;
&lt;h2&gt;Flexibility&lt;/h2&gt;
&lt;p&gt;The document object model, or &lt;span class="caps"&gt;DOM&lt;/span&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue094.html"&gt;Issue 94&lt;/a&gt;)) is how web apps keep track of all the elements and their contents on a page, but it is not the most efficient way to do so. A mobile app has more choice in deciding which user interface library (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue017.html"&gt;Issue 17&lt;/a&gt;)) it wants to&amp;nbsp;use.&lt;/p&gt;
&lt;h2&gt;Storage&amp;nbsp;access&lt;/h2&gt;
&lt;p&gt;A mobile app can request permission to access storage on the mobile device, allowing it to store files (images, data, &amp;#8230;) on the device without having to interrupt the user each time. It is not limited only to browser storage interfaces (localstorage, sessionstorage) and browser databases (IndexedDB)—see &lt;a href="https://ngjunsiang.github.io/laymansguide/issue093.html"&gt;Issue 93&lt;/a&gt;)—but can use other kinds of interfaces and databases, if they are available on the mobile device, or bundled into the mobile&amp;nbsp;app.&lt;/p&gt;
&lt;h2&gt;Memory&amp;nbsp;use&lt;/h2&gt;
&lt;p&gt;Both Android and iOS impose a memory limit on each app that is running. And they treat a mobile browser as a single app, despite all the web apps running inside it. So a web app has to share that limit with all the other web apps running in the mobile browser (which is why your tabs have to reload so often—they are also cleared&amp;nbsp;often!).&lt;/p&gt;
&lt;p&gt;On the other hand, a mobile app can have that per-app limit all to&amp;nbsp;itself.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;All told, a mobile app has more resources, which it requests directly from the &lt;span class="caps"&gt;OS&lt;/span&gt; instead of via the web browser, and it has more freedom in using those resources.
How so? And more importantly, why are some mobile apps just so darn &lt;em&gt;huge&lt;/em&gt;?&lt;/p&gt;
&lt;p&gt;This and mobile app sandboxing explained next&amp;nbsp;issue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Mobile apps, unlike web apps, can bundle resources and libraries to be installed to a mobile device. They can also request access to storage, and typically have a higher memory limit than web&amp;nbsp;apps.&lt;/p&gt;
&lt;p&gt;Mobile apps are a bit of a weak spot for me since I haven’t had as much experience here as I had in other areas, but nonetheless the limits of sandboxing are pretty visible. For the most part, we have accepted this tradeoff between size and security since storage space became much cheaper. But this tradeoff is also apparent not only in software, but also in business management and other areas: to increase security, we often also have to increase&amp;nbsp;bloat.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 96: Why are mobile apps so large in&amp;nbsp;size?&lt;/p&gt;
&lt;p&gt;Remember the days when most apps we downloaded and installed on a laptop were &lt;span class="caps"&gt;2MB&lt;/span&gt; or less? Today, mobile apps are many times that size. This is partly because of the way sandboxing is done for mobile apps. How so? I’ll go into more detail next week&amp;nbsp;;)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 08"></category><category term="app"></category><category term="memory"></category><category term="operating system"></category></entry><entry><title>Issue 94: Why do web browsers take up so much memory?</title><link href="https://ngjunsiang.github.io/laymansguide/issue094.html" rel="alternate"></link><published>2020-11-14T08:00:00+08:00</published><updated>2020-11-14T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-11-14:/laymansguide/issue094.html</id><summary type="html">&lt;p&gt;Web apps require the browser to request memory on their behalf, and thus their memory usage shows up under the browser process in the &lt;span class="caps"&gt;OS&lt;/span&gt; Task Manager. Web apps use this data to store a more convenient (but larger) representation of the webpage document, and to store the data needed by the&amp;nbsp;app.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Web apps have limited access to the device’ storage, and can only store data in browser-managed databases. Progressive Web Apps (PWAs) can additionally register service workers that run in the background. Because they are so cleanly sandboxed, they can be easily removed by clearing the browser cache and storage, and deregistering any service workers&amp;nbsp;manually.&lt;/p&gt;
&lt;p&gt;Apps once lived on a computer. You double-clicked them or pressed Enter or right-clicked Run, a window pops up and a rectangle appears on your taskbar. If Task Manager is showing you that it is using up gobs of memory, you just End Task and the rectangle disappears. Later in this season I’ll say more about what it is like to live in a computer, but we are on web apps&amp;nbsp;now.&lt;/p&gt;
&lt;p&gt;Web apps are somewhat more complicated. Because they are so tidily sandboxed (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue092.html"&gt;Issue 92&lt;/a&gt;)), they cannot actually live on your computer. Instead, they live in your&amp;nbsp;browser.&lt;/p&gt;
&lt;h2&gt;Living in a web&amp;nbsp;browser&lt;/h2&gt;
&lt;p&gt;Living in a computer means that the operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;) takes care of you; it gives you the memory and disk space you need, gives you &lt;span class="caps"&gt;CPU&lt;/span&gt; time to run your processes, and gives you access to devices (such as the screen and&amp;nbsp;keyboard).&lt;/p&gt;
&lt;p&gt;When you live in the browser, the browser takes care of you. Everything you need is requested from the operating system (&lt;span class="caps"&gt;OS&lt;/span&gt;) by the browser. The space that the web app uses in localstorage, sessionstorage, IndexedDB, and the cache, is space that the browser requested. The &lt;span class="caps"&gt;CPU&lt;/span&gt; cycles that the web app needs are cycles that the browser has&amp;nbsp;requested.&lt;/p&gt;
&lt;p&gt;When you open the &lt;span class="caps"&gt;OS&lt;/span&gt; Task Manager, where do these requests show up? Under (one of) the browser processes, naturally. If you have more than 20 browser tabs open for more than 5 apps, it shouldn’t be surprising that they are using a lot of memory; I’ll go into why shortly. More worryingly, that’s not helpful if you’re trying to figure out which browser tab to close so you can play your memory-consuming video&amp;nbsp;game.&lt;/p&gt;
&lt;p&gt;Only the browser has that information; you will have to open the browser’s Task Manager (another google away) to see that&amp;nbsp;information.&lt;/p&gt;
&lt;h2&gt;A web app’s&amp;nbsp;needs&lt;/h2&gt;
&lt;p&gt;I have a browser tab open now, with a Google Sheet loaded. What is the Google Sheet app doing on that page? Let’s open DevTools and find&amp;nbsp;out.&lt;/p&gt;
&lt;p&gt;DevTools has a really cool tab labelled “Memory”, and it has a nice visual depiction of what the app is doing with all that&amp;nbsp;memory:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Firefox DevTools, showing the Memory tab. The app is using 83.84MB of memory, and 32MiB of that is used by objects." src="https://ngjunsiang.github.io/laymansguide/issue094_01.png" /&gt;&lt;br /&gt;
&lt;em&gt;DevTools in Firefox lets you inspect the memory that web apps use.&lt;br /&gt;The Memory tab shows what is stored in memory.&lt;br /&gt;Most of the memory here is being taken up by javascript&amp;nbsp;objects.&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Javascript &lt;code&gt;object&lt;/code&gt;s here are Javascript’s own internal representation of data, which is quite similar to a document database’s format. Altogether, they take up 32 MiB of memory space (difference between &lt;span class="caps"&gt;MB&lt;/span&gt; and MiB is covered in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue040.html"&gt;Issue 40&lt;/a&gt;)). Google Sheets is juggling a lot of data internally, data which is not stored in IndexedDB or&amp;nbsp;localstorage!&lt;/p&gt;
&lt;p&gt;&lt;code&gt;other&lt;/code&gt;, taking up 15 MiB, seems to be pointing to a javascript library that Google Sheets is using to render the&amp;nbsp;spreadsheet.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;strings&lt;/code&gt; are simpler&amp;nbsp;than &lt;code&gt;object&lt;/code&gt;s, each one representing a snippet of text, or possibly even a number. They only take up&amp;nbsp;5MiB. &lt;code&gt;scripts&lt;/code&gt; are the internally stored scripts that the page is executing; they take up 11&amp;nbsp;MiB.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;domNode&lt;/code&gt; is where it gets interesting. We are used to seeing &lt;span class="caps"&gt;HTML&lt;/span&gt; documents (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue050.html"&gt;Issue 50&lt;/a&gt;)) as a plain text document with lots of formatting, but in a browser it becomes more than just text. Each part of the page, an &lt;span class="caps"&gt;HTML&lt;/span&gt; element, can have its properties changed by Javascript as the page reacts to new data, or to user&amp;nbsp;input.&lt;/p&gt;
&lt;h2&gt;The &lt;span class="caps"&gt;HTML&lt;/span&gt; Document Object&amp;nbsp;Model&lt;/h2&gt;
&lt;p&gt;It would be too computationally taxing to keep scanning through the text document to figure out which part of the page is meant to be changed. Instead, the browser has its own way of storing the &lt;em&gt;hierarchy&lt;/em&gt; of elements: each menu option falls under a menu heading, which falls under the navigation bar, which falls under the header, which falls under the main document, and so on. If each browser had its own way of doing that, a web developer would have to learn all of them to make a webpage that worked across all browsers; that’s&amp;nbsp;terrible!&lt;/p&gt;
&lt;p&gt;Instead, the web standardised on one way of doing so: the &lt;span class="caps"&gt;HTML&lt;/span&gt; &lt;strong&gt;Document Object Model (&lt;span class="caps"&gt;DOM&lt;/span&gt;)&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Internally, a web browser converts the &lt;span class="caps"&gt;HTML&lt;/span&gt; page into a &lt;span class="caps"&gt;DOM&lt;/span&gt;—a data structure that makes it easy to find the specific &lt;span class="caps"&gt;HTML&lt;/span&gt; element (or elements) that need to be modified by each function. The &lt;span class="caps"&gt;HTML&lt;/span&gt; &lt;span class="caps"&gt;DOM&lt;/span&gt; for the page I’m on takes up 14 MiB, which may sound like a lot, until you remember that each element also has associated metadata stored along with its content. And Google Sheets has lots of&amp;nbsp;elements!&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Web apps require the browser to request memory on their behalf, and thus their memory usage shows up under the browser process in the &lt;span class="caps"&gt;OS&lt;/span&gt; Task Manager. Web apps use this data to store a more convenient (but larger) representation of the webpage document, and to store the data needed by the&amp;nbsp;app.&lt;/p&gt;
&lt;p&gt;And that is how a web app uses up 84MiB of memory space. If you have multiple tabs running the same app (e.g. multiple Google Sheets open), some of the memory can be shared (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue065.html"&gt;Issue 84&lt;/a&gt;)) by these tabs&amp;nbsp;(e.g. &lt;code&gt;scripts&lt;/code&gt;), but otherwise each tab is going to have its own memory&amp;nbsp;needs.&lt;/p&gt;
&lt;p&gt;In the earlier days of the internet, when spreadsheets were still a separate app, this memory usage would have showed up in the &lt;span class="caps"&gt;OS&lt;/span&gt; Task Manager under Lotus 1-2-3, Microsoft Excel, or some other spreadsheet program. Today, it shows up under Chrome or Firefox, and the details are only inspectable through the browser’s Task&amp;nbsp;Manager.&lt;/p&gt;
&lt;p&gt;No wonder browsers get all the blame these&amp;nbsp;days.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 95: What’s in a mobile&amp;nbsp;app?&lt;/p&gt;
&lt;p&gt;This issue felt like a data dump; I know it’s a lot all to take in 😅 In my childhood, I had access to lots of books with these cutaways showing the inner mechanisms of devices of all sorts, and I loved those books. It’s rather harder to do the same with software, since there’s nothing to physically slice through (even if only in the imagination!) I hope that the screenshots in this season of &lt;span class="caps"&gt;LMG&lt;/span&gt; will help you imagine the inner mechanisms of apps. Let me know if it’s working for you, and if there’s anything you’d like to see&amp;nbsp;:)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 08"></category><category term="app"></category><category term="cache"></category><category term="memory"></category><category term="operating system"></category></entry><entry><title>Issue 93: What’s in a web app?</title><link href="https://ngjunsiang.github.io/laymansguide/issue093.html" rel="alternate"></link><published>2020-11-07T08:00:00+08:00</published><updated>2020-11-07T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-11-07:/laymansguide/issue093.html</id><summary type="html">&lt;p&gt;Web apps have limited access to the device’ storage, and can only store data in browser-managed databases. Progressive Web Apps (PWAs) can additionally register service workers that run in the background. Because they are so cleanly sandboxed, they can be easily removed by clearing the browser cache and storage, and deregistering any service workers&amp;nbsp;manually.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Sandboxing is a catch-all term for the concept of ensuring apps don’t have access to resources outside of their privileges. Sandboxed apps are generally safer than non-sandboxed apps in terms of security, and easier to manage, terminate, and&amp;nbsp;uninstall.&lt;/p&gt;
&lt;p&gt;The simplest apps we use do not &lt;em&gt;generate&lt;/em&gt; data; think about your calculator, which simply crunches calculations and displays the result (conveniently ignoring the ones with memory slots for storing calculated values …). Or currency converters, or timezone converters … no storage&amp;nbsp;needed.&lt;/p&gt;
&lt;p&gt;So how do web apps store&amp;nbsp;data?&lt;/p&gt;
&lt;h2&gt;Data storage in web&amp;nbsp;apps&lt;/h2&gt;
&lt;p&gt;As a web-first programming language, Javascript programs were not expected to have to access, open, or create files on the device. That would make it really easy for a malicious script to download malware to a folder, where it could be accidentally invoked! Instead, it relies on other features to store and retrieve data for specific&amp;nbsp;purposes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;To remember user logins (the “Remember me” feature you see on almost every login screen), web apps can set/unset cookies (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue069.html"&gt;Issue 69&lt;/a&gt;)) in the&amp;nbsp;browser.&lt;/li&gt;
&lt;li&gt;To obtain files for use, the web app can invoke a File Select dialog for the user to choose a file, such as for uploading to the server. The web app is not allowed to access arbitrary files this&amp;nbsp;way.&lt;/li&gt;
&lt;li&gt;If data needs to be provided to the user in the form of a file, it can be stored on the disk with the user’s permission through a download&amp;nbsp;dialog.&lt;/li&gt;
&lt;li&gt;The web app can store data through a browser feature called &lt;strong&gt;localstorage&lt;/strong&gt;. This is a &lt;em&gt;key-value database&lt;/em&gt;, managed by the browser, that allows you to store data (the &lt;em&gt;value&lt;/em&gt;) tagged to a &lt;em&gt;key&lt;/em&gt;. The same way a hotel lobby holds your luggage for you and lets you access it through a luggage tag, or the way you can rent a locker for storing your stuff (&lt;em&gt;value&lt;/em&gt;) and access it through the locker &lt;em&gt;key&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;For data that is only needed in that tab (e.g. partially filled form data), and can be safely deleted when the tab is closed, the browser provides &lt;strong&gt;sessionstorage&lt;/strong&gt;. This works similarly to&amp;nbsp;localstorage.&lt;/li&gt;
&lt;li&gt;For more significant amounts of data, web apps can use IndexedDB, a more advanced database also managed in the browser. It is a document database (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue088.html"&gt;Issue 88&lt;/a&gt;)), with each document tagged to a key in a key-object&amp;nbsp;system.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img alt="Firefox DevTools, showing the Storage tab. Local Storage is selected, displaying a list of keys and values." src="https://ngjunsiang.github.io/laymansguide/issue093_01.png" /&gt;&lt;br /&gt;
&lt;em&gt;DevTools in Firefox lets you inspect the data that web apps keep.&lt;br /&gt;The Storage tab shows what is stored in cache, cookies, IndexedDB, localstorage, and sessionstorage.&lt;br /&gt;IndexedDB, localstorage, and sessionstorage are key-value databases that store the data (value) tagged to a&amp;nbsp;key.&lt;/em&gt;    &lt;/p&gt;
&lt;h2&gt;Requesting and receiving data on a&amp;nbsp;server&lt;/h2&gt;
&lt;p&gt;So a web app doesn’t much in the way of storage access, but they were not designed for that at all. Most of the heavy lifting is not meant to be done in the browser, but elsewhere, on a server. A web app would send heavy workloads to a server (typically owned by the same company) through an &lt;span class="caps"&gt;API&lt;/span&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue004.html"&gt;Issue 4&lt;/a&gt;)) through a web request (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue009.html"&gt;Issue 9&lt;/a&gt;)), and receive the results through a server response (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue008.html"&gt;Issue 8&lt;/a&gt;)).&lt;/p&gt;
&lt;p&gt;A web app would also need resources for display: images, videos, PDFs, &amp;#8230; these are requested and received via web requests as&amp;nbsp;well.&lt;/p&gt;
&lt;p&gt;Cool, so a web developer can just write Javascript code to get the data and resources it needs, display stuff to the user, wait for the user to interact, and then make more requests to the server to calculate stuff, or send the app more data (such as the user’s tweets or posts or other&amp;nbsp;stuff).&lt;/p&gt;
&lt;p&gt;Just one problem with this: if internet connectivity is intermittent or laggy, none of this is going to work! Even when the internet is fine, it makes for a very slow experience. How do we improve&amp;nbsp;this?&lt;/p&gt;
&lt;p&gt;One way is to cache (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue039.html"&gt;Issue 39&lt;/a&gt;)) as many things as possible: header images, logos, emojis, icons, … these can all be stored in localstorage and accessed even when the app is&amp;nbsp;offline.&lt;/p&gt;
&lt;p&gt;Some apps, such as Google Docs, will also store user data in IndexedDB for a smoother experience—imagine having to wait for a request-response round-trip to the Google servers for every word you type. The data gets modified in IndexedDB first, and then synced to the servers. If the device gets disconnected from the internet, at least you will still be able to read whatever is in IndexedDB (and if you have enabled offline access, you can even edit the data in IndexedDB, and the Docs app will attempt to sync it to the server once connectivity is&amp;nbsp;restored).&lt;/p&gt;
&lt;h2&gt;Running background&amp;nbsp;processes&lt;/h2&gt;
&lt;p&gt;Then what’s with these popups on some websites asking you to install them? And how are some websites actually able to send us notifications? Something has got to be running in the browser background for these to happen, and none of what we have learned so far explains that … what&amp;nbsp;gives?&lt;/p&gt;
&lt;p&gt;Enter &lt;strong&gt;Progressive Web Apps&lt;/strong&gt; (PWAs). With some Googling and lots of reading on StackOverflow and other web documents, a web developer can get started meeting the various requirements needed to create a &lt;span class="caps"&gt;PWA&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;And in exchange for that inconvenience, she can use &lt;strong&gt;service workers&lt;/strong&gt;: javascript scripts that run on their own and are not dependent on the browser tab staying open. These service workers can listen for messages from the server, carry out some processing, make requests and receive responses, all independently from the app running in the tab. On mobile devices, they are gradually gaining more features as well, such as access to the Share feature (enabling users to share content with the &lt;span class="caps"&gt;PWA&lt;/span&gt;), and being able to access cameras, microphones, location services, and other things (provided the user grants&amp;nbsp;permission).&lt;/p&gt;
&lt;h2&gt;Installing web&amp;nbsp;apps&lt;/h2&gt;
&lt;p&gt;Okay, wait. &lt;span class="caps"&gt;WHAT&lt;/span&gt;?!&lt;/p&gt;
&lt;p&gt;I imagine most folks would be okay with cookies, with localstorage and even IndexedDB. You want things from an app, you gotta give it space to work, right? That’s&amp;nbsp;fair.&lt;/p&gt;
&lt;p&gt;But service workers, what?! You mean if I click Install, these apps get to run stuff &lt;em&gt;in the background&lt;/em&gt; in my browser, even after the tab is closed? If I’m not cool with that, I can just choose not to install and then these service workers won’t get installed,&amp;nbsp;right?&lt;/p&gt;
&lt;p&gt;Umm, I don’t know how to break this to you gently, but&amp;nbsp;nope.&lt;/p&gt;
&lt;p&gt;Once you visit a site (&lt;span class="caps"&gt;URL&lt;/span&gt;) with a registered service worker, your browser automatically registers it. The only thing the Install button does is to add a shortcut on your Desktop/Home Screen, and perhaps enable some features (such as mobile Share). But the service workers are already&amp;nbsp;there.&lt;/p&gt;
&lt;h2&gt;Uninstalling web&amp;nbsp;apps&lt;/h2&gt;
&lt;p&gt;And now the good&amp;nbsp;news.&lt;/p&gt;
&lt;p&gt;Because web apps are so cleanly sandboxed, they don’t stick tendrils into your operating system or device storage (beyond the space reserved by the browser, anyway). Removing apps and their files just involves clearing your browser cache and website storage. You will, however, have to deregister the service workers manually; please google for&amp;nbsp;instructions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Web apps have limited access to the device’ storage, and can only store data in browser-managed databases. Progressive Web Apps (PWAs) can additionally register service workers that run in the background. Because they are so cleanly sandboxed, they can be easily removed by clearing the browser cache and storage, and deregistering any service workers&amp;nbsp;manually.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 94: Why do web browsers take up so much&amp;nbsp;memory?&lt;/p&gt;
&lt;p&gt;I’m finally starting to answer one of the sometime-in-the-future questions below, and can’t wait to get to the meat of “What is involved in installing a piece of software?”; it’ll be a ride!&amp;nbsp;:)&lt;/p&gt;
&lt;p&gt;Before I move on to compare web apps with mobile apps, I’m going to take a short detour next issue and answer a question I hear all too often: “Why do web browsers take up so much&amp;nbsp;memory?”&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 08"></category><category term="app"></category><category term="cache"></category></entry><entry><title>Issue 92: All about apps</title><link href="https://ngjunsiang.github.io/laymansguide/issue092.html" rel="alternate"></link><published>2020-10-31T08:00:00+08:00</published><updated>2020-10-31T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-10-31:/laymansguide/issue092.html</id><summary type="html">&lt;p&gt;Sandboxing is a catch-all term for the concept of ensuring apps don’t have access to resources outside of their privileges. Sandboxed apps are generally safer than non-sandboxed apps in terms of security, and easier to manage, terminate, and&amp;nbsp;uninstall.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Depending on what you need a database for, there may be online database platforms that can manage and automate much of the work for you. Airtable, Smartsheet, Knack, and Zoho Creator are just 4 of many options that offer an easier way to set up and input your data, then access them through apps or other&amp;nbsp;means.&lt;/p&gt;
&lt;p&gt;There was a time when apps, short for &lt;em&gt;applications&lt;/em&gt;, were these files that came on a &lt;span class="caps"&gt;CD&lt;/span&gt; or diskette, usually ending&amp;nbsp;in &lt;code&gt;.exe&lt;/code&gt; (if your settings enabled you to see file extensions). You double-clicked them or pressed ‘Enter’, and things&amp;nbsp;happened.&lt;/p&gt;
&lt;p&gt;Today … the idea of an app is more nebulous.&amp;nbsp;The &lt;code&gt;.exe&lt;/code&gt; files are still there, but now there are also apps that you install from the app store. And what’s up with webpages that display an app installation pop-up and create an icon on your home screen or desktop? Are they the same kind of app? If not, what’s the difference between&amp;nbsp;them?&lt;/p&gt;
&lt;p&gt;Before I answer that question, we need to talk about an important concept called &lt;strong&gt;sandboxing&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;Why do apps need to be&amp;nbsp;sandboxed?&lt;/h2&gt;
&lt;p&gt;An unrestrained app running in your operating system would have access to any and all resources on that machine. It could potentially modify or remove system files, halt running programs or accidentally overwrite their memory contents, and so&amp;nbsp;on.&lt;/p&gt;
&lt;p&gt;&lt;a href="[Issue 56]({filename}/season05/issue056/issue056.md)"&gt;The operating system&lt;/a&gt;) takes some measures to minimise or prevent this. But even within these limits, a app developed with good intentions could still potentially cause trouble. It might accidentally hang on to audio access when it no longer needs it, make a mess of the user’s home folder, leave files in limbo, etc. Programs can leave a mess, and a way to limit this mess is very&amp;nbsp;welcome.&lt;/p&gt;
&lt;p&gt;This is why sandbox systems were already being researched as early as the 1970s, and are still an ongoing research interest at many&amp;nbsp;institutions.&lt;/p&gt;
&lt;h2&gt;What is&amp;nbsp;sandboxing?&lt;/h2&gt;
&lt;p&gt;If you are old enough you might remember playing in a sandbox. While there are usually no explicit rules about how to play in a sandbox, there is usually one unspoken&amp;nbsp;rule:&lt;/p&gt;
&lt;p&gt;What was in the sandbox, stays in the&amp;nbsp;sandbox.&lt;/p&gt;
&lt;p&gt;A sandbox limits the mess, yet gives you unrestrained freedom &lt;em&gt;within&lt;/em&gt; that&amp;nbsp;box.&lt;/p&gt;
&lt;p&gt;In a computer, a sandbox system imposes restrictions on running applications. Some common restrictions&amp;nbsp;include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;only being able to access/change/delete files within a particular&amp;nbsp;subfolder&lt;/li&gt;
&lt;li&gt;only having access to some &lt;span class="caps"&gt;OS&lt;/span&gt; resources (e.g. not being able to access &lt;span class="caps"&gt;USB&lt;/span&gt; devices, or audio, or&amp;nbsp;webcam)&lt;/li&gt;
&lt;li&gt;having limited privileges while the application is not active (e.g. no internet access when not being directly interacted with, and just running in the&amp;nbsp;background)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These restrictions are intended to limit any damage and keep the system more stable than it might otherwise have&amp;nbsp;been.&lt;/p&gt;
&lt;p&gt;So one way to categorise laptop apps, mobile apps, and web apps is in the amount of sandboxing they are subject&amp;nbsp;to.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Sandboxing is a catch-all term for the concept of ensuring apps don’t have access to resources outside of their privileges. Sandboxed apps are generally safer than non-sandboxed apps in terms of security, and easier to manage, terminate, and&amp;nbsp;uninstall.&lt;/p&gt;
&lt;p&gt;Keeping it vague in this issue because the details really differ between operating systems, kinds of apps, and even the way they are distributed. We’ll be digging into details next issue, starting with the most heavily sandboxed app: the web&amp;nbsp;app.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 93: What’s in a web&amp;nbsp;app?&lt;/p&gt;
&lt;p&gt;From just little snippets of script that animated buttons and counted visitors back in the 90s, Javascript now powers a huge portion of the Internet, processing payments, serving ads, and much more besides. We’ll look at how this gets packaged into a web app next&amp;nbsp;issue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 08"></category><category term="app"></category></entry><entry><title>Issue 91: Commercial database alternatives</title><link href="https://ngjunsiang.github.io/laymansguide/issue091.html" rel="alternate"></link><published>2020-10-11T11:00:00+08:00</published><updated>2020-10-11T11:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-10-11:/laymansguide/issue091.html</id><summary type="html">&lt;p&gt;Depending on what you need a database for, there may be online database platforms that can manage and automate much of the work for you. Airtable, Smartsheet, Knack, and Zoho Creator are just 4 of many options that offer an easier way to set up and input your data, then access them through apps or other&amp;nbsp;means.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; A &lt;span class="caps"&gt;URI&lt;/span&gt; (Uniform Resource Identifier) is required to connect to a database. This &lt;span class="caps"&gt;URI&lt;/span&gt; can be provided by a hosting service provider that runs your own database for you, or by a cloud service provider that runs your database on their&amp;nbsp;platform.&lt;/p&gt;
&lt;p&gt;So you’re running up against the limits of a spreadsheet and want to do more with the data inside it. Databases sound cool and kind of like what you want right now. But writing a whole app and setting up the database yourself, or even getting someone else to do it and checking their work &amp;#8230; it all sounds like so&amp;nbsp;much.&lt;/p&gt;
&lt;p&gt;What to&amp;nbsp;do?&lt;/p&gt;
&lt;h2&gt;Airtable&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://airtable.com/"&gt;Airtable&lt;/a&gt; is a database-like platform that lets you set up Bases (similar to databases), which can contain different tables for your data. You can specify a specific data type for each table, limit entries to a list of options, and even create lookups (match the value here with a column in another table, and return data from another column in the same&amp;nbsp;row).&lt;/p&gt;
&lt;p&gt;Just as databases don’t have a single canonical view, and everything depends on queries, Airtable also lets you create different views of your data. You can set it up as a list, a gallery, a job status board, and filter it as you&amp;nbsp;like.&lt;/p&gt;
&lt;p&gt;Interestingly, Airtable also dynamically generates an &lt;span class="caps"&gt;API&lt;/span&gt; for each of your bases, so that apps you create have a way to retrieve, modify, or delete data from the database. That saves you a lot of trouble having to set up your own database, for simple&amp;nbsp;needs.&lt;/p&gt;
&lt;h2&gt;Smartsheet&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://www.smartsheet.com/platform"&gt;Smartsheet&lt;/a&gt; is another platform that lets you create sheets with different views. Unlike Airtable, is leans more heavily towards workplace workflows, with built-in task management features and integration with many services. If you are already using one or more of these services, Smartsheet could be a way to store information for&amp;nbsp;collaboration.&lt;/p&gt;
&lt;h2&gt;Knack&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://www.knack.com/tour"&gt;Knack&lt;/a&gt; is yet another database-as-a-platform, which also allows you to craft queries to extract the data you need. It has an interesting feature that lets you specify how tables relate to each other (e.g. Contact connects with &lt;em&gt;one&lt;/em&gt; Company, Company connects with &lt;em&gt;many&lt;/em&gt; contacts) to improve&amp;nbsp;queries.&lt;/p&gt;
&lt;p&gt;Knack also lets you create simple apps with limited access to the data, for employee or customer use. If you mainly need internal apps for disseminating or allowing field access to data, this is probably a simpler option than hiring an app&amp;nbsp;programmer/company.&lt;/p&gt;
&lt;h2&gt;Zoho&amp;nbsp;Creator&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://www.zoho.com/creator/"&gt;Zoho Creator&lt;/a&gt; is a database platform that is more focused on app-building (or so it appears). The database just comes bundled as part of the deal. Another option for corporate operations-focused&amp;nbsp;apps.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Depending on what you need a database for, there may be online database platforms that can manage and automate much of the work for you. Airtable, Smartsheet, Knack, and Zoho Creator are just 4 of many options that offer an easier way to set up and input your data, then access them through apps or other&amp;nbsp;means.&lt;/p&gt;
&lt;p&gt;The best thing about these cloud services is that you probably don’t need to learn &lt;span class="caps"&gt;SQL&lt;/span&gt; or other advanced query languages to use them. A passing familiarity with spreadsheets, and time to sit down and watch tutorial videos, is probably sufficient to get&amp;nbsp;started.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S8] Issue 92: All about&amp;nbsp;apps&lt;/p&gt;
&lt;p&gt;I’ve spent a whole season talking about data (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue040.html"&gt;Season 4, Issue 40&lt;/a&gt;) to &lt;a href="https://ngjunsiang.github.io/laymansguide/issue052.html"&gt;Issue 52&lt;/a&gt;)), then detoured to talking about computers, and the internet, and now back to databases. I think that’s plenty of foundation to finally move on to something more familiar:&amp;nbsp;apps.&lt;/p&gt;
&lt;p&gt;What exactly are apps and what do they do? What are they like under the surface? What makes them&amp;nbsp;tick?&lt;/p&gt;
&lt;p&gt;This and more in Season 8 &amp;#8230; which will start after a two-week hiatus. It has been really fun putting finger to keyboard and watching everything come together, but I noticed the quality of recent issues has been sliding more than I’d like. I’m going to take a little break to reconsolidate, recuperate, and think about the next couple of&amp;nbsp;seasons.&lt;/p&gt;
&lt;p&gt;See you next&amp;nbsp;issue!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 07"></category></entry><entry><title>Issue 90: Using a database</title><link href="https://ngjunsiang.github.io/laymansguide/issue090.html" rel="alternate"></link><published>2020-10-03T08:00:00+08:00</published><updated>2020-10-03T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-10-03:/laymansguide/issue090.html</id><summary type="html">&lt;p&gt;A &lt;span class="caps"&gt;URI&lt;/span&gt; (Uniform Resource Identifier) is required to connect to a database. This &lt;span class="caps"&gt;URI&lt;/span&gt; can be provided by a hosting service provider that runs your own database for you, or by a cloud service provider that runs your database on their&amp;nbsp;platform.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Graph databases treat the details of things as secondary, and optimise for managing the network of relationships. A graph database can quickly look up how things are related to each other, and return the&amp;nbsp;results.&lt;/p&gt;
&lt;p&gt;At some point in the past, getting a database meant talking to a consultant or contractor, who would then sit with you to understand your requirements, then set everything up for you without letting you touch any part of it. And that is probably for the benefit of you both. But today, for SMEs with some relevant expertise, it is actually possible to get your own database up and running very&amp;nbsp;quickly.&lt;/p&gt;
&lt;h2&gt;Setting up a database on a&amp;nbsp;server&lt;/h2&gt;
&lt;p&gt;If you have admin rights to the workplace server (which can be both a blessing and a curse), you’ll have to find the setup instructions that came with the server software (or Google it online). I’m sorry, it is painful for layfolks (and even for many experienced database admins) and there just isn’t an easier way&amp;nbsp;yet.&lt;/p&gt;
&lt;h2&gt;Registering a database in the&amp;nbsp;cloud&lt;/h2&gt;
&lt;p&gt;If you do not have admin rights to the workplace server, you usually ask your friendly server administrator to help you install the database and set up a web admin panel for you. They will give you a &lt;span class="caps"&gt;URL&lt;/span&gt; and login credentials for that web admin panel, and you configure the database through the database section of the admin&amp;nbsp;panel.&lt;/p&gt;
&lt;p&gt;If your company has decided to do away with organic &lt;span class="caps"&gt;IT&lt;/span&gt; support, your next bet is to outsource that help from cloud services. Each of the major cloud providers provide multiple database types for your perusal. Some app hosting services will also host a database for you (usually intended for app use, but who’s&amp;nbsp;asking?).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Relational databases&lt;/strong&gt;
- Amazon Relational Database Service
- Google Cloud &lt;span class="caps"&gt;SQL&lt;/span&gt;
- Microsoft Azure &lt;span class="caps"&gt;SQL&lt;/span&gt;&amp;nbsp;Database&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Document databases&lt;/strong&gt; (You will see many of them referred to as NoSQL databases)
- Amazon DynamoDB
- Google Cloud Firestore (part of Firebase)
- Microsoft Azure Cosmos&amp;nbsp;Database&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Graph databases&lt;/strong&gt;
- Amazon Neptune
- Microsoft Azure Cosmos also has an &lt;span class="caps"&gt;API&lt;/span&gt; (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue004.html"&gt;Issue 4&lt;/a&gt;)) for graph&amp;nbsp;databases&lt;/p&gt;
&lt;h2&gt;Getting the database&amp;nbsp;identifier&lt;/h2&gt;
&lt;p&gt;After you have successfully registered a database (of any type), you will be given a connection &lt;span class="caps"&gt;URI&lt;/span&gt; (Uniform Resource Identifier), which is a fancy way of saying “&lt;span class="caps"&gt;URL&lt;/span&gt; to identify your database uniquely”. It can be a simple line of text,&amp;nbsp;like:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;mongodb://mongodb0.example.com:27017&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;which identifies your database as&amp;nbsp;a &lt;code&gt;mongodb&lt;/code&gt; (document) database running on the server&amp;nbsp;at &lt;code&gt;mongodb0.example.com&lt;/code&gt; on&amp;nbsp;port &lt;code&gt;27017&lt;/code&gt;. (I covered server hostnames in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue029.html"&gt;Issue 29&lt;/a&gt;) and port numbers in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue033.html"&gt;Issue 33&lt;/a&gt;)).&lt;/p&gt;
&lt;p&gt;or it can look&amp;nbsp;like:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;postgres://myusername:myverylongwindedpasswordwhichisobviouslygeneratedbyacomputerandnotahuman@ec2-52-207-124-89.compute-1.amazonaws.com:5432/d77ila0heea1lk&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;which identifies your database as&amp;nbsp;a &lt;code&gt;postgres&lt;/code&gt; (relational) database running on the server&amp;nbsp;at &lt;code&gt;ec2-56-486-386-34.compute-5.amazonaws.com&lt;/code&gt; on&amp;nbsp;port &lt;code&gt;5432&lt;/code&gt;, and your particular database is&amp;nbsp;named &lt;code&gt;d77ila0heea1lk&lt;/code&gt; (you can run multiple databases on a single&amp;nbsp;server).&lt;/p&gt;
&lt;h2&gt;Connecting to a&amp;nbsp;database&lt;/h2&gt;
&lt;p&gt;This is where it gets a bit&amp;nbsp;trickier.&lt;/p&gt;
&lt;p&gt;If you are using another online service that integrates with your database, that service needs to know your &lt;span class="caps"&gt;URI&lt;/span&gt; and its associated information. The service will either ask your for your login/authentication credentials, hostname, and port separately, or ask for it in a single &lt;span class="caps"&gt;URI&lt;/span&gt;, or some mix of the two&amp;nbsp;options.&lt;/p&gt;
&lt;p&gt;If you are hiring your own developer (including possibly yourself), you will have to figure out which module you need to connect to the&amp;nbsp;database.&lt;/p&gt;
&lt;p&gt;For example, MongoDB in&amp;nbsp;Python: &lt;code&gt;MongoClient('mongodb://mongodb0.example.com:27017')&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;And for PostGreSQL in&amp;nbsp;Python: &lt;code&gt;psycopg2.connect('postgres://myusername:myverylongwindedpasswordwhichisobviouslygeneratedbyacomputerandnotahuman@ec2-52-207-124-89.compute-1.amazonaws.com:5432/d77ila0heea1lk')&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; It is considered insecure to simply leave your login credentials in code like that. Please read up on best practices for importing sensitive information from more secure sources in your programming language of&amp;nbsp;choice.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; A &lt;span class="caps"&gt;URI&lt;/span&gt; (Uniform Resource Identifier) is required to connect to a database. This &lt;span class="caps"&gt;URI&lt;/span&gt; can be provided by a hosting service provider that runs your own database for you, or by a cloud service provider that runs your database on their&amp;nbsp;platform.&lt;/p&gt;
&lt;p&gt;Once you go through the painful process the first time, it gets easier. A lot of engineering work has been done to make this possible: connect to a database with one identifier. URIs are their own fascinating bit of information engineering, definitely not within the scope of &lt;em&gt;Layman’s Guide&lt;/em&gt;. It is something to think about whenever you need to identify everything in your office or warehouse with a unique name (think barcode system or inventory/asset&amp;nbsp;management).&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S7] Issue 91: Commercial database&amp;nbsp;alternatives&lt;/p&gt;
&lt;p&gt;What if we don’t want to do all of that? Next issue, to wrap up this season, I’ll give you some alternatives that sit somewhere between a full database solution, and a simple Excel/Google Sheets&amp;nbsp;spreadsheet.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 07"></category></entry><entry><title>Issue 89: Graph Databases</title><link href="https://ngjunsiang.github.io/laymansguide/issue089.html" rel="alternate"></link><published>2020-09-26T08:00:00+08:00</published><updated>2020-09-26T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-09-26:/laymansguide/issue089.html</id><summary type="html">&lt;p&gt;Graph databases treat the details of things as secondary, and optimise for managing the network of relationships. A graph database can quickly look up how things are related to each other, and return the&amp;nbsp;results.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Document databases organise data into documents, each containing a number of field-value pairs. each value can itself be a document, and multiple values/documents can be grouped under a field. Document databases do not enforce data consistency across documents, so those rules need to be managed by the application which is using the database. This allows document databases to continue operating even when partitioned, at the cost of some&amp;nbsp;consistency.&lt;/p&gt;
&lt;p&gt;In the past two issues, I laid out how relational databases primarily focus on the &lt;strong&gt;relations&lt;/strong&gt; between tables, while document databases primarily focus on organising data into &lt;strong&gt;documents&lt;/strong&gt;. I’ll look at one more application&amp;nbsp;today.&lt;/p&gt;
&lt;p&gt;If I’m trying to start a new social media platform today, I would have to store posts and user account data into a database. Which type of database should I&amp;nbsp;use?&lt;/p&gt;
&lt;p&gt;I could use a relational database, but joining multiple tables to get a chain of posts, Twitter-style, could get ugly and involve lots of lookups … that is going to be one laggy service at&amp;nbsp;scale!&lt;/p&gt;
&lt;p&gt;I could use a document database, but it would involve retrieving each post one at a time, searching to find posts which are linked to it, and then checking which posts are linked to those posts … that is too many&amp;nbsp;searches!&lt;/p&gt;
&lt;p&gt;Maybe I’m approaching this wrong. I don’t need to relate many different types of tables or retrieve self-contained documents here. I am actually trying to store a humongous, densely linked network of data—a&amp;nbsp;graph!&lt;/p&gt;
&lt;h2&gt;What?&lt;/h2&gt;
&lt;p&gt;Okay, stay with me here, I know you are thinking of a horizontal and a vertical axis, and axis labels and bars and lines and—that’s not the kind of graph I am talking&amp;nbsp;about.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“In mathematics, graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects.”&lt;br /&gt;
— &lt;a href="https://en.wikipedia.org/wiki/Graph_theory"&gt;Graph theory&amp;nbsp;(Wikipedia)&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That’s what I’m talking about. And it looks like&amp;nbsp;this:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Wikipedia multilingual network graph, showing circles representing languages, and arrows between pairs of circles, representing editors who edited both languages." src="https://ngjunsiang.github.io/laymansguide/issue089_01.png" /&gt;&lt;br /&gt;
&lt;em&gt;This network graph shows the co-editing patterns on Wikipedia. The size of the arrows indicate the number of Wikipedia editors for one language edition of Wikipedia, who also edited another language edition.&lt;br /&gt;Source: &lt;a href="https://en.wikipedia.org/wiki/File:Wikipedia_multilingual_network_graph_July_2013.svg"&gt;Wikimedia&amp;nbsp;Commons&lt;/a&gt;&lt;/em&gt;    &lt;/p&gt;
&lt;p&gt;Okay,&amp;nbsp;phew.&lt;/p&gt;
&lt;h2&gt;Graph databases: a network of&amp;nbsp;relationships&lt;/h2&gt;
&lt;p&gt;So if I’m going to make a social media platform that can retrieve chains of posts, how would a graph database make it&amp;nbsp;easier?&lt;/p&gt;
&lt;p&gt;A graph database will still need to have some data for the users and&amp;nbsp;posts:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;personA:User {name:&amp;quot;Alice&amp;quot;}
personB:User {name:&amp;quot;Bob&amp;quot;}
...
post001:Post {tags:&amp;quot;...&amp;quot;, contents:&amp;quot;...&amp;quot;}
post002:Post {tags:&amp;quot;...&amp;quot;, contents:&amp;quot;...&amp;quot;}
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But the heart of the graph database is the data that stores the relationships between those users and&amp;nbsp;posts:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;personA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;SAYS_TO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;personB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;personB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;SAYS_TO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;personA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If I want to lookup a conversation between Alice and Bob, I can search&amp;nbsp;for &lt;code&gt;SAYS_TO&lt;/code&gt; relationships with Alice and Bob at either end of the relationship arrow&amp;nbsp;(&lt;code&gt;--&amp;gt;&lt;/code&gt;), and sort the results in chronological&amp;nbsp;order.&lt;/p&gt;
&lt;h2&gt;Graph databases put relationships&amp;nbsp;first&lt;/h2&gt;
&lt;p&gt;What about posts and comments? For social media, we can treat them as the same type of data&amp;nbsp;(&lt;code&gt;Post&lt;/code&gt;), but link them with&amp;nbsp;relationships:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;personA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WROTE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post001&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;personB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WROTE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post003&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;personC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WROTE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post005&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;personD&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WROTE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post007&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;personA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WROTE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post011&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;personB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WROTE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post013&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;personA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WROTE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post017&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post003&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;REPLY_TO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post001&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post005&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;REPLY_TO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post003&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post007&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;REPLY_TO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post003&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post011&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;REPLY_TO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post005&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post013&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;REPLY_TO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post011&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post017&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;REPLY_TO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post013&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Because the relationships contain only the bare minimum data for figuring out the network, they are quick to search through. I don’t have to load the names, post tags, post contents, and other irrelevant&amp;nbsp;detail.&lt;/p&gt;
&lt;p&gt;Although I would still have to&amp;nbsp;retrieve &lt;code&gt;post001&lt;/code&gt;, check for replies, check those replies for replies, and so on, this is much faster with relationships between labels. A graph database optimises for this type of&amp;nbsp;lookup.&lt;/p&gt;
&lt;p&gt;Once I have figured out which users and posts are involved in this chain, I can then retrieve their information in a subsequent query. I won’t even need to load all the information at a go, since the app user is not going to see the contents of later posts until they&amp;nbsp;scroll.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Graph databases treat the details of things as secondary, and optimise for managing the network of relationships. A graph database can quickly look up how things are related to each other, and return the&amp;nbsp;results.&lt;/p&gt;
&lt;p&gt;So there you go, three types of databases in three weeks. I picked these three because they’re the least technical to give an overview of (in my opinion), and are three different ways of thinking about data that I think you are likely to&amp;nbsp;encounter.&lt;/p&gt;
&lt;p&gt;There are, of course, other types of databases: key-value stores (used heavily in web browsers), wide column databases, search databases (very similar to document-based), … but beyond this point the differences are primarily technical, and not really suitable for this&amp;nbsp;newsletter.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S7] Issue 90: Using a&amp;nbsp;database&lt;/p&gt;
&lt;p&gt;I’ve been cracking my head trying to come up with 2 more topics to round up this season on databases. I suppose most layfolks would (hopefully) never ever have to start or run their own database. But it could be helpful to know what is needed to get a database up and running, and the most common ways of getting access to one. Expect a short issue next&amp;nbsp;week.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 07"></category><category term="document"></category></entry><entry><title>Issue 88: Document Databases</title><link href="https://ngjunsiang.github.io/laymansguide/issue088.html" rel="alternate"></link><published>2020-09-19T08:00:00+08:00</published><updated>2020-09-19T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-09-19:/laymansguide/issue088.html</id><summary type="html">&lt;p&gt;Document databases organise data into documents, each containing a number of field-value pairs. Each value can itself be a document, and multiple values/documents can be grouped under a field. Document databases do not enforce data consistency across documents, so those rules need to be managed by the application which is using the database. This allows document databases to continue operating even when partitioned, at the cost of some&amp;nbsp;consistency.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Relational databases are designed to maintain a well-structured set of data tables through constraint rules. This makes them very useful for preventing accidental inconsistencies in data, but make any changes to the data schema difficult to implement. Changing from one schema to another involves downtime and a&amp;nbsp;migration.&lt;/p&gt;
&lt;p&gt;One problem I keep running into with Excel is when I &lt;em&gt;think&lt;/em&gt; the data has a consistent structure, but halfway through I realise that it actually doesn’t: sometimes I might have two students with different categories of accomplishments, and that requires a big change in the way I design the&amp;nbsp;columns.&lt;/p&gt;
&lt;p&gt;Document databases bypass this problem by not enforcing a strict schema on the data. That is not to say you can’t; it is &lt;em&gt;optional&lt;/em&gt; and up to you to&amp;nbsp;enforce.&lt;/p&gt;
&lt;h2&gt;Document databases: a collection of fields and&amp;nbsp;values&lt;/h2&gt;
&lt;p&gt;When we think of documents, we usually think of Office documents, or PDFs, or things that are … more associated with the way a workplace&amp;nbsp;works.&lt;/p&gt;
&lt;p&gt;These documents are not the ones I have in mind when talking about document databases. In these databases, &lt;strong&gt;documents&lt;/strong&gt; are simply bits of data grouped together. Each bit of data is described by a field. For example, I might start out defining a student document this&amp;nbsp;way:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;{
  name: &amp;quot;Harry Potter&amp;quot;,
  school: &amp;quot;Hogwarts School of Witchcraft and Wizardry&amp;quot;,
  characteristics: &amp;quot;lightning-shaped scar on forehead&amp;quot;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I can add more fields later, if I&amp;nbsp;wish:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;{
  name: &amp;quot;Harry Potter&amp;quot;,
  school: &amp;quot;Hogwarts School of Witchcraft and Wizardry&amp;quot;,
  characteristics: &amp;quot;lightning-shaped scar on forehead&amp;quot;
  mother: Lily Potter,
  father: James Potter,
  ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But what makes document databases truly &lt;em&gt;document-oriented&lt;/em&gt; is the way they can be nested. Suppose I want to expand a bit more on this student’s education, to include the years of study. I could expand each entry in&amp;nbsp;the &lt;code&gt;school&lt;/code&gt; field to include&amp;nbsp;that:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;{
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;:&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Harry Potter&amp;quot;&lt;/span&gt;,
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nv"&gt;school&lt;/span&gt;:&lt;span class="w"&gt; &lt;/span&gt;{
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;:&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Hogwarts School of Witchcraft and Wizardry&amp;quot;&lt;/span&gt;,
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;start&lt;/span&gt;:&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;1991&amp;quot;&lt;/span&gt;,
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;end&lt;/span&gt;:&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;1997&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;}
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nv"&gt;characteristics&lt;/span&gt;:&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;lightning-shaped scar on forehead&amp;quot;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Yup, now I’ve just expanded the value of&amp;nbsp;the &lt;code&gt;school&lt;/code&gt; field into &amp;#8230; another document! This document has&amp;nbsp;a &lt;code&gt;name&lt;/code&gt; field,&amp;nbsp;a &lt;code&gt;start&lt;/code&gt; field, and&amp;nbsp;an &lt;code&gt;end&lt;/code&gt; field. I can embed documents just about any place I&amp;nbsp;want.&lt;/p&gt;
&lt;p&gt;I can also group multiple values under a&amp;nbsp;field:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;{
  ...
  characteristics: [&amp;quot;wears glasses&amp;quot;, &amp;quot;lightning-shaped scar on forehead&amp;quot;]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I can also group multiple &lt;em&gt;documents&lt;/em&gt; under a field. It’s documents all the way&amp;nbsp;down!&lt;/p&gt;
&lt;h2&gt;Collections: the only way to organise&amp;nbsp;documents&lt;/h2&gt;
&lt;p&gt;While relational databases have tables for organising rows, document databases have collections for organising&amp;nbsp;documents.&lt;/p&gt;
&lt;p&gt;Each collection can contain multiple documents. There is no constraint on what kind of documents each collection can&amp;nbsp;contain.&lt;/p&gt;
&lt;p&gt;I could have a collection for teachers containing only teacher documents, a collection for students containing only student documents, a collection for subjects containing only subject documents, … or I could just have a collection for the department containing a mix of all three types of&amp;nbsp;documents.&lt;/p&gt;
&lt;h2&gt;What can I do with a document&amp;nbsp;database?&lt;/h2&gt;
&lt;p&gt;Just about &amp;#8230; anything? If you can think of a way to organise the data as documents, you can put it into a document&amp;nbsp;database.&lt;/p&gt;
&lt;p&gt;A document database lets you find documents based on its fields. I can look up all documents which have&amp;nbsp;a &lt;code&gt;name&lt;/code&gt; field, or check that the word &amp;#8220;Harry&amp;#8221; is in&amp;nbsp;the &lt;code&gt;name&lt;/code&gt; field. I could look for students who enrolled in the&amp;nbsp;year &lt;code&gt;"1991"&lt;/code&gt; or later, or more specifically students who enrolled&amp;nbsp;in &lt;code&gt;"Hogwarts School of Witchcraft and Wizardry"&lt;/code&gt; in &lt;code&gt;"1991"&lt;/code&gt; or&amp;nbsp;later.&lt;/p&gt;
&lt;h2&gt;Drawbacks&lt;/h2&gt;
&lt;p&gt;Since this is not a relational database, you don’t have the protection of foreign keys and other features that stop you from making the data inconsistent—there’s no concept of enforced consistency here! You’ll have to write those rules into your app when it accesses the document database; the database won’t enforce them for&amp;nbsp;you.&lt;/p&gt;
&lt;h2&gt;Advantages&lt;/h2&gt;
&lt;p&gt;Data organised as documents tends to be more self-contained. Since the database does not enforce consistency, it has less to worry about when edits or changes are made to the database. In a distributed document database, we thus sacrifice some consistency—unless we make pains to ensure it in our application&amp;nbsp;code.&lt;/p&gt;
&lt;p&gt;This does provide an advantage: when the distributed document database suffers a network outage, causing it to partition into multiple clusters (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue086.html"&gt;Issue 86&lt;/a&gt;)), the database can continue to operate. However, each cluster only has access to its own data, and not data on the other clusters. Over time, each cluster will become less and less consistent, since changes in each cluster are not synchronised to other&amp;nbsp;clusters.&lt;/p&gt;
&lt;p&gt;Once the network issue is resolved and the clusters are synchronised again, these changes can subsequently be merged following rules for resolving conflicts. The database remains operational throughout the ordeal, just with some&amp;nbsp;desynchronisation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Document databases organise data into documents, each containing a number of field-value pairs. Each value can itself be a document, and multiple values/documents can be grouped under a field. Document databases do not enforce data consistency across documents, so those rules need to be managed by the application which is using the database. This allows document databases to continue operating even when partitioned, at the cost of some&amp;nbsp;consistency.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S7] Issue 89: Graph&amp;nbsp;Databases&lt;/p&gt;
&lt;p&gt;Okay, relational and document databases were easy enough. They are more easily mapped to spreadsheets and file/folder hierarchies,&amp;nbsp;respectively.&lt;/p&gt;
&lt;p&gt;But now we go up the abstraction ladder, and get to more abstract ideas of data. In a social network, the user profile is usually the least significant part of the account; what often matters most is how this account is linked to other accounts (followers and following). The study of such interlinked objects is known in mathematics as &lt;strong&gt;graph theory&lt;/strong&gt; (nope, not the kind of graphs we are so used to in reports). This is where terms like “social graph”, the representation of your social network on Facebook or Twitter, comes&amp;nbsp;from.&lt;/p&gt;
&lt;p&gt;What is the most intuitive way to represent, store, and modify this kind of graph data? Using a graph database, of&amp;nbsp;course.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 07"></category><category term="document"></category></entry><entry><title>Issue 87: Relational Databases</title><link href="https://ngjunsiang.github.io/laymansguide/issue087.html" rel="alternate"></link><published>2020-09-12T08:00:00+08:00</published><updated>2020-09-12T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-09-12:/laymansguide/issue087.html</id><summary type="html">&lt;p&gt;Relational databases are designed to maintain a well-structured set of data tables through constraint rules. This makes them very useful for preventing accidental inconsistencies in data, but make any changes to the data schema difficult to implement. Changing from one schema to another involves downtime and a&amp;nbsp;migration.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; To increase the performance of a distributed database, we can scale up/scale vertically by increasing the computers’ performance, or scale out/scale horizontally by adding more computers. Distributed databases can only prioritise two of the following three factors: consistency, availability, partitioning (&lt;span class="caps"&gt;CAP&lt;/span&gt;&amp;nbsp;theorem).&lt;/p&gt;
&lt;p&gt;I’ve already discussed one big strength of relational databases in &lt;a href="https://ngjunsiang.github.io/laymansguide/issue084.html"&gt;Issue 84&lt;/a&gt;) when I illustrated how the &lt;span class="caps"&gt;JOIN&lt;/span&gt; keyword, one of many &lt;span class="caps"&gt;SQL&lt;/span&gt; commands (&lt;a href="https://ngjunsiang.github.io/laymansguide/issue083.html"&gt;Issue 83&lt;/a&gt;)), can join our data from multiple tables into a single view. This is where we look under the surface to see what makes that&amp;nbsp;possible.&lt;/p&gt;
&lt;h2&gt;Linking tables through foreign&amp;nbsp;keys&lt;/h2&gt;
&lt;p&gt;From &lt;a href="https://ngjunsiang.github.io/laymansguide/issue084.html"&gt;Issue 84&lt;/a&gt;):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;To join&amp;nbsp;the &lt;code&gt;Customer&lt;/code&gt; and &lt;code&gt;Sales&lt;/code&gt; data so that we get the sales data along&amp;nbsp;with &lt;code&gt;custName&lt;/code&gt;, we would write a &lt;span class="caps"&gt;SQL&lt;/span&gt; query like&amp;nbsp;this:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SELECT salesID, orderDate, custID FROM Sales
JOIN Customer ON Sales.custID = Customer.custID&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Here, &lt;code&gt;Sales.custID&lt;/code&gt; refers to&amp;nbsp;the &lt;code&gt;custID&lt;/code&gt; of&amp;nbsp;the &lt;code&gt;Sales&lt;/code&gt; table,&amp;nbsp;while &lt;code&gt;Customer.custID&lt;/code&gt; refers to&amp;nbsp;the &lt;code&gt;custID&lt;/code&gt; of&amp;nbsp;the &lt;code&gt;Customer&lt;/code&gt; table. This query effectively says “select&amp;nbsp;the &lt;code&gt;salesID&lt;/code&gt;, &lt;code&gt;orderDate&lt;/code&gt;,&amp;nbsp;and &lt;code&gt;custID&lt;/code&gt; columns&amp;nbsp;from &lt;code&gt;Sales&lt;/code&gt; table, and add data from&amp;nbsp;the &lt;code&gt;Customer&lt;/code&gt; table where&amp;nbsp;the &lt;code&gt;custID&lt;/code&gt; column matches”. This will&amp;nbsp;return:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Screenshot of an INNER JOIN operation between the Sales and Customer data tables, merged using custID values." src="https://ngjunsiang.github.io/laymansguide/issue084_04.png" /&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Did you catch the fact that there were actually &lt;em&gt;two&lt;/em&gt; &lt;code&gt;custID&lt;/code&gt; columns? One in&amp;nbsp;the &lt;code&gt;Sales&lt;/code&gt; table, and one in&amp;nbsp;the &lt;code&gt;Customer&lt;/code&gt; table &amp;#8230; by linking two tables like that, we actually introduce a point of potential&amp;nbsp;breakage.&lt;/p&gt;
&lt;p&gt;Suppose one day, a customer goes out of business, or changes name, and the&amp;nbsp;corresponding &lt;code&gt;Customer&lt;/code&gt; entry gets deleted. Now if we accidentally attempt to&amp;nbsp;retrieve &lt;code&gt;Sales&lt;/code&gt; to that customer, the &lt;span class="caps"&gt;SQL&lt;/span&gt; command will fail because it is unable to find the&amp;nbsp;entry.&lt;/p&gt;
&lt;p&gt;We can protect ourselves from this kind of error by&amp;nbsp;declaring &lt;code&gt;Sales.custID&lt;/code&gt; as a &lt;strong&gt;foreign key&lt;/strong&gt;&amp;nbsp;in &lt;code&gt;Customer&lt;/code&gt;, thus informing the database&amp;nbsp;that &lt;code&gt;Sales.custID&lt;/code&gt; is actually a column&amp;nbsp;from &lt;code&gt;Customer&lt;/code&gt;. If we attempt to delete that customer again, the database will help to check if that entry is referenced by other tables as a foreign key. Entries can only be deleted if they are not referenced by other&amp;nbsp;entries.&lt;/p&gt;
&lt;p&gt;These and other constraints allow us to protect ourselves from inadvertent harm, but over time, they accumulate and make a relational database very hard to modify. Database administrators will tell you to think about your database tables in advance, as even attempting to add a column or change a column type is going to be a pain in&amp;nbsp;future!&lt;/p&gt;
&lt;h2&gt;The tradeoff: downtime for database maintenance and&amp;nbsp;migrations&lt;/h2&gt;
&lt;p&gt;To modify a relational database, we have to shut it down&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;, and &lt;strong&gt;migrate&lt;/strong&gt; the database from the old schema to the new schema. In essence, we are exporting our data and re-importing it again. Attempting to migrate while the database is active—known as a &lt;strong&gt;live migration&lt;/strong&gt;—is strongly discouraged, as changing a database while a migration is in progress can introduce data inconsistency; a real headache with&amp;nbsp;constraints!&lt;/p&gt;
&lt;p&gt;Relational databases can also develop problems that require them to be shut down and rectified. It’s the tradeoff for having a consistent and structured way to store our data, and automated rules to enforce this&amp;nbsp;structure.&lt;/p&gt;
&lt;h2&gt;Relational databases: excellent for predictable data&amp;nbsp;needs&lt;/h2&gt;
&lt;p&gt;If you don’t expect to be changing your database schema often, or if you are able to design the schema to minimise such migrations, relational databases can be quite excellent for your needs. Please consult a professional database engineer if you are planning to use a database for your business&amp;nbsp;needs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Relational databases are designed to maintain a well-structured set of data tables through constraint rules. This makes them very useful for preventing accidental inconsistencies in data, but make any changes to the data schema difficult to implement. Changing from one schema to another involves downtime and a&amp;nbsp;migration.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S7] Issue 88: Document&amp;nbsp;Databases&lt;/p&gt;
&lt;p&gt;Relational databases work well for data that we can imagine as an Excel table. But often, we have data that might not share the same set of properties, or might not have a predictable structure (such as online collaboration data). Such data is more intuitively imagined as a set of documents than as a set of tables. What do databases that encourage a document-based model of data look&amp;nbsp;like?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;There are ways to avoid this, but I’ll let a &lt;strong&gt;real&lt;/strong&gt; database administrator tell you about how to make it happen.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 07"></category></entry><entry><title>Issue 86: Distributed databases</title><link href="https://ngjunsiang.github.io/laymansguide/issue086.html" rel="alternate"></link><published>2020-09-05T08:00:00+08:00</published><updated>2020-09-05T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-09-05:/laymansguide/issue086.html</id><summary type="html">&lt;p&gt;To increase the performance of a distributed database, we can scale up/scale vertically by increasing the computers’ performance, or scale out/scale horizontally by adding more computers. Distributed databases can only prioritise two of the following three factors: consistency, availability, partitioning (&lt;span class="caps"&gt;CAP&lt;/span&gt;&amp;nbsp;theorem).&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Forms that naïvely inject user-submitted data into a &lt;span class="caps"&gt;SQL&lt;/span&gt; query template may end up sending valid &lt;span class="caps"&gt;SQL&lt;/span&gt; commands to the database, with disastrous&amp;nbsp;consequences.&lt;/p&gt;
&lt;p&gt;So far, we have been assuming that the database runs from a single computer, and all its data is stored on one as well. What happens when it outgrows this single&amp;nbsp;computer?&lt;/p&gt;
&lt;p&gt;We could add more disk space, more memory, more cores on the processor; this is called &lt;strong&gt;vertical scaling&lt;/strong&gt;/&lt;strong&gt;scaling up&lt;/strong&gt; (because we are increasing the performance of the computer, which usually &lt;em&gt;feels&lt;/em&gt; like pushing up the performance bar on the vertical axis of a&amp;nbsp;graph).&lt;/p&gt;
&lt;p&gt;Or we could spread that database over two or more computers. And keep them constantly synchronised. This is called &lt;strong&gt;horizontal scaling&lt;/strong&gt;/&lt;strong&gt;scaling out&lt;/strong&gt; (because we are adding more computers, which is usually depicted as adding more units on a horizontal&amp;nbsp;axis).&lt;/p&gt;
&lt;p&gt;We can only take vertical scaling so far; at some point we will have the most powerful server possible and it still won’t be enough. So if we are expecting massive growth, that means we will need a &lt;strong&gt;distributed database&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;Wait, who actually expects a database to not have to store &lt;em&gt;a lot&lt;/em&gt; of&amp;nbsp;information?&lt;/h2&gt;
&lt;p&gt;There are tiny databases out&amp;nbsp;there!&lt;/p&gt;
&lt;p&gt;These are often used in places where the task is not expected to grow beyond a single &lt;span class="caps"&gt;PC&lt;/span&gt;. For example, the database that stores your WhatsApp messages on your mobile phone, or a tiny database that stores records from a remote standalone sensor. These databases are designed to be extremely efficient at handling small amounts of data, to use very little memory, and/or to ensure that data is always written&amp;nbsp;securely.&lt;/p&gt;
&lt;h2&gt;Okay, fine. Back to distributed&amp;nbsp;databases&lt;/h2&gt;
&lt;p&gt;Buying more computers to run a server is similar to hiring more employees to do the company’s work. The good: you now have more help. The bad: you now have to talk to them!&amp;nbsp;Regularly!&lt;/p&gt;
&lt;p&gt;In distributed databases, there are three factors that are impossible to achieve together in&amp;nbsp;full:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;C&lt;/strong&gt;onsistency — reading the same data multiple times should not give us different&amp;nbsp;results&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A&lt;/strong&gt;vailability — we should get a response from the database&amp;nbsp;quickly&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;P&lt;/strong&gt;artition tolerance — If network disruptions or software/hardware failures break communication, our cluster of servers break up into smaller clusters—they get partitioned. Computers in each subcluster can communicate with each other, but not with computers outside the subcluster. Under such conditions, the system should still continue to&amp;nbsp;operate.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is known as the &lt;strong&gt;&lt;span class="caps"&gt;CAP&lt;/span&gt; theorem&lt;/strong&gt;: you can only really prioritise two out of the three&amp;nbsp;factors.&lt;/p&gt;
&lt;h2&gt;Consistency and&amp;nbsp;Availability&lt;/h2&gt;
&lt;p&gt;The database we have been examining so far in Season 7 are known as relational databases, which handle data in the form of tables. When implemented as a distributed database, they often prioritise consistency and&amp;nbsp;availability.&lt;/p&gt;
&lt;p&gt;How does that work? When our distributed database is being hit with 100,000s of requests per second, more than one computer can handle, we need multiple computers to serve these requests. These computers had better be synchronised (to achieve consistency) so that the request will always return the same response from any of those&amp;nbsp;computers.&lt;/p&gt;
&lt;p&gt;One way to achieve this is to have a Single Source of Truth: perhaps we design it so that only one “leader” computer handles edits/changes to the database, which then get sent to all the other “follower” computers. (This assumption that reading data occurs much more frequently than writing/changing data holds up for most use cases.) What happens if the “leader” computer goes down, and our distributed database goes from a leader-follower system to a partitioned bunch of followers? No writes can happen, the system is no longer&amp;nbsp;operational.&lt;/p&gt;
&lt;p&gt;(There are multiple theorems on how to design this system to automatically/manually select a new leader, but I won’t go into that here. The fundamental problem of ensuring consistency and availability in such cases&amp;nbsp;remains.)&lt;/p&gt;
&lt;h2&gt;When a partition&amp;nbsp;happens&lt;/h2&gt;
&lt;p&gt;So it comes down to this: when communication failure happens in a scenario like the above, we have to&amp;nbsp;choose.&lt;/p&gt;
&lt;p&gt;If we need a workaround to ensure that updates on one computer still reaches all the computers so that the data is consistent, that is going to be slow — we lose&amp;nbsp;availability.&lt;/p&gt;
&lt;p&gt;If we want to achieve availability, we could have each computer just return or update the data it has, then worry about synchronisation later — we lose&amp;nbsp;consistency.&lt;/p&gt;
&lt;p&gt;If you find yourself in the position of having to choose a distributed database, it would be immensely helpful to know upfront which 2 factors you want to&amp;nbsp;prioritise!&lt;/p&gt;
&lt;h2&gt;Examples&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Consistency and Availability&lt;/strong&gt;: Bank databases fall in this category. Financial transactions must be accurate, and people need to quickly know whether they were successful. So we have to live with these databases requiring regular maintenance (usually late at night) to minimise the risk of partitioning&amp;nbsp;failure.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Partitioning and Consistency&lt;/strong&gt;: Authentication systems are relied upon to ensure that data is only accessed by people who are authorised to do so, and cannot afford to go down for long periods of time. This requires that permissions be properly synchronised across all computers, so consistency is key. These two factors are more important than ensuring a speedy&amp;nbsp;response.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Partitioning and Availability&lt;/strong&gt;: Essential services, such as Google Maps, have to remain operational even with (recoverable) failures, and still have to respond in a reasonable amount of time (otherwise real-time navigation would fail). Roads do not change often, so it is okay if the info we are getting is slightly out of date; we might occasionally get a slower route or find ourselves at a business whose operating hours are not updated in Google Maps, but these are not critical&amp;nbsp;failures.&lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;CAP&lt;/span&gt; theorem does not say we can never have the third factor! It means we have to pick 2 factors to prioritise, and live with the lowered performance of the&amp;nbsp;third.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; To increase the performance of a distributed database, we can scale up/scale vertically by increasing the computers’ performance, or scale out/scale horizontally by adding more computers. Distributed databases can only prioritise two of the following three factors: consistency, availability, partitioning (&lt;span class="caps"&gt;CAP&lt;/span&gt;&amp;nbsp;theorem).&lt;/p&gt;
&lt;p&gt;This actually ran longer than I expected; the examples were an unplanned addition that I think helps to clarify use cases for each&amp;nbsp;combination.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S7] Issue 87: Relational&amp;nbsp;Databases&lt;/p&gt;
&lt;p&gt;I’ll spend the next 3 issues talking about 3 major types of databases in use today. This isn’t strictly layman content, but I suspect in some non-technical conversations these terms may pop up. More importantly, I think the 3 major types cover 3 different concepts of data, and I hope that elaborating on these in a little bit more detail will help to develop a more nuanced way of thinking about&amp;nbsp;data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 07"></category></entry><entry><title>Issue 85: SQL Injections</title><link href="https://ngjunsiang.github.io/laymansguide/issue085.html" rel="alternate"></link><published>2020-08-29T08:00:00+08:00</published><updated>2020-08-29T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-08-29:/laymansguide/issue085.html</id><summary type="html">&lt;p&gt;Forms that naïvely inject user-submitted data into a &lt;span class="caps"&gt;SQL&lt;/span&gt; query template may end up sending valid (but otherwise unathorised) &lt;span class="caps"&gt;SQL&lt;/span&gt; commands to the database, with disastrous&amp;nbsp;consequences.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; &lt;span class="caps"&gt;SQL&lt;/span&gt; queries let you join multiple tables based on specified conditions using the &lt;span class="caps"&gt;JOIN&lt;/span&gt; keyword. This enables crafting complex queries to return only the specific data that is&amp;nbsp;required.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;SQL&lt;/span&gt; databases are really powerful; this is usually a good thing since it allows developers to do amazing things with the data inside. But it can also lead to disastrous consequences in the unsupervised hands of inexperienced developers. And matters can be even worse if these powers are not carefully granted. A malicious actor could “borrow” these powers to wreak havoc on the&amp;nbsp;database!&lt;/p&gt;
&lt;p&gt;&lt;img alt="XKCD comic: Exploits of a Mom" src="https://imgs.xkcd.com/comics/exploits_of_a_mom.png" /&gt;&lt;br/&gt;
&lt;small&gt;&lt;a href="https://xkcd.com/327/"&gt;Relevant xkcd comic&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;
&lt;h2&gt;Adding data to an &lt;span class="caps"&gt;SQL&lt;/span&gt;&amp;nbsp;database&lt;/h2&gt;
&lt;p&gt;Adding data to an &lt;span class="caps"&gt;SQL&lt;/span&gt; database is easy. If&amp;nbsp;our &lt;code&gt;Customer&lt;/code&gt; table looks like this (from &lt;a href="https://ngjunsiang.github.io/laymansguide/issue084.html"&gt;Issue 84&lt;/a&gt;)):&lt;/p&gt;
&lt;p&gt;&lt;img alt="Screenshot of a Customer data table, with custID, custName, custEmail, and custContact columns." src="https://ngjunsiang.github.io/laymansguide/issue084_01.png" /&gt;&lt;/p&gt;
&lt;p&gt;The relevant &lt;span class="caps"&gt;SQL&lt;/span&gt; query to add another customer&amp;nbsp;is:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;INTO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Customer&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Ernest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ernest&lt;/span&gt;&lt;span class="nv"&gt;@lmn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;57564986&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What could go&amp;nbsp;wrong?&lt;/p&gt;
&lt;h2&gt;Interacting with an &lt;span class="caps"&gt;SQL&lt;/span&gt;&amp;nbsp;database&lt;/h2&gt;
&lt;p&gt;The most direct way of managing and interacting with a database is through its commandline tool. Needless to say, this is not how you would want your users using it. It’s just a terrible user experience, and gives them &lt;em&gt;waaaay&lt;/em&gt; too much&amp;nbsp;power.&lt;/p&gt;
&lt;p&gt;So we usually design a frontend—an app, webpage, or database form—that formats and lays out the data nicely for them, and limits the things they can do to the data. This frontend will usually only allow users to edit or delete existing data, and add new data. Then it constructs an &lt;span class="caps"&gt;SQL&lt;/span&gt; query to be sent to the database. The code to do this might look like the&amp;nbsp;following:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;custName = request.form[&amp;#39;custName&amp;#39;]
custEmail = request.form[&amp;#39;custEmail&amp;#39;]
custContact = request.form[&amp;#39;custContact&amp;#39;]
sql.execute(f&amp;#39;INSERT INTO Customer VALUES ({custName}, {custEmail}, {custContact})&amp;#39;)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code naïvely inserts data from the submitted form into the database without any checks. That’s not smart; the contact number might have the wrong number of digits, the email might not even have an &amp;#8216;@&amp;#8217;, and people often type the wrong things in the wrong&amp;nbsp;fields.&lt;/p&gt;
&lt;p&gt;What else could go&amp;nbsp;wrong?&lt;/p&gt;
&lt;h2&gt;&lt;span class="caps"&gt;SQL&lt;/span&gt; Injections: sending &lt;span class="caps"&gt;SQL&lt;/span&gt; commands through an unsecured&amp;nbsp;form&lt;/h2&gt;
&lt;p&gt;A malicious/clever user might attempt to submit the following form&amp;nbsp;data:&lt;/p&gt;
&lt;p&gt;Customer Name: Ernest
Customer Email: ernest@lmn.com
Customer Contact: 10); &lt;span class="caps"&gt;DROP&lt;/span&gt; &lt;span class="caps"&gt;TABLE&lt;/span&gt;&amp;nbsp;Customers&amp;#8212;&lt;/p&gt;
&lt;p&gt;Why would they do that? When inserted into the template above, the full &lt;span class="caps"&gt;SQL&lt;/span&gt; query&amp;nbsp;becomes:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;INTO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Customer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Ernest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ernest&lt;/span&gt;&lt;span class="nv"&gt;@lmn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;DROP&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;TABLE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Customers&lt;/span&gt;&lt;span class="c1"&gt;--)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Two things to explain:
- the semicolon&amp;nbsp;(&lt;code&gt;;&lt;/code&gt;) indicates the end of an &lt;span class="caps"&gt;SQL&lt;/span&gt; query. It is used to write two or more queries in one line.
- The database ignores everything after&amp;nbsp;the &lt;code&gt;--&lt;/code&gt;. It is a useful way to add comments to &lt;span class="caps"&gt;SQL&lt;/span&gt; queries (for human consumption) &amp;#8230; or to make the database ignore invalid syntax (such as the&amp;nbsp;standalone &lt;code&gt;)&lt;/code&gt;), which is what happens in this&amp;nbsp;case.&lt;/p&gt;
&lt;p&gt;So the database ends up executing&amp;nbsp;this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;INTO&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Customer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Ernest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ernest&lt;/span&gt;&lt;span class="nv"&gt;@lmn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;TABLE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Customers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Goodbye, &lt;code&gt;Customer&lt;/code&gt; table&amp;nbsp;&amp;#8230;&lt;/p&gt;
&lt;h2&gt;Data leakage through &lt;span class="caps"&gt;SQL&lt;/span&gt;&amp;nbsp;injections&lt;/h2&gt;
&lt;p&gt;This app is probably going to have some kind of search or filtering feature, where we enter a name to search for and get results that match. If we were searching for a user named George, an inexperienced developer might send this as the &lt;span class="caps"&gt;SQL&lt;/span&gt;&amp;nbsp;query:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;SELECT * FROM Customer WHERE custName = George
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If I submit the following in the search&amp;nbsp;box:&lt;/p&gt;
&lt;p&gt;Customer Name: George &lt;span class="caps"&gt;OR&lt;/span&gt;&amp;nbsp;1=1&lt;/p&gt;
&lt;p&gt;It might get naïvely substituted to form the following&amp;nbsp;query:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;SELECT * FROM Customer WHERE custName = George OR 1=1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The database will attempt to parse this, and come&amp;nbsp;across &lt;code&gt;custName = George OR 1=1&lt;/code&gt;. It gets interpreted as “return all results&amp;nbsp;from &lt;code&gt;Customer&lt;/code&gt; table where&amp;nbsp;the &lt;code&gt;custName&lt;/code&gt; column matches the result&amp;nbsp;of &lt;code&gt;George OR 1=1&lt;/code&gt;”.&lt;/p&gt;
&lt;p&gt;It will then attempt to&amp;nbsp;evaluate &lt;code&gt;George OR 1=1&lt;/code&gt;. By the unintuitive reasoning of computer logic, this always evaluates to True, and results in the database returning &amp;#8230; all the rows&amp;nbsp;in &lt;code&gt;Customer&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;If you’re going to use a database with a frontend, get an experienced developer to do it. If all you have are inexperienced developers, send them for the appropriate training. If you don’t have developers, use an established product over an untested one. If in doubt, find someone with the relevant credentials to ask for&amp;nbsp;advice.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Forms that naïvely inject user-submitted data into a &lt;span class="caps"&gt;SQL&lt;/span&gt; query template may end up sending valid (but otherwise unathorised) &lt;span class="caps"&gt;SQL&lt;/span&gt; commands to the database, with disastrous&amp;nbsp;consequences.&lt;/p&gt;
&lt;p&gt;This would have been 3–5 times as long if I had started going into some basic ways to prevent this kind of mistake. Fortunately, this is just a layman’s guide, and I can foist that responsibility off to the rest of the&amp;nbsp;internet.&lt;/p&gt;
&lt;p&gt;On a serious note, database security is a whole field of study. If you are using a database for enterprise purposes, please give database security the resources it needs; there are just so many ways that things can go&amp;nbsp;wrong!&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S7] Issue 86: Distributed&amp;nbsp;databases&lt;/p&gt;
&lt;p&gt;So far, we have been assuming that the database runs from a single computer, and all its data is stored on one as well. What happens when it outgrows this single computer? Why, it then gets transmitted and infects another computer &amp;#8230; just kidding, we then have to spread that database over two or more computers. And keep them constantly synchronised. If that sounds like a pain, you are exactly right! More on this next&amp;nbsp;issue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 07"></category></entry><entry><title>Issue 84: JOIN – supercharged VLOOKUP</title><link href="https://ngjunsiang.github.io/laymansguide/issue084.html" rel="alternate"></link><published>2020-08-22T08:00:00+08:00</published><updated>2020-08-22T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-08-22:/laymansguide/issue084.html</id><summary type="html">&lt;p&gt;&lt;span class="caps"&gt;SQL&lt;/span&gt; queries let you join multiple tables based on specified conditions using the &lt;span class="caps"&gt;JOIN&lt;/span&gt; keyword. This enables crafting complex queries to return only the specific data that is&amp;nbsp;required.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; Structured Query Language (&lt;span class="caps"&gt;SQL&lt;/span&gt;) is a computer language for managing data in databases. It has keywords and keyphrases that let you filter rows and columns, group and order data, perform basic arithmetic on data, and more. It is complex and powerful, but astute and efficient use requires specialised&amp;nbsp;training.&lt;/p&gt;
&lt;h2&gt;&lt;span class="caps"&gt;VLOOKUP&lt;/span&gt;: The bread-and-butter of&amp;nbsp;spreadsheets&lt;/h2&gt;
&lt;p&gt;If I have&amp;nbsp;a &lt;code&gt;Customer&lt;/code&gt; data table that looks like&amp;nbsp;this:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Screenshot of a Customer data table, with custID, custName, custEmail, and custContact columns." src="https://ngjunsiang.github.io/laymansguide/issue084_01.png" /&gt;&lt;/p&gt;
&lt;p&gt;And&amp;nbsp;a &lt;code&gt;Sales&lt;/code&gt; data table that looks like&amp;nbsp;this:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Screenshot of a Sales data table, with salesID, orderDate, and custID columns." src="https://ngjunsiang.github.io/laymansguide/issue084_02.png" /&gt;&lt;/p&gt;
&lt;p&gt;I could add&amp;nbsp;a &lt;code&gt;custName&lt;/code&gt; column to the sales table that &lt;em&gt;looks up&lt;/em&gt;&amp;nbsp;the &lt;code&gt;custID&lt;/code&gt;, and inserts&amp;nbsp;the &lt;code&gt;custName&lt;/code&gt; info from the same row. This feature of spreadsheets is known as &lt;strong&gt;&lt;span class="caps"&gt;VLOOKUP&lt;/span&gt;&lt;/strong&gt; (vertical lookup)&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;. This is what the formula for each cell&amp;nbsp;in &lt;code&gt;custName&lt;/code&gt; would look&amp;nbsp;like:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Screenshot of a Sales data table, with salesID, orderDate, and custID columns." src="https://ngjunsiang.github.io/laymansguide/issue084_03.png" /&gt;&lt;/p&gt;
&lt;p&gt;Let’s break down each part of that&amp;nbsp;formula:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;=VLOOKUP(C2,Customer!A:D,2)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This means “in columns &lt;strong&gt;A:D&lt;/strong&gt; of the &lt;strong&gt;Customer&lt;/strong&gt; table, look for the value from cell &lt;strong&gt;C2&lt;/strong&gt; (which&amp;nbsp;is &lt;code&gt;1&lt;/code&gt;) in the first column of the &lt;strong&gt;Customer&lt;/strong&gt; table, and return the value from the same-row cell in the &lt;strong&gt;2&lt;/strong&gt;nd column of the &lt;strong&gt;Customer&lt;/strong&gt;&amp;nbsp;table.”&lt;/p&gt;
&lt;p&gt;What if you needed to insert more than one column? What if you need to “join” two or more tables? Your spreadsheet would soon be filled with &lt;span class="caps"&gt;VLOOKUP&lt;/span&gt; cells, and this really slows down the performance of the spreadsheet. This method is not suitable for data involving millions of rows, for&amp;nbsp;sure.&lt;/p&gt;
&lt;h2&gt;&lt;span class="caps"&gt;SQL&lt;/span&gt; &lt;span class="caps"&gt;JOIN&lt;/span&gt;: &lt;span class="caps"&gt;VLOOKUP&lt;/span&gt; on&amp;nbsp;steroids&lt;/h2&gt;
&lt;p&gt;In a database, there is no “standard view” of the data. All data you want to see has to be retrieved with a &lt;strong&gt;query&lt;/strong&gt;. So it makes no sense to require cells filled with &lt;span class="caps"&gt;VLOOKUPS&lt;/span&gt;; we just need to figure out how to do the equivalent in a query. The keyword for that is called &lt;strong&gt;&lt;span class="caps"&gt;JOIN&lt;/span&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;To join&amp;nbsp;the &lt;code&gt;Customer&lt;/code&gt; and &lt;code&gt;Sales&lt;/code&gt; data so that we get the sales data along&amp;nbsp;with &lt;code&gt;custName&lt;/code&gt;, we would write a &lt;span class="caps"&gt;SQL&lt;/span&gt; query like&amp;nbsp;this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;SELECT salesID, orderDate, custID FROM Sales
JOIN Customer ON Sales.custID = Customer.custID
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, &lt;code&gt;Sales.custID&lt;/code&gt; refers to&amp;nbsp;the &lt;code&gt;custID&lt;/code&gt; of&amp;nbsp;the &lt;code&gt;Sales&lt;/code&gt; table,&amp;nbsp;while &lt;code&gt;Customer.custID&lt;/code&gt; refers to&amp;nbsp;the &lt;code&gt;custID&lt;/code&gt; of&amp;nbsp;the &lt;code&gt;Customer&lt;/code&gt; table. This query effectively says “select&amp;nbsp;the &lt;code&gt;salesID&lt;/code&gt;, &lt;code&gt;orderDate&lt;/code&gt;,&amp;nbsp;and &lt;code&gt;custID&lt;/code&gt; columns&amp;nbsp;from &lt;code&gt;Sales&lt;/code&gt; table, and add data from&amp;nbsp;the &lt;code&gt;Customer&lt;/code&gt; table where&amp;nbsp;the &lt;code&gt;custID&lt;/code&gt; column matches”. This will&amp;nbsp;return:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Screenshot of an INNER JOIN operation between the Sales and Customer data tables, merged using custID values." src="https://ngjunsiang.github.io/laymansguide/issue084_04.png" /&gt;&lt;/p&gt;
&lt;p&gt;That is much easier—once you’ve been trained in &lt;span class="caps"&gt;SQL&lt;/span&gt; syntax—than writing separate &lt;span class="caps"&gt;VLOOKUP&lt;/span&gt; formulas for each column you want, and having to maintain a whole table of&amp;nbsp;formulas!&lt;/p&gt;
&lt;p&gt;You can even join more than two tables together with a query&amp;nbsp;like:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;SELECT salesID, orderDate, custID, invoiceID, Customer.custName, Customer.custContact, invoiceDate, invoiceAmt FROM Sales
JOIN Customer ON Sales.custID = Customer.custID
JOIN Invoice ON Sales.invoiceID = Invoice.invoiceID
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is barely scratching the surface of what &lt;span class="caps"&gt;SQL&lt;/span&gt; can do; it has at least 4 types of JOINs, and many more ways of crafting queries to return specifically the data you&amp;nbsp;want.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;SQL&lt;/span&gt; queries are a whole different way of talking to your computer, and they can be really frustrating to write for people who are new to it. But they are behind many of the interfaces you see, which seem to seamlessly pull data from multiple sources together into a coherent&amp;nbsp;view.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; &lt;span class="caps"&gt;SQL&lt;/span&gt; queries let you join multiple tables based on specified conditions using the &lt;span class="caps"&gt;JOIN&lt;/span&gt; keyword. This enables crafting complex queries to return only the specific data that is&amp;nbsp;required.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S7] Issue 85: &lt;span class="caps"&gt;SQL&lt;/span&gt;&amp;nbsp;injections&lt;/p&gt;
&lt;p&gt;Databases are immensely powerful software systems when it comes to searching for information. One recurring challenge that all admins face is ensuring that only authorised use is permitted; how do we prevent malicious activity from being able to access the&amp;nbsp;database?&lt;/p&gt;
&lt;p&gt;Next week, I will introduce a common &lt;strong&gt;vulnerability&lt;/strong&gt; that web developers always have to guard against: &lt;span class="caps"&gt;SQL&lt;/span&gt;&amp;nbsp;injection.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;There is an equivalent feature for columns known as &lt;span class="caps"&gt;HLOOKUP&lt;/span&gt; (horizontal lookup) that looks up info in a row and inserts data from the same column, but it is not as popular. So the &lt;span class="caps"&gt;VLOOKUP&lt;/span&gt; name is more commonly used for this kind of operation.&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Season 07"></category></entry><entry><title>Issue 83: Structured Query Language</title><link href="https://ngjunsiang.github.io/laymansguide/issue083.html" rel="alternate"></link><published>2020-08-08T08:00:00+08:00</published><updated>2020-08-08T08:00:00+08:00</updated><author><name>J S Ng</name></author><id>tag:ngjunsiang.github.io,2020-08-08:/laymansguide/issue083.html</id><summary type="html">&lt;p&gt;Structured Query Language (&lt;span class="caps"&gt;SQL&lt;/span&gt;) is a computer language for managing data in databases. It has keywords and keyphrases that let you filter rows and columns, group and order data, perform basic arithmetic on data, and more. It is complex and powerful, but using it in an astute and efficient manner requires specialised&amp;nbsp;training.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Previously:&lt;/strong&gt; A database system follows rules that enable multiple users to send commands to the database at the same time. The system attempts to execute each action one at a time, locking data that is in use by other users, and ensuring that each user does not carry out actions that they are not permitted to. Such systems are better able to prevent data corruption compared to a text-based&amp;nbsp;system.&lt;/p&gt;
&lt;p&gt;Have you experienced the pain of having really huge tables in your spreadsheet, sometimes spanning more than a hundred columns? Then you might know how painful it can be trying to filter data from it, e.g. if your boss just wants a few columns of info from certain rows. Like if he asks for the performance numbers of employees who are up for&amp;nbsp;promotion.&lt;/p&gt;
&lt;p&gt;In a spreadsheet, you would have to apply filters&amp;nbsp;for &lt;code&gt;nextPromoYear&lt;/code&gt; to only show the appropriate rows, then you&amp;#8217;ll have to hide all the other irrelevant columns. Or you&amp;#8217;d just copy all more-than-a-hundred columns for those rows into another new spreadsheet, and manually delete the unnecessary&amp;nbsp;columns.&lt;/p&gt;
&lt;p&gt;Database designers don’t want to to do that. You should be able to ask the database to do this querying and filtering for you, and return you only the data you want. But how would that be&amp;nbsp;designed?&lt;/p&gt;
&lt;h2&gt;Structured Query Language: the universal database&amp;nbsp;language&lt;/h2&gt;
&lt;p&gt;Structured Query Language (&lt;span class="caps"&gt;SQL&lt;/span&gt;) is another computer language designed to manage data in databases. It reads &lt;em&gt;almost&lt;/em&gt; like English, but more logical and less poetic. It has its own syntax and grammar, which are not the same as in English. And sending a proper &lt;span class="caps"&gt;SQL&lt;/span&gt; query to any database that supports it will get you what you&amp;nbsp;want.&lt;/p&gt;
&lt;p&gt;Here’s what an &lt;span class="caps"&gt;SQL&lt;/span&gt; query for the above info might look&amp;nbsp;like:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;SELECT employeeName, teamName, salesCount, salesTotal FROM SalesData
WHERE nextPromoYear = 2020
GROUP BY teamName
ORDER BY salesTotal;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;SELECT&lt;/code&gt; keyword lets you filter only the columns you&amp;nbsp;want &lt;code&gt;FROM&lt;/code&gt; a&amp;nbsp;table&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;WHERE&lt;/code&gt; keyword lets you filter only the rows you want, based on one or more&amp;nbsp;criteria&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;GROUP BY&lt;/code&gt; keyphrase lets you group the returned data based on values in a&amp;nbsp;column&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;ORDER BY&lt;/code&gt; keyphrase lets you sort the returned results according to values in a&amp;nbsp;column&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;A database has no “main&amp;nbsp;view”&lt;/h2&gt;
&lt;p&gt;One difficulty many people have in “upgrading” from a spreadsheet mindset to a database mindset is that they expect to have a “main spreadsheet” where (almost) all the data lives, and where sub-spreadsheets pull data from. In a database, all data lives in separate tables, and are joined only when a query is executed. The only way to get data from a database is to use&amp;nbsp;queries!&lt;/p&gt;
&lt;p&gt;Most websites or software you are using which retrieves data for you usually end up executing one or more queries such as the above to get that data. And the job of the database software is to interpret such commands, pull the data from the various tables together, collate it correctly, and send it to&amp;nbsp;you.&lt;/p&gt;
&lt;h2&gt;A database can give you almost exactly what you&amp;nbsp;want&lt;/h2&gt;
&lt;p&gt;By using these and many other keywords and keyphrases, it is possible to put together a query that gives you only the data you want. &lt;span class="caps"&gt;SQL&lt;/span&gt; has arithmetic functions such as count, average, sum, and it can even return only unique&amp;nbsp;values.&lt;/p&gt;
&lt;p&gt;The tradeoff is that you have to learn another language, and use it regularly enough to understand the ins and outs. This is why every big corporation has a data team that can do&amp;nbsp;this!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Issue summary:&lt;/strong&gt; Structured Query Language (&lt;span class="caps"&gt;SQL&lt;/span&gt;) is a computer language for managing data in databases. It has keywords and keyphrases that let you filter rows and columns, group and order data, perform basic arithmetic on data, and more. It is complex and powerful, but using it in an astute and efficient manner requires specialised&amp;nbsp;training.&lt;/p&gt;
&lt;h2&gt;What I’ll be covering&amp;nbsp;next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Next issue:&lt;/strong&gt; [&lt;span class="caps"&gt;LMG&lt;/span&gt; S7] Issue 84: &lt;span class="caps"&gt;JOIN&lt;/span&gt; – supercharged &lt;span class="caps"&gt;VLOOKUP&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I haven’t even touched on &lt;span class="caps"&gt;SQL&lt;/span&gt;’s really powerful features yet. Filtering data from a table is fine, but if my data is spread across many tables, how do I pull that data together? Excel folks have a command they rely on heavily to do this, and it is&amp;nbsp;called &lt;code&gt;VLOOKUP&lt;/code&gt;. I’ll show you the &lt;span class="caps"&gt;SQL&lt;/span&gt; version next&amp;nbsp;issue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sometime in the future:&lt;/strong&gt; What&amp;nbsp;is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;booting up? [Issue&amp;nbsp;15]&lt;/li&gt;
&lt;li&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;? [Issue&amp;nbsp;8]&lt;/li&gt;
&lt;li&gt;a good reason developers write code and give it away for free online? [Issue&amp;nbsp;21]&lt;/li&gt;
&lt;li&gt;firmware? [Issue&amp;nbsp;34]&lt;/li&gt;
&lt;li&gt;OpenType? And what are fonts anyway? [Issue&amp;nbsp;42]&lt;/li&gt;
&lt;li&gt;What is involved in installing a piece of software? [Issue&amp;nbsp;48]&lt;/li&gt;
&lt;li&gt;How do apps know where a file starts and ends? [Issue&amp;nbsp;49]&lt;/li&gt;
&lt;li&gt;What is a password hash? [Issue&amp;nbsp;63]&lt;/li&gt;
&lt;/ul&gt;</content><category term="Season 07"></category></entry></feed>