Why Golang (Go) so Fast?

Why Golang (Go) so Fast?

Go is praised by everyone for achieving extremely high concurrent performance, but Why? In this Golang (Go) tutorial, I’ll introduce how the Go program to achieve extremely high concurrent performance, and its internal scheduler implementation architecture (G-P-M model). I’ll also explain how Go can make full use of computing resources, and how the Go scheduler deals with threads step by step

In this article, I’ll introduce how the Go program to achieve extremely high concurrent performance, and its internal scheduler implementation architecture (G-P-M model). I’ll also explain how Go can make full use of computing resources, and how the Go scheduler deals with threads step by step:

  • Introduce concurrency, explain concept related in the following article
  • Introduce Go GPM model
  • Describe cases about Go scheduler deals multiple threads

Introduce Concurrency

With the rapid development of information technology, the processing power of a single server is getting stronger, forcing the programming mode to upgrade from the previous serial to the concurrent.

The concurrency model includes IO multiplexing, multi-process, and multi-threading. These models have their advantages and disadvantages. Most of the modern complex high-concurrency architectures are used in conjunction with different models. Different models are used in different scenarios to promote strengths and avoid weaknesses to achieve maximum performance.

Multi-threading, because of its lightweight and ease of use, has become the most frequently used concurrency model in concurrent programming, including other sub-products such as post-derived coroutines.

Concurrency ≠ Parallelism

On a single CPU core, threads switch tasks through time slices or give up control to achieve the purpose of running multiple tasks “simultaneously.” It is called concurrency. But in fact, only one task is executed at any time, and the others wait in the queue.

Multi-core CPUs can allow “multiple threads” in the same process to run simultaneously, which is parallelism.

Process, Thread, Coroutine

  • Process: Process is the basic unit of resource allocation by the system, with independent memory space.
  • Threads: Thread is the basic unit of CPU scheduling and dispatch. Threads depend on the existence of processes, and each thread shares the resources of the parent process.
  • Coroutine: Coroutine is a lightweight thread in user mode. The user completely controls the scheduling of coroutine. Shifting between coroutines only needs to save the context of the task without the overhead of the kernel.

Thread Context Switch

Interrupt processing, multi-tasking, user-mode switching, and other reasons will cause the CPU to switch from one thread to another thread. The switching process needs to save the state of the current process and restore the state of another.

The cost of context switching is high since swapping threads on the core takes a lot of time. The delay of context switching depends on different factors, probably between 50 and 100 nanoseconds. Considering that the hardware performs an average of 12 instructions per nanosecond on each core, then a context switch may take between 600 and 1200 instructions of latency. In fact, context switching takes up a lot of time for the program to execute instructions.

If there is a Cross-Core Context Switch, it may cause the CPU cache to fail. (the cost of CPU accessing data from the cache is about 3 to 40 clock cycles, and the cost of accessing data from main memory is about 100 to 300 clock cycles ). The switching cost of this scenario will be more expensive.

Go is born for concurrency

Since its official release in 2009, Golang has quickly gained market share due to its extremely high operating speed and efficient development efficiency. Golang supports concurrency from the language level and uses lightweight coroutines to implement concurrent programs.

Goroutine is very lightweight, mainly reflected in the following two aspects:

  • The cost of context switching is low: Goroutine context switching involves only the modification of the value of three registers(PC / SP / DX) while the context switching of the comparison thread needs to include mode switching (switching from user mode to kernel mode) and 16 registers, PC, SP, etc. register refresh
  • Less memory usage: thread stack space is usually 2M, Goroutine stack space is at least 2K;

Go programs can easily support six figures concurrent Goroutine operation, and when the number of threads reaches 1k, the memory consumption has reached 2G.

golang-tutorial golang programing go

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Golang Array Example | Arrays in Go Tutorial Explained

Golang array is a fixed-size collection of items of the same type. The items of an array are stored sequentially and can be accessed using their index.

Secure HTTPS servers in Go

In this article, we are going to look at some of the basic APIs of the http package to create and initialize HTTPS servers in Go

Learning Go — Array, Slice, Map

In this article, we are going to see the four important collection types built into the language. we start with humble Arrays, Maps, Slices

Anatomy of Conditional Statements and Loops in Go

In this article we will explore Anatomy of Conditional Statements and Loops in Go . Go provides if/else and switch conditional statements for code execution based on a certain condition(s) and for code execution in…

Go Tutorial (Golang) - Looping Constructs in Golang | Golang For Loop

In this video we will see the Looping Constructs in Golang. Golang For Loop. Go is an open source programming language which was originally developed by Google. In this Go Tutorial we will Learn Go from the Basics with Code Examples. Go is a statically-typed language.