Pass By Rvalue Reference Or Pass By Value

Passing by rvalue reference is typically an optimization for the case when data can be “stolen” from the parameter instead of copying from it.

struct S
    void init(const SomeType& param); // copy from param
    void init(SomeType&& param);      // steal from param

People noticed that if SomeType is cheaply movable, then we don’t have to write two overloads and can get away with a single pass-by-value:

struct S
    void initByVal(SomeType param); // can now steal from param

SomeType t;
s.initByVal(std::move(t)); // move data to param, then can steal from it

Compared to the first version, this will result in one more invocation of SomeType’s move constructor to construct param, but as it is cheap to move it does not matter (much).

It is often mentioned that the first approach would result in 2^N overloads in case of N arguments, and passing by value magically solves this issue. However, passing by reference solves it just as well (see below). Also, passing by value can easily lead to unspecified behavior and, in my opinion, this alone makes it worth considering alternative approaches.

Read more →

Limiting File Download Speed In C#

Apparently, limiting file download speed it is not straightforward with the standard web helper classes provied by .NET - WebClient or HttpWebResponse. Someone even re-implemented a socket-based HTTP client to add this feature. While the latter might be a solution, relying on an incomplete and potentially buggy HTTP implementation is not something I like. Libcurl, on the other hand, has been around for ages and also has an option to limit the download speed. The library is ported to many OSes including Windows and wrapped for .NET as well, so I gave it a try.

Read more →

Rvo And Copy Elision Failing

Following this interesting StackOverflow question about RVO failure, I’ve experimented a little to see in which cases copies are elided by the compiler. The question is about RVO failing in case when NRVO is performed properly; it is hard to explain why this happens without knowing the internals of the MSVC compiler, but here are a few observations on when this happens.

Read more →

Objects As Data In D3

D3 uses a very powerful data binding approach. However, many D3 examples only show how to deal with very simple data, such as [1,2,3]. Let’s have a look at what happens when data items are objects, for example

var data = [
  {x: 70, y: 20, label: "first"},
  {x: 10, y: 60, label: "second"}
Read more →

Github Pages 302 Redirect

My blog is on a custom domain with GitHub Pages. As it turns out, this configuration can cause some issues, for example, when the site is indexed by a search engine.

A few people have already posted about this, and here an update on the top of the post says: “This is no longer true since at least August 2014. GitHub fixed this!”. This note might refer to the bigger issue about the 5 second delay described in that post, although it made me think maybe GitHub does not do redirects anymore. It still does sometimes:

$ curl -I
HTTP/1.1 302 Found
Connection: close
Pragma: no-cache
cache-control: no-cache
Location: /

Note: this does not happen every time, but after a certain timeout the first request oftens gets redirected.

Read more →