This article explains the rules for executing defer and introduces the defer type. It explains how defer function calls are done, mainly through heap allocation. Introduction defer execution rules The order of execution of multiple defers is “Last In First Out LIFO " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 package main import ( "fmt" ) func main() { name := "Naveen" fmt.Printf("Original
Go language implementation of Snowflake algorithm
Introduction Sometimes in business, it is necessary to use some unique ID to record the identification of one of our data. The most common ones are none other than the following: UUID, database self-incrementing primary key, Redis’ Incr command, and other methods to get a unique value. Below we talk about the advantages and disadvantages of each of them in order to introduce our distributed snowflake algorithm. UUID First is
How to build a high-performance Go caching library
I was looking at some excellent open source libraries when I saw an interesting cache library fastcache, in its introduction mainly has the following characteristics: read and write data to be fast, even under concurrency. maintain good performance even in a cache of several gigabytes, as well as minimize the number of GCs. the design should be as simple as possible. This article will examine how its kernel achieves such
A deep dive into each of the high-performance JSON parsing libraries in Go
In fact, I had no intention of looking at the performance of JSON libraries, but I recently did a pprof on my project and found from the flame chart below that more than half of the performance consumption in business logic processing is in the JSON parsing process, so this article came about. This article dives into the source code to analyze how the standard library in Go parses JSON,
Design and implementation of zap, a high-performance Go logging library
Recently I’ve been learning how to make code run more efficiently in development, and while browsing through various good logging designs, I came across a logging library called zap from uber that caught my attention, with its main feature of extreme performance and memory allocation optimizations. For me, I was originally using logrus as log output in my project, but when I saw the benchmark of zap, I felt that
5 CSS mistakes to avoid
As we know today, the CSS language is an essential part of the web. It gives us the ability to draw the way elements are presented on a screen, web page or other media. It is simple, powerful, and declarative. We can easily implement complex things like dark/light patterns. However, there are many misconceptions and misuses of it. These can turn CSS markup into complex unreadable and unscalable code. How
cURL will natively support JSON
cURL author Daniel Stenberg emailed revealed about plans to add native support for JSON to cURL.
Daniel explains the reasons for adding native JSON support to cURL:
sending JSON is a common practice in areas such as REST APIs “Ease of use with JSON” was a common consideration when many people were asked about choosing a cURL alternative on sites like Stack Overflow, a significant number of people were unable to send the correct JSON and quote JSON correctly using cURL because JSON uses double quotes and the Shell does not expand variables inside single quotes Daniel also said that since he doesn’t send JSON very often, it wasn’t appropriate to rely entirely on him to design this feature.
Analysis of the Go language HTTP standard library
This article looks at how the Go language HTTP standard library is implemented. The HTTP-based service standard model consists of two ends, a client (‘Client’) and a server (‘Server’). HTTP requests are sent from the client, and the server receives the request, processes it and returns the response to the client. So the job of the HTTP server is to accept requests from the client and return a response to
Fasthttp: a Go framework ten times faster than net/http (server part)
I found a Go framework that claims to be ten times faster than net/http - Fasthttp. Let’s take a look at what great design it has to offer and let’s dig in. A typical HTTP service should look like this: The standard model of a service built on HTTP consists of two ends, a client (‘Client’) and a server (‘Server’). HTTP requests are sent from the client, the server receives
How does endless achieve a non-stop restart of Go programs?
For a non-stop restart of a Go program we need to address two issues. the process restart does not need to close the listening port. pre-existing requests should be fully processed or timeout. We’ll see how endless does both of these things later. Basic concepts The following is a brief introduction to two knowledge points for the content that follows. Signal handling Go signal notification works by sending the os.Signal
Using Failpoint to inject faults in Go
When I was looking at the TiDB source code recently, I found it very interesting that failpoint is used for fault injection. It uses code generation and code AST tree parsing and replacement to implement failpoint, I will also try to parse it to learn how to parse the AST tree to generate code. I will also try to parse it and learn how to parse the AST tree to
Exploring Go-YCSB for database benchmarking
Recently we have been doing a technical selection of databases and inevitably we need to do a benchmark test of the databases so that we can compare the performance of different databases across the board. Cloud Serving Benchmark (YCSB) is a tool developed by Yahoo for basic testing of cloud services. Redis, etc. As a go developer, we used the Go YCSB developed by pingcap for benchmarking purposes. Installation First
Exploring what are the pitfalls of panic & recover in Go source code?
Preface The reason for writing this article is that a colleague at work recently wrote a Goroutine directly using the Go keyword, and then had a null pointer problem that caused the whole program to go down because there was no recover. The code looks like this. 1 2 3 4 5 6 7 8 9 10 11 12 13 func main() { defer func() { if err := recover();
Golang Simple Architecture in Action
Since golang does not have a uniform coding pattern like java, we, like the rest of the team, used some of the theory presented in the article Go Package Oriented Design and Architecture Layering and then combined it with our previous project experience to define the packge. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ├── cmd/
Evan You announces: Vue 3 will be the new default version
In his announcement, he mentioned that developers should be concerned about the measures that may need to be taken to confirm whether they need to make appropriate changes to avoid anomalies before the default version switch.
The following is reproduced from the original announcement by Evan You.
Make sure to read the Potential Required Actions section to see if you need to make certain changes before the switch to avoid breakage.
C++ std::thread join() and deconstruct function
During a debugging session a few days ago, I noticed that the Stack Trace printed by the program was not quite what I expected. After much research, I found a problematic piece of code. Can you see what the problem is?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include <chrono> #include <iostream> #include <thread> int subtask1(int x) { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); return x; } int subtask2(int x) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); return x; } int run(int a, int b) { int result1; std::thread t([&]() { result1 = subtask1(a); }); int result2 = subtask2(b); if (result2 < 0) { return -2; } t.
C++ threading: how to use promise, future, packaged_task and async
Today I would like to introduce the C++ threaded high-level APIs: std::promise, std::future, std::packaged_task and std::async. The content of this article can be condensed into the following diagram.
where std::promise and std::future are synchronisation channels between threads. The std::packed_task class template is an adapter for a function or a function object. It wraps the return value of a function in std::future, allowing us to easily execute any function with std::thread. The std::async function is equivalent to the sum of std::packaged_task and std::thread.
Usage of std::any in C++ 17
This article introduces the new std::any class in C++ 17. It can store all values that are “Copy Constructible”. In the following we will first introduce the basic usage of std::any, and then describe the practical use of it.
Usage The use of the std::any class is broken down as follows.
Constructed objects Assigning operators any_cast function has_value member function reset member function emplace member function type member function Constructed objects The std::any category is defined in the <any> header file.
Using Perf for program hotspot analysis
Perf is a performance analysis tool that has been added to the kernel since Linux 2.6.31 (2009). It uses kernel runtime staking to detect the running of programs and can provide quite rich information, enough to find performance bottlenecks and optimisation priorities in the running of programs. Installation On some systems there may be a full Perf pre-installed, but on the Debian Buster (10) I am using, only the user
Implementation of the Linux kernel CFS scheduler
This article provides an easy and quick introduction to the principles and implementation of the CFS scheduler, a fully fair class scheduling class in the Linux kernel.
Principle The core principle of the CFS (Completely Fair Scheduler) is simple: each process is given as “fair” a runtime as possible. So the process that has run the least in the past is chosen to run each time. Of course, as a scheduler, it has to meet much more than that, and a number of features such as Linux’s preemptive processes and support for priorities in the Completely Fair class fair_sched_class make the design of CFS much more complex than simply picking the least amount of runtime.