Acquire/Release Operation 和 Acquire/Release Fence

其实是 Jeff Preshing 的 Acquire and Release Fences Don’t Work the Way You’d Expect 这篇文章的笔记。

Read More

解决 CMake 依赖工程中同名 cmake 函数调用混乱

拿 learn-asio 这个项目练手时发现一个问题:learn-asio 依赖了 KBase,这两个项目都是我用 anvil 进行托管的,所以两个项目的 cmake 目录里各自有一份 compiler_*.cmake

因为 compiler_*.cmake 提供的函数 apply_common_compiler_property_to_target() 默认使用了非常严格的属性,导致构建 asio 练手工程出现很多 warning 和静态分析的错误,所以为了省事我把 learn-asio 的这个函数做了宽松化处理。

Read More

安利 asio-cmake

0x00 Background

考虑到 ASIO 的相当一部分组件(subset)已经进入了 C++ 20 Networking TS,未来再经过一些小调整之后成为标准库的一员基本板上钉钉。

那么提前熟悉一下 ASIO 也不是什么坏事,毕竟没准等到 C++ 23 的时候,coroutine, networking, executor 啥的都已经很完备了,谁都可以用 C++ 实现一些性能不差的 server-end 代码了(比如 RPC 框架?)。

不过和使用 Boost 类似,使用 ASIO 作为依赖库也不是一件容易的事情;毕竟自 modern C++ 元年至今都8年了,CMake 还在成为 de facto building system 的道路上挣扎,广泛接受的包管理依然不见踪影…

0x01 ASIO-CMake Wrapper

陈年老账就不再扯了,得益于 CMake 3.2 开始提倡的 taget-based properties 和 CMake 3.11 开始提供的 FetchContent module,我花了一些时间实现了一个基于 FetchContent 的 asio-cmake wrapper。

Read More

Monthly Read Posts in July 2019

Programming Languages

Go: Understand the Design of Sync.Pool

Read More

学习 MySQL 的一些吐槽

最近学习 MySQL 的过程中发现一些很坑的点,总结记录如下:

  1. RDBMS 虽然理论很早就有了,但是近些年的实际应用进化已经导致各家独立。不同引擎实现不同,甚至连 spec 都不同。
  2. 不考虑 Oracl, SQL Server 这种传统行业使用的,PostgreSQL 和 MySQL 之间差异性也不小,体现在 transaction, locking 上。甚至 MariaDB 和 MySQL 未来的差异可能也会变大。

Read More

Monthly Read Posts in June 2019

Concurrency

Memory Barriers Are Like Source Control Operations

Read More

实现 compressed pair

C++ 的 std::unique_ptr 有个神奇的特性:如果使用默认的 deleter(即使用 operator delete),或者 non-capturing lambda 作为 deleter,则有

1
sizeof(std::unique<T>) == sizeof(void*);

即整个对象的内存布局和 trivial pointer 一致,没有额外的开销。

这个特性的背后就是 compress-pair;这个设施能够在某个元素是一个 empty class 时避免为其分配内存。

注:这里假设你知道什么是 EBO,以及为什么会有 EBO。

这里自己动手实现一个 compressed pair:

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
41
42
43
44
45
46
47
48
template<typename Tx, typename Ty, bool = std::is_empty<Tx>::value>
struct CompressedPair : Tx {
Ty second;

template<typename T>
explicit CompressedPair(T&& t)
: Tx(),
second(std::forward<T>(t))
{}

template<typename T, typename U>
CompressedPair(T&& t, U&& u)
: Tx(std::forward<T>(t)),
second(std::forward<U>(u))
{}

Tx& get_first() noexcept
{
return *this;
}

const Tx& get_first() const noexcept
{
return *this;
}
};

template<typename Tx, typename Ty>
struct CompressedPair<Tx, Ty, false> {
Tx first;
Ty second;

template<typename T, typename U>
CompressedPair(T&& t, U&& u)
: first(std::forward<T>(t)),
second(std::forward<U>(u))
{}

Tx& get_first() noexcept
{
return first;
}

const Tx& get_first() const noexcept
{
return first;
}
};

因为 EBO 是实现的核心,而父类的构造顺序先于子类的任何成员,上面将 Tx 作为可被优化的成员。

Read More