0

I have a large data in RAM, where for each user there are several unsigned short (2 bytes) values. Their amount is ranged from 0 to 256, but the real case is that I have 12M users with just 1 value, 6M with 2, 3.5M with 3 etc. up to 1 user with 60 values.

I need to choose a datatype that would be memory, access and modification-efficient. I wanted to use vector for such a purpose, but the size of the empty vector itself is 24 bytes. This is too much for my RAM.

Now I want to use std::array<>, because it doesn't allocate any additional bytes except the space for the data inside, i.e.

sizeof(std::array<unsigned short, 3>) // 6UL
sizeof(std::vector<unsigned short>) + sizeof(unsigned short) * v.capacity() // 24UL + 6UL

The problem is that std::array must specify the size on compile time. As you could guess, I can't do it. Is there a workaround? I store my users in hast_table<unsigned int, T>. Can I, for example, store arrays with different size for each key?

5
  • Can't be done without playing games that'll probably push you back up into vector memory consumption territory. You could allocate the maximum possible size for all, but that's also a huge waste. You're going to have to do something custom, I think. Commented Jan 19, 2021 at 7:54
  • The size of a std::array is part of it's type, so you can't have different size arrays as part of a hash table. Have you considered std::unique_ptr<unsigned short[]>? Commented Jan 19, 2021 at 7:54
  • Or your own vector like class but with something like short string optimization to avoid heap allocation for short vectors. Commented Jan 19, 2021 at 7:56
  • @user4581301 can you give me a hint where I can start searching for that custom type. I have no ideas Commented Jan 19, 2021 at 8:14
  • 1
    @vamirio-chan You don't search for a custom type, by definition you write it yourself. Think about what you need (a vector with a smaller overhead) and start coding it yourself. Commented Jan 19, 2021 at 8:40

1 Answer 1

0

No. I am afraid it does not work with different size!

Sign up to request clarification or add additional context in comments.

1 Comment

While this is true, the asker can't have varying sizes of arrays, there will be work-arounds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.