The variables of a program (on modern non-embedded architectures) can live in one of 2 places, the RAM and the registers. The RAM is indexed and registers can be accessed directly in the opcodes.
The address of a variable is the index where it lives in the RAM, a register doesn't live in the RAM so there is no way to create a meaningful address for it unless you put it on the stack but that defeats the purpose of the register
keyword.
As fun as the keyword is, these days compilers are good enough to decide for themselves which variables never need to get copied on the stack or not (the sub branch there is called register allocation).
And as with most old school optimizations giving hints to the optimizer is more likely to confuse and/or hinder it if you don't know exactly what the results are. Because optimizers are geared towards the idiomatic language constructs, and unless there is a bottle neck in the function don't worry about it. Don't shave off 1 or 2 cycles of a rarely used function when the true bottle neck is the DB access.
The approach I would recommend is to focus on the interface of your key-value store, so as to make it as clean as possible and as nonrestrictive as possible, meaning that it should allow maximum freedom to the callers, but also maximum freedom for choosing how to implement it.
Then, I would recommend that you provide an as bare as possible, and as clean as possible implementation, without any performance concerns whatsoever. To me it seems like unordered_map
should be your first choice, or perhaps map
if some kind of ordering of keys must be exposed by the interface.
So, first get it to work cleanly and minimally; then, put it to use in a real application; in doing so, you will find what issues you need to address on the interface; then, go ahead and address them. Most chances are that as a result of changing the interface, you will need to rewrite big parts of the implementation, so any time you have already invested on the first iteration of the implementation beyond the bare minimum amount of time necessary to get it to just barely work is time wasted.
Then, profile it, and see what needs to be improved in the implementation, without altering the interface. Or you may have your own ideas about how to improve the implementation, before you even profile. That's fine, but it is still no reason to work on these ideas at any earlier point in time.
You say you hope to do better than map
; there are two things that can be said about that:
a) you probably won't;
b) avoid premature optimization at all costs.
With respect to the implementation, your main issue appears to be memory allocation, since you seem to be concerned with how to structure your design in order to work around problems that you foresee that you are going to have with respect to memory allocation. The best way to address memory allocation concerns in C++ is by implementing a suitable memory allocation management, not by twisting and bending the design around them. You should consider yourself lucky that you are using C++, which allows you to do your own memory allocation management, as opposed to languages like Java and C#, where you are pretty much stuck with what the language runtime has to offer.
There are various ways of going about memory management in C++, and the ability to overload the new
operator may come in handy. A simplistic memory allocator for your project would preallocate a huge array of bytes and use it as a heap. (byte* heap
.) You would have a firstFreeByte
index, initialized to zero, which indicates the first free byte in the heap. When a request for N
bytes comes in, you return the address heap + firstFreeByte
and you add N
to firstFreeByte
. So, memory allocation becomes so fast and efficient that it becomes virtually no issue.
Of course, preallocating all of your memory may not be a good idea, so you may have to break your heap into banks which are allocated on demand, and keep serving allocation requests from the at-any-given-moment-newest bank.
Since your data are immutable, this is a good solution. It allows you to abandon the idea of variable length objects, and to have each Pair
contain a pointer to its data as it should, since the extra memory allocation for the data costs virtually nothing.
If you want to be able to discard objects from the heap, so as to be able to reclaim their memory, then things become more complicated: you will need to be using not pointers, but pointers to pointers, so that you can always move objects around in the heaps so as to reclaim the space of deleted objects. Everything becomes a bit slower due to the extra indirection, but everything is still lightning fast compared to using standard runtime library memory allocation routines.
But all this is of course really useless to be concerned with if you don't first build a straightforward, bare-minimal, working version of your database, and put it to use in a real application.
Best Answer
The point is that
this
is an implicit formal parameter (containing the address of the object whose method you are calling). It is not a local variable.Look at the generated code of your program. I compiled (on Linux/Debian/Sid/x86-64 with GCC 4.9.1) your example
arman.cc
withand got the function
main
belowYou see that some space for your
vec
is allocated on the stack (e.g. withsubq $48, %rsp
etc...) and then the address of that zone on the stack is passed as thethis
formal argument (using the usual x86-64 ABI conventions, which dictates (p 20) that the first argument of function is passed thru register%rdi
) so you could say thatthis
is, at the beginning of some member function, in the register%rdi
...IIRC, the wording of the C++ standard are vague enough to permit the
this
argument to be passed in a special way, but all the ABIs I heard of are passing it exactly as the first (pointer) argument of usual C functions.BTW, you should trust the compiler and let it pass
this
as convenient and as prescribed by ABI specifications.