Differential Code Coverage for Debugging: A Powerful Technique

2025-04-25

This article introduces a powerful debugging technique: differential code coverage analysis. By comparing the code coverage of passing and failing tests, you can quickly pinpoint buggy code. The author uses Go's `math/big` library as an example, demonstrating how to use `go test` and `go tool cover` to generate coverage reports and `diff` to compare the differences. This efficiently identifies the code snippet causing the test failure, significantly reducing debugging time compared to traditional methods. The technique is illustrated by finding a bug in a few lines of code out of over 15,000.

Read more
Development code coverage

C/C++: Performance Over Correctness?

2025-03-31

This article delves into the pitfalls of "undefined behavior" in C and C++. In the pursuit of ultimate performance, compilers often take a laissez-faire approach to uninitialized variables, arithmetic overflow, infinite loops, and null pointers, rather than reporting errors or inserting safety checks. This makes programs difficult to debug and maintain, potentially leading to unpredictable crashes. The author uses several examples to illustrate how C/C++ compilers prioritize optimization, even at the cost of program correctness and predictability, prompting reflection on this design philosophy.

Read more
Development C Language

Go Interfaces: Static Compile-Time Checking, Dynamic Run-Time Dispatch

2025-02-09

Go's interfaces, a unique blend of static type checking and dynamic dispatch, are arguably its most exciting feature. This post delves into the implementation details of interface values within Go's gc compilers, covering their memory representation, itable (interface table) generation and caching, and memory optimizations for various data sizes. Through code examples and illustrations, the author clearly explains how Go achieves compile-time type safety and efficient run-time interface calls. Comparisons with other languages' interface implementations highlight Go's distinctive approach.

Read more

Go Data Structures: A Deep Dive into Memory Layout

2025-02-05

This post provides a detailed explanation of the memory layout of basic data types, structs, arrays, and slices in Go. Using illustrative diagrams, it clearly shows how various data types are represented in memory, including ints, floats, arrays, structs, and pointers. The article also specifically explains the underlying implementation of strings and slices in Go, as well as the differences between the `new` and `make` functions. This helps readers better understand the mechanisms behind Go's efficiency and gain a deeper understanding of Go's memory management.

Read more
Development

Programming Language Memory Models: Challenges and Solutions in Concurrent Programming

2024-12-12

This article delves into programming language memory models, specifically the behavior of shared memory in multithreaded programs. Using a simple C-like program as an example, it illustrates how compiler optimizations can lead to unexpected results, such as race conditions between threads. To address this, modern languages introduce atomic variables and atomic operations to ensure thread synchronization and avoid data races. The article compares the memory models of Java, C++, Rust, and other languages, analyzing their strengths and weaknesses and evolution, and points out the remaining challenges in formally defining memory models.

Read more