Challenges in writing our RavenDB client in C++
I’ll be writing a lot more about our RavenDB C++ client, but today I was reviewing some code and I got a reply that made me go: “Ohhhhh! Nice”, and I just had to blog about it.
This is pretty much a direct transaction of how you’ll write this kind of query in C#, and the output of this is a RQL query that looks like this:
The problem is that I know how the C# version works. It uses Reflection to extract the field names from the type, so we can figure out what fields you are interested in. In C++, you don’t have Reflection, so how can this possibly work?
What Alexander did was really nice. Given that the user already have to provide us with the serialization routine for this type (so we can turn the JSON into the types that will be returned). Inside the select_fields() call, he constructed an empty object, serialize that and then use the field names in the resulting JSON to figure out what fields we want to project from the Users documents.
It make perfect sense, it require no additional work from the user and it gives us consistent API. It is also something that I would probably never think to do.
Comments
You're just asking for it. The C++ community will bash you for doing such "inefficient things" (but it's not like they would have not done for doing it any other way)
It's not necessarily too bad a solution, provided that such projections are cached somewhere and not recomputed on each call. I'd be willing to wager this solution could still outperform C#'s reflection.
Pop Catalin, We can cache the result, so you only pay it once. And there is an overload you can use to specify the exact fields you want at no cost.
Aleksander, It is fast, yes. But I wouldn't be on being faster than reflection. Because reflection is known to be slow, it got a lot of perf optimizations
Comment preview