Condense 3 floats to a uint64_t
I'm working on a program that generates a lot of instances of a class (millions). The class is really simple, it just holds 3 floats.
Now the range these floats can live in exists between 0 and 1 (color values) and most often they are pretty simple values (1, 0.5, 0.25, 0). In an interest to save memory, I thought making a pool of the开发者_如何转开发se classes would work best, since they are only used in read-only circumstances, so they can be shared in multiple places pointer style.
I figured storing these objects in a unordered_map would work, and would use a key value of uint64_t. The value of the key would be the upper 21 bits of each float.
Is there a succinct way to get the upper 21 bits of each float and put them all together into a single 64 bit integer?
Or is this all just a dumb way to go about this? I'm not really sure if this will be effective or advantageous, but if it works I was going to extend it to other classes in the program (position values, shapes, etc that could end up sharing values)
You should just use a byte for each color value (24bit color). That would make each one of your classes take up 1/4(1/3 with padding) the space. That is the highest quality format each pixel of your screen is rendered in anyways.
Edit: If you want to save even more space you could have the classes store 16bit color in RGB565 format. And then you could use the short this would make, as a key if you need to do color lookups.
Or is this all just a dumb way to go about this?
Yes.
Keep it simple.
精彩评论