Skip to contentSkip to conversation

Review Request

DISCUSSION Adam C. Emerson 3 participants 5 messages Sep 16, 2015 – Sep 17, 2015

lore
Share & tools
lore
#0 lore raw

Creators of the Storage Squid,

If you're interested in less use of the allocator, you are interested in Context* elimination. If so, please review the top two commits on the wip-decontextualization branch of:

https://github.com/cohortfsllc/cohortfs-ceph

They provide an enhanced version of std::function that allows you to specify how much space gets preallocated for function objects or disable allocation entirely.

Also, I have two pull requests, wip-cxx11time and wip-cxx11concurrency that pass check on which I would appreciate any feedback.

Thank you, Comrades!

#1 lore raw
Quoted 11 lines · Adam C. Emerson

Creators of the Storage Squid,

If you're interested in less use of the allocator, you are interested in Context* elimination. If so, please review the top two commits on the wip-decontextualization branch of:

https://github.com/cohortfsllc/cohortfs-ceph

They provide an enhanced version of std::function that allows you to specify how much space gets preallocated for function objects or disable allocation entirely.

Can you provide a little background (links or text) for those of us who aren't up on C++11x/14x? I looked at them briefly but having only the vaguest idea about some of them quickly got lost. :) -Greg

Quoted 9 lines

Also, I have two pull requests, wip-cxx11time and wip-cxx11concurrency that pass check on which I would appreciate any feedback.

Thank you, Comrades!
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 3 lines · Gregory Farnum

Can you provide a little background (links or text) for those of us who aren't up on C++11x/14x? I looked at them briefly but having only the vaguest idea about some of them quickly got lost. :)

Surely, sir.

So, in summary, Context* is the means we have been using to handle callbacks. It has two big problems:

(1) Every Context is allocated at the point of use and freed when it's called. So by their nature they make heavy use of the heap. (If you overload complete there's a few exceptions, but by and large Context is very heapy.)

(2) Context accepts an int. That's it. You can get around this by storing other things in it and passing pointers to it and so forth. But it would be nice to have more variety of type in our callbacks.

C++11 (and following) have a whole pile of things that can be used as functions. There are function objects (objects with an operator() defined on them), lambdas with variable capture, function member pointers, reference wrappers pointing to function objects and the like. This gives you a good bit of flexibility, allows things like variable capture, let's you allocate one object once and just pass references to it, and so forth.

Now, these objects all have different sizes and different types. So you can't just shove them in an object naively. Because a class one of whose members is a function pointer is going to be a different type than a class holding a function object. (Which itself is different to a type holding a reference to a function object.)

std::function exists as a solution to this problem. It provides a uniform type that can hold any object satisfying the requirement of being callable with given argument types and a given return value. So, for example, if you have some 'stat'-like call, you could specify it as taking a function taking an error code, a size, and a date. Anything that can be called with such would be accepted, anything that can't (wrong argument types, say) would be rejected at compile time. And it could be uniformly stored in a list of Operations.

The downside is that if the thing you provide is too big, std::function will allocate. 'too big' depends on your library vendor and there's no way to find it out what. Thus the purpose of the ceph::function class. If we have a pretty good idea how large most of the callback function objects we expect to hold are, we can tell it to statically allocate that much space. This gives us a tool to get allocations out of our fast path. (For example, if we preallocate a bunch of classes with a ceph::function that preallocates enough space to hold likely callbacks, we can just pick them off and have no allocations, in the usual case.)

If we know /everything/ we'll ever get, we can disable allocations entirely. This is mostly so we can catch situations where we're trying to shove something unexpectedly huge somewhere it ought not go. An internal sanity check.

Now, ceph::function, like std::function, is still an abstraction with a virtual call in it and because it copies things around it reduces opportunities for inlining. Thus, if you aren't storing a callback on an object that's supposed to be in a queue, you shouldn't use it. You should do something like:

template<typename Fun>
void do_stuff(Fun&& f) {
...;
f(some, values);
};

This allows inlining, and (based on my experiments trying multiple implementations and looking at the generated assembly) if f is called in a loop, the code is just as good as if you'd open-coded the loop and written the body of 'f' in it. Functions like this can have the potential to use closures (lambda expression) effectively for free.

So, the summary is:

Context* is very heapy. We have less heapy alternatives. This implements a foundation for one of them. We also get a bit more flexibility.

Thank you.

#3 lore raw

This is neat, I had been hankering to lambda-ize various things, but hadn't worked through what the allocation would looked like in practice.

Do you know if there's a reason the standard is defined so as to not let us override the reserved size of a std::function? Are we taking any kind of hit by doing this?

John

P.S. I have just gone on Amazon to order a C++11 book because I need to start using more than just lambda, auto, and pretty for loops ;-)

Quoted 85 lines · Adam C. Emerson
Quoted 3 lines · Gregory Farnum

Can you provide a little background (links or text) for those of us who aren't up on C++11x/14x? I looked at them briefly but having only the vaguest idea about some of them quickly got lost. :)

Surely, sir.

So, in summary, Context* is the means we have been using to handle callbacks. It has two big problems:

(1) Every Context is allocated at the point of use and freed when it's called. So by their nature they make heavy use of the heap. (If you overload complete there's a few exceptions, but by and large Context is very heapy.)

(2) Context accepts an int. That's it. You can get around this by storing other things in it and passing pointers to it and so forth. But it would be nice to have more variety of type in our callbacks.

C++11 (and following) have a whole pile of things that can be used as functions. There are function objects (objects with an operator() defined on them), lambdas with variable capture, function member pointers, reference wrappers pointing to function objects and the like. This gives you a good bit of flexibility, allows things like variable capture, let's you allocate one object once and just pass references to it, and so forth.

Now, these objects all have different sizes and different types. So you can't just shove them in an object naively. Because a class one of whose members is a function pointer is going to be a different type than a class holding a function object. (Which itself is different to a type holding a reference to a function object.)

std::function exists as a solution to this problem. It provides a uniform type that can hold any object satisfying the requirement of being callable with given argument types and a given return value. So, for example, if you have some 'stat'-like call, you could specify it as taking a function taking an error code, a size, and a date. Anything that can be called with such would be accepted, anything that can't (wrong argument types, say) would be rejected at compile time. And it could be uniformly stored in a list of Operations.

The downside is that if the thing you provide is too big, std::function will allocate. 'too big' depends on your library vendor and there's no way to find it out what. Thus the purpose of the ceph::function class. If we have a pretty good idea how large most of the callback function objects we expect to hold are, we can tell it to statically allocate that much space. This gives us a tool to get allocations out of our fast path. (For example, if we preallocate a bunch of classes with a ceph::function that preallocates enough space to hold likely callbacks, we can just pick them off and have no allocations, in the usual case.)

If we know /everything/ we'll ever get, we can disable allocations entirely. This is mostly so we can catch situations where we're trying to shove something unexpectedly huge somewhere it ought not go. An internal sanity check.

Now, ceph::function, like std::function, is still an abstraction with a virtual call in it and because it copies things around it reduces opportunities for inlining. Thus, if you aren't storing a callback on an object that's supposed to be in a queue, you shouldn't use it. You should do something like:

template<typename Fun>
void do_stuff(Fun&& f) {
...;
f(some, values);
};

This allows inlining, and (based on my experiments trying multiple implementations and looking at the generated assembly) if f is called in a loop, the code is just as good as if you'd open-coded the loop and written the body of 'f' in it. Functions like this can have the potential to use closures (lambda expression) effectively for free.

So, the summary is:

Context* is very heapy. We have less heapy alternatives. This implements a foundation for one of them. We also get a bit more flexibility.

Thank you.
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
Quoted 7 lines · John Spray

This is neat, I had been hankering to lambda-ize various things, but hadn't worked through what the allocation would looked like in practice.

Do you know if there's a reason the standard is defined so as to not let us override the reserved size of a std::function? Are we taking any kind of hit by doing this?

There are tradeoffs, but I do not believe they burden us.

Small Function Optimization is not actually required by the standard. It's just that all implementations of std::function I'm familiar with implement it to at least some degree. In the GNU implementation, the structure always has space for a pointer to point to heap allocated stuff. If your 'function' is just a pointer (or something as big as a pointer) it stores that. Since there aren't really any tradeoffs with providing at least /some/ form of Small Function Optimization, there's no reason for an implementation not to write it in. (This is in contrast to Short String Operation, where it can clash with Copy-on-Write.)

So, the main reason the Standard doesn't allow one to specify the amount of space reserved for Small Functions is that it doesn't talk about it.

There is a trade-off, however.

ceph::function<void(int), 4 * sizeof(void*)>

Is a different type than

ceph::function<void(int), 3 * sizeof(void*)>

This is good, in that it means that in our heap_allocations::forbid case, we can reject, at compile time anything that the type system can't guarantee will fit in our reserved space.

The cost is that they differ by type. A reference to one type won't reference an object of the other type. This means that you'll need to use a template and universal reference, like:

  template<typename F>
  void do_stuff(F&& f) {
    auto o = new Thing(std::forward<F>(f));
  }

And if you want to overload do_stuff, you'll need to use enable_if to disambiguate the function from the other stuff. One day we shall have Concepts. And if they don't give us too broken down and wimpified a version it should make these problems go away. But for now we need enable_if. There's a good chance that passing functions by universal reference and forward right until the point they get stored somewhere is going to be slightly more efficient at runtime than having the function take a std::function&/ceph::function& parameter, depending on what the compiler does (I haven't looked at the assembler for this case but I suspect it would call the std::function constructor at the point of function call and the move constructor at the point of storing it into the data structure), so you might want to do this anyway.

It also means that you can't store pointer/reference to functions with a different reserved size in the same container. In practice this doesn't really come up, since if I'm a subsystem with a bunch of OnComplete functions that I've stored and I want to gather up a list of references to them I already control what I've stored. Or if you really need to you can combine a ceph::function with a reserved size of 1 * sizeof(void*) with std::ref to get a 'universal function reference' at the cost of a double indirection and two virtual function calls where one would normally be required.

There is also a potential runtime cost in the move constructor. If our ceph::function heap allocates its storage and we then use swap, move assignment, or move construction, we just end up copying a pointer at each move. If everything fits in reserved space, all these pointer copies become fairly substantial memcpies. Since Ceph normally stores a callback function in the structure describing an operation, leaves it there, and then calls it, this shouldn't hurt us too much. (And is one of the reasons I recommend using a universal reference and std::forward above rather than accepting a ceph::function reference and then std::moving it into its final home.) For cases where we do want to 'move' them around a lot, we can use std::ref and other tricks to turn the memcpies back into pointer copies.

Thank you!