Changing type from int to char?

  • QuanTech
    22nd May 2017 Member 0 Permalink

    In struct Particle, I've noticed that Particle->type is an integer. But since types only go up to 255 (1 byte), shouldn't it be a char? Assuming this change wouldn't muck things up, you could save 235008 * 3 bytes ~= 705 KB of RAM!

  • jacob1
    22nd May 2017 Developer 0 Permalink
    Structures in C/C++ are aligned to certain offsets. An int has to always be aligned to a 4 byte offset. So if you have a char followed by an int, the char will take up one byte, then it will need 3 bytes of unused padding to ensure the following int is aligned. The only way to save space would be to put more chars / non-4 byte aligned fields after the char.
    I think there's also some way to force it to pack the fields together, but this is slower than allowing them to be aligned properly.

    Basically, if we change the int to a char it will still use the exact same amount of space due to padding.

    Also, I want to increase the element limit to 1024, so it would have to be a short.
  • QuanTech
    22nd May 2017 Member 0 Permalink

    @jacob1 (View Post)

     Ah yes, I can't believe I forgot about null padding... :(. Also, why would you need 1024 element slots??

  • jacob1
    22nd May 2017 Developer 0 Permalink
    @jacob1 (View Post)
    Custom elements. People already hit the 255 limit all the time because they add more than 50 elements using massive packs.

    I could do 512 ... but if i'm already changing it I would go for 1024 :). The element limit in bits would probably be a #define for easier changing later.
  • QuanTech
    22nd May 2017 Member 0 Permalink

    @jacob1 (View Post)

     Why not go all the way and do 4,294,967,295 elements since you use ints to represent types! :D

  • jacob1
    22nd May 2017 Developer 0 Permalink
    @QuanTech (View Post)
    We store type and particle ID in pmap. The first 8 bytes are for type, the rest of the 24 bits are for particle ID.

    I don't remember how many free bits we have, but we definitely have to ensure there is enough room to store particle IDs up to 235008. And it's always nice to allow even more if people want to increase the window size at some point in the future.
  • QuanTech
    22nd May 2017 Member 0 Permalink

    @jacob1 (View Post)

     I was being sarcastic lol