Revisiting Move Semantics(and all the related idioms)

While I was learning move semantics, I always had a feeling, that even though I knew the concept quite well, I cannot fit it into the big picture of C++. Move semantics is not like some syntactic sugar that solely exists for convenience, it deeply affected the way people think and write C++ and has become one of the most important C++ idioms. But hey, the pond of C++ was already full of other idioms, when you throw move semantics in, mutual extrusion comes with it. Did move semantics break, enhance or replace other idioms? I don't know, but I want to find out.

Value Semantics

Value semantics is what makes me start to think about this problem. Since there aren't many things in C++ with the name "semantics", I naturally thought, "maybe value and move semantics have some connections?". Turns out, it's not just connections, it's the origin:

Move semantics is not an attempt to supplant copy semantics, nor undermine it in any way. Rather this proposal seeks to augment copy semantics.

- Move Semantics Proposal, September 10, 2002

Perhaps you've noticed it uses the wording "copy semantics", in fact, "value semantics" and "copy semantics" are the same thing, and I'll use them interchangeably.

OK, so what is value semantics? isocpp has a whole page talking about it, but basically, value semantics means assignment copies the value, like T b = a;. That's the definition, but often times value semantics just means to create, use, store the object itself, pass, return by value, rather than pointers or references.

The opposite concept is reference semantics, where assignment copies the pointer. In reference semantics, what's important is identity, for example T& b = a; , we have to remember that b is an alias of a, not anything else. But in value semantics, we don't care about identity at all, we only care about the value an object1 holds. This is brought by the nature of copy, because a copy is ensured to give us two independent objects that hold the same value, you can't tell which one is the source, nor does it affect usage.

Unlike other languages(Java, C#, JavaScript), C++ is built on value semantics. By default, assignment does bit-wise-copy(if no user-defined copy ctor is involved), arguments and return values are copy-constructed(yes I know there's RVO). Keeping value semantics is considered a good thing in C++. On the one hand, it's safer, because you don't need to worry about dangling pointers and all the creepy stuff; on the other hand, it's faster, because you have less indirection, see here for the official explanation.

Move Semantics: V8 Engine on the Value Semantics Car

Move semantics is not an attempt to supplant copy semantics. They are totally compatible with each other. I came up with this metaphor which I feel describes their relation really well.

Imagine you have a car, it ran smoothly with the built-in engine. One day, you installed an extra V8 engine onto this car. Whenever you have enough fuel, the V8 engine is able to speed up your car, and that makes you happy.

So, the car is value semantics, and the V8 engine is move semantics. Installing an engine on your car does not require a new car, it's still the same car, just like using move semantics won't make you drop value semantics, because you're still operating on the object itself not its references or pointers. Further more, the move if you can, else copy strategy, implemented by the binding preferences, is exactly like way engine is chosen, that is to use V8 if you can(enough fuel), otherwise fall back to the original engine.

Now we have a pretty good understanding of Howard Hinnant(main author of the move proposal)'s answer on SO:

Move semantics allows us to keep value semantics, but at the same time gain the performance of reference semantics in those cases where the value of the original (copied-from) object is unimportant to program logic.

EDIT: Howard added some comment that really worth mentioning. By definition, move semantics acts more like reference semantics, because the moved-to and moved-from objects are not independent, when modifying(either by move-construction or move-assignment) the moved-to object, the moved-from object is also modified. However, it doesn't really matter——when move semantics takes place, you don't care about the moved-from object, it's either a pure rvalue (so nobody else has a reference to the original), or when the programmer specifically says "I don't care about the value of the original after the copy" (by using std::move instead of copy). Since modification to the original object has no impact on the program, you can use the moved-to object as if it's an independent copy, retaining the appearance of value semantics.

Move Semantics and Performance Optimization

Move semantics is mostly about performance optimization: the ability to move an expensive object from one address in memory to another, while pilfering resources of the source in order to construct the target with minimum expense.

- Move Semantics Proposal

As stated in the proposal, the main benefit people get from move semantics are performance boost. I'll give two examples here.

The optimization you can see

Suppose we have a handler(whatever that is) which is expensive to construct, and we want to store it into a map for future use.

std::unordered_map<string, Handler> handlers;
void RegisterHandler(const string& name, Handler handler) {
  handlers[name] = std::move(handler);
}
RegisterHandler("handler-A", build_handler());

This is a typical use of move, and of course it assumes Handler has a move ctor. By moving(not copying)-constructing a map value, a lot of time may be saved.

The optimization you can't see

Howard Hinnant once mentioned in his talk that, the idea of move semantics came from optimizing std::vector. How?

A std::vector<T> object is basically a set of pointers to an internal data buffer on heap, like begin() and end(). Copying a vector is expensive due to allocating new memory for the data buffer. When move is used instead of copy, only the pointers get copied and point to the old buffer.

What's more, move also boosts vector insert operation. This is explained in the vector Example section in the proposal. Say we have a std::vector<string> with two elements "AAAAA" and "BBBBB", now we want to insert "CCCCC" at index 1. Assuming the vector has enough capacity, the following graph demonstrates the process of inserting with copy vs move.

Everything shown on the graph is on heap, including the vector's data buffer and each element string's data buffer. With copy, str_b's data buffer has to be copied, which involves a buffer allocation then deallocation. With move, old str_b's data buffer is reused by the new str_b in the new address, no buffer allocation or deallocation is needed(As Howard pointed out, the "data" that old str_b now points to is unspecified). This brings a huge performance boost, yet it means more than that, because now you can store expensive objects into a vector without sacrificing performance, while previously having to store pointers. This also helps extend usage of value semantics.

Move Semantics and Resource Management

In the famous article Rule of Zero, the author wrote:

Using value semantics is essential for RAII, because references don’t affect the lifetime of their referrents.

I found it to be a good starting point to discuss the correlation between move semantics and resource management.

As you may or may not know, RAII has another name called Scope-Bound Resource Management (SBRM), after the basic use case where the lifetime of an RAII object ends due to scope exit. Remember one advantage of using value semantics? Safety. We know exactly when an object's lifetime starts and ends, just by looking at its storage duration, and 99% of the time we'll find it at block scope, which makes it very simple. Things get a lot more complicated for pointers and references, now we have to worry about whether the object that is referenced or pointed to has been released. This is hard, what makes it worse is that these objects usually exist in different scope from its pointers and references.

It's obvious why value semantics gets along well with RAII —— RAII binds the life cycle of a resource to the lifetime of an object, and with value semantics, you have a clear idea of an object's lifetime.

But, resource is about identity…

Though value semantics and RAII seems to be a perfect match, in reality it was not. Why? Fundamentally speaking, because resource is about identity, while value semantics only cares about value. You have an open socket, you use the very socket; you have an open file, you use the very file. In the context of resource management, there aren't things with the same value. A resource represents itself, with unique identity.

See the contradiction here? Prior to C++11, if we stick with value semantics, it was hard to work with resources cause they cannot be copied, therefore programmers came up with some workarounds:

  • Use raw pointers;
  • Write their own movable-but-not-copyable class(often Involves private copy ctor and operations like swap and splice);
  • Use auto_ptr.

These solutions intended to solve the problem of unique ownership and ownership transferring, but they all have some drawbacks. I won't talk about it here cause it's everywhere on the Internet. What I would like to address is that, even without move semantics, resource ownership management can be done, it's just that it takes more code and is often error-prone.

What is lacking is uniform syntax and semantics to enable generic code to move arbitrary objects (just as generic code today can copy arbitrary objects).

- Move Semantics Proposal

Compared to the above statement from proposal, I like this answer more:

In addition to the obvious efficiency benefit, this also affords a programmer a standards-compliant way to have objects that are movable but not copyable. Objects that are movable and not copyable convey a very clear boundary of resource ownership via standard language semantics …my point is that move semantics is now a standard way to concisely express (among other things) movable-but-not-copyable objects.

The above quote has done a pretty good job explaining what move semantics means to resource ownership management in C++. Resource should naturally be movable(by "movable" I mean transferrable) but not copyable, now with the help of move semantics(well actually a whole lot of change at language level to support it), there's a standard way to do this right and efficiently.

The Rebirth of Value Semantics

Finally, we are able to talk about the other aspect(besides performance) of augmentation that move semantics brought to value semantics.

Stepping through the above discussion, we've seen why value semantics fits the RAII model, but at the same time not compatible with resource management. With the arise of move semantics, the necessary materials to fill this gap is finally prepared. So here we have, smart pointers!

Needless to say the importance of std::unique_ptr and std::shared_ptr, here I'd like to emphasize three things:

  • They follow RAII;
  • They take huge advantage of move semantics(especially for unique_ptr);
  • They help keep value semantics.

For the third point, if you've read Rule of Zero, you know what I'm talking about. No need to use raw pointers to manage resources, EVER, just use unique_ptr directly or store as member variable, and you're done. When transferring resource ownership, the implicitly constructed move ctor is able to do the job well. Better yet, the current specification ensures that, a named value in the return statement in the worst case(i.e. without elisions) is treated as an rvalue. It means, returning by value should be the default choice for unique_ptr.

std::unique_ptr<ExpensiveResource> foo() {
  auto data = std::make_unique<ExpensiveResource>();
  return data;
}
std::unique_ptr<ExpensiveResource> p = foo();  // a move at worst

See here for a more detailed explanation. In fact, when using unique_ptr as function parameters, passing by value is still the best choice. I'll probably write an article about it, if time is available.

Besides smart pointers, std::string and std::vector are also RAII wrappers, and the resource they manage is heap memory. For these classes, return by value is still preferred. I'm not too sure about other things like std::thread or std::lock_guard cause I haven't got chance to use them.

To summarize, by utilizing smart pointers, value semantics now truly gains compatibility with RAII. At its core, this is powered by move semantics.

Summary

So far we've gone through a lot of concepts and you probably feel overwhelmed, but the points I want to convey are simple:

  1. Move semantics boosts performance while keeping value semantics;
  2. Move semantics helps bring every piece of resource management together to become what it is today. In particular, it is the key that makes value semantics and RAII truly work together, as it should have been long ago.

I'm a learner on this topic myself, so feel free to point out anything that you feel is wrong, I really appreciate it.

[1]: Here object means "a piece of memory that has an address, a type, and is capable of storing values", from Andrzej's C++ blog.

pdir2 现在支持 attribute filtering 了

最近应用户要求pdir2 添加了一个功能,在 0.3.0 中发布。

现在你可以更精确地获取所需属性了:
properties: Find properties/variables defined in the inspected object.

methods: Find methods/functions defined in the inspected object.

public: Find public attributes.

own: Find attributes that are not inherited from parent classes.

比如这个例子(完整文档参考 wiki

class Base(object):
    base_class_variable = 1

    def __init__(self):
        self.base_instance_variable = 2

    def base_method(self):
        pass


class DerivedClass(Base):
    derived_class_variable = 1

    def __init__(self):
        self.derived_instance_variable = 2
        super(DerivedClass, self).__init__()

    def derived_method(self):
        pass


inst = DerivedClass()

pdir(inst).properties  # 'base_class_variable', 'base_instance_variable',
                       # 'derived_class_variable', 'derived_instance_variable', '__class__',
                       # '__dict__', '__doc__', '__module__', '__weakref__'

pdir(inst).methods  # '__subclasshook__', '__delattr__', '__dir__', '__getattribute__',
                    # '__setattr__', '__init_subclass__', 'base_method',
                    # 'derived_method', '__format__', '__hash__', '__init__', '__new__',
                    # '__repr__', '__sizeof__', '__str__', '__reduce__', '__reduce_ex__',
                    # '__eq__', '__ge__', '__gt__', '__le__', '__lt__', '__ne__'

pdir(inst).public  # 'base_method', 'derived_method', 'base_class_variable',
                   # 'base_instance_variable', 'derived_class_variable',
                   # 'derived_instance_variable'

pdir(inst).own  # 'derived_method', '__init__', 'base_instance_variable',
                # 'derived_class_variable', 'derived_instance_variable', '__doc__',
                # '__module__'

pdir(inst).public.own.properties  # 'base_instance_variable','derived_class_variable',
                                  # 'derived_instance_variable'
pdir(inst).own.properties.public  # ditto
pdir(inst).properties.public.own  # ditto

pdir(inst).public.own.properties.search('derived_inst')  # 'derived_instance_variable'

支持 Method Chaining,支持和 search() 方法一起使用。大概就这样。

令人迷惑的 context

很长一段时间,我都搞不懂 Go 的 context package。最近我读了两篇文章:

终于理解了 context 是在干什么,以及为何它让人费解。

从设计的目的上说,context 是用来控制 goroutine 的,确切地说,是为了当你需要的时候,能干掉一个 goroutine。比如你用 go someFunc() 创建了成千上万个 goroutine,一切都很美好。突然你不想让它们继续执行了,这时你发现,糟了,你根本不知道这些 goroutine 放在哪,更别说控制它们了。不像其它语言用多线程时往往有个 Thread object,goroutine 并没有保存在某个变量里。怎么办?

一. 如何让 goroutine 从内部退出?

办法是每次创建 goroutine,都传进去一个 context,把函数定义成这样

func someFunc(ctx context.Context) {
  select {
  case <-ctx.Done():
      return
  }
}

这个模板包含了实现 goroutine 控制的必要元素。首先有一个 context.Context 类型的参数 ctx,它有一个最重要的方法 Done()case <-ctx.Done(): 本质上就是在做这样一个判断 if ctx.goroutine_should_be_cancelled(),如果是,那它就进入这个 case 并且 return,结束当前 goroutine 的执行,否则进别的 case 继续执行。Done() 的实现机理在这里并不重要,我们只需要知道它在做判断即可。( 其原理是返回一个 read-only 的 channel,这个 channel 在 ctx被 cancel 的时候关闭,而读被关闭的 channel 会直接返回一个 zero value 而不会阻塞。)

这里用两个例子展示 case <-ctx.Done(): 和别的控制逻辑(比如循环,别的 channel)是怎么结合使用的,比较简单所以就不解释了。

Example 1

func someFunc(ctx context.Context) {
    for {
        innerFunc()
        select {
            case <-ctx.Done():
                // ctx is canceled
                return
            default:
                // ctx is not canceled, continue immediately
        }
    }
}

Example 2

func httpDo(ctx context.Context, req *http.Request, f func(*http.Response, error) error) error {
    // Run the HTTP request in a goroutine and pass the response to f.
    tr := &http.Transport{}
    client := &http.Client{Transport: tr}
    c := make(chan error, 1)
    go func() { c <- f(client.Do(req)) }()
    select {
    case <-ctx.Done():
        tr.CancelRequest(req)
        <-c // Wait for f to return.
        return ctx.Err()
    case err := <-c:
        return err
    }
}

理论上说,不传 context 而是传 channel 来控制也做得到,但为每个 goroutine 都创建一个 channel 并不符合 channel 的用法。

二. 上层如何告诉 goroutine 该退出了?

现在我们已经知道 <-ctx.Done() 用来在 goroutine 内部用来判断当前 goroutine 是否应该退出。那么外界该如何控制?有两种方式,要么是手动 cancel,要么是设定条件自动 cancel。

先看手动 cancel:

ctx, cancel = context.WithCancel(context.Background())

WithCancel 返回一个被包装过的 context,以及一个 cancel 函数。只要把 ctx 传进 goroutine,在合适的时候调用 cancel() 就可以告诉 ctx,这个 goroutine 应该退出了,从而进入 case <-ctx.Done():

自动 cancel 可以设置两种条件:1. 设置一个 deadline,时间达到或超过 deadline 则 cancel;2. 设置一个 timeout,超时则 cancel。函数是 WithDeadlineWithTimeout,用法和 WithCancel 一样。

func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)

三. Context For Pythonista

自然我们要看一下其它语言对与并发是如何控制的。比如,在 Python 里人们怎么让一个线程停止的?

Is there any way to kill a Thread in Python?

首先必须要说明,一般情况下不应该去试图干掉一个线程。当你需要这么做时,最简单的方法是在线程类里加入一个 threading.Event object 成员变量,之后在线程里去定期检查 event.is_set()。甚至于你都可以直接用一个 Boolean。对于 Java 也是类似的方法(当然实际上 Java 有更暴力的 Thread.interrupt(),不过我们假定用户想自己控制线程的退出)。

观察一下会发现,不管是 Python 里继承了 threading.Thread 的类,还是 Java 里实现了 Runnable 接口的类,它们都是个类,从而你可以在其实例中添加任何需要的变量。而且,由于每个线程都是一个实例,你可以去调用上面定义的方法,比如 terminate(),来设置之前提到的 Boolean 的值。

Go 就不行了,由于没有“goroutine object”,必须要以参数的形式把一个实现控制功能的载体也就是 context 传进去。再仔细观察一下,case <-ctx.Done(): 实现了 wait,和 Python 的 threading.Event.wait() 功能完全相同——毕竟这种等待的机制非常有用。

四. 为什么 context 看起来很奇怪?

context 奇怪的根源,在于它混合了两种不同的目的,更确切地说,是因为加入了 WithValueWithValue 非常好理解,就是外界在 context 上 set 一个 (key, value) pair,然后在 goroutine 内部读取。比如

ctx := context.WithValue(context.Background(), "key", "value")
go func(ctx) {
   value := ctx.Value("key")
}()

KV 存储嘛,谁都知道在干嘛。但是我们不要忘了 context 的主要用途是控制 goroutine 的退出。于是就很奇怪了,明明是控制 goroutine 退出的 indicator,为什么又能当成 KV 存储来用呢?这个问题困扰了许多人,以至于 Dave Cheney 专门写文章吐槽:《Context isn’t for cancellation》,他的观点是,context 顾名思义应该就是做存储用的,用来 cancel 才奇怪,所以应当:

I propose context.Context becomes just that; a request scoped association list of copy on write values.

那篇文章还有个有意思的点,就是当前的 context 其实并不能做到完美的控制,不过这里暂且不提。总之,这个混合了两种目的的设计,不论是谁都觉得奇怪。我猜想,设计者的初衷应该是不想让人们传两个变量,索性都塞进 context。

WithValue 相当于 Python 里的 threading.local,但显然,没人会把 threading.localthreading.Event 合成一个东西来用。


top