Search Unity

Are Bytes actually useful?

Discussion in 'Scripting' started by Marscaleb, Dec 17, 2014.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    I'm creating some scripts for my game, and I have some variables that are always going to be low numbers. For example, one will never be anything other than 1, 2, 3, or 4.

    Instinctively I want to make that variable type be a byte instead of an int. But I honestly wonder if there is really any technical difference on our modern computers.
    I know in ye olden days it saved a bit of memory and even processing power to use a byte instead of an integer. In modern times that amount of memory is practically invisible. But I wonder if it is technically no different.

    If I understand correctly, a 32-bit processor will take a whole cycle to process any variable up to 32-bits, so whether I use a int or a bool it still takes up exactly the same amount of processor power to compute. And with 64-bit processors, even more variable types take only one cycle. So for modern systems there is no difference on the processor if I use an int or a byte. (Correct me if I'm wrong.)

    But what about memory? Does the way our systems allocate memory also nullify the slight difference between a byte and an int? Or do I still gain that boost to my memory?
    I know that the difference in memory is negligible, seeing as how it accounts to about 1/786432th of a small texture file. But I still wonder if there is an actual difference or if it gets swallowed up with modern architecture.

    Furthermore, is there any other advantage I am not seeing? I mean, bools are still useful because there's a lot of operations you can perform with them that you can't with ints. But I can't think of such an advantage with a byte.

    I'm coding in C#, by the way. In case it makes a difference.
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    In c# bytes are always an unsigned 8 bit integer, so you will save memory over using int or long etc, but unless you are storing millions of them (for example a database) really there is no reason to use them. I used them in some non game stuff as we were handling millions of records with someones age in the data. I mean really, I would be more worried about those variables increasing over the 255 byte limit in the future (due a game design change) than the space saving you would be getting.
     
    muzboz and MintTree117 like this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    @Korno... well, technically not.

    A field will almost always take up 32-bits (unless more is needed), no matter if it's 8-bits, 16-bits, or 32-bits. The left over space will just be padding. You can actually pack them in tighter using the StructLayoutAttribute:
    http://msdn.microsoft.com/en-us/lib...pservices.structlayoutattribute(v=vs.90).aspx

    When operating them on the stack, again, the size uses padding, because the processor operates at some word size. commonly 32-bit or 64-bit.

    If you have things like arrays, they pack in tightly. So an array of bytes will take up as much memory as each individual byte (rounded up to the nearest 32-bits for off sized arrays).


    @OP - if you're laying out a struct or class that uses several fields that could fit into smaller numeric types. Then use the StructLayoutAttribute (for instance, the Color struct would use 4 bytes, all packed in as tightly as possible so the overall struct is only 32-bits). Otherwise, just use whatever is works for what you need.
     
    BBeck likes this.
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I'm working on a Unity project now where I'm packing a color and another piece of information into two bytes, to address LEDs over a serial connection to an Arduino.
     
  5. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    It really, really depends on exactly what you are doing. 99.9% of the time I just use ints, but when writing my voxel engine I used bytes extensively due to both memory constraints and bandwidth constraints (multiplayer).
     
    Vanamerax and Tomnnn like this.
  6. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Voxel anything is a great reason to optimize everywhere you can.

    @Marscaleb how about making an enum? Values that are only 1-4 sound like they mean something important. If it's to designate a type or ID, an enum might help. If it's literally just the scope of a numeric value, then sure, use the smallest numeric datatype you want lol.
     
  7. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    Yes and no. I suppose variables like "current player" and "controller number" could be used as an enum, but that would just be a waste to make an enum of "player 1, player 2" and so forth. ;)
    There's also a number of other variables that conceivable could be shifted to being bytes; player lives and health and ammo counts. Honestly a good half or more of the variables used in a game could be converted to bytes because they never really get that high.

    Oh, and I had another thought about this. What about online games?

    Quite frankly, any type of game could really benefit from using smaller packets for data. Even with high bandwidth, a lot of folks like to have online voice chat while they play, and saving a couple nanoseconds of data transfer can actually make a difference.

    Would using bytes instead of ints reduce data to send in an online game? Assuming you were talking about a variable that was actually going to be sent to the other players, of course.
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes. In memory, though, a byte is just a 32-bit int with 3 of the bytes masked out and unused. Unless it's a byte array, in which case a byte is really a byte. Also structs can be complicated, since I believe bytes are padded out as necessary to align other types that might be in the struct.

    --Eric
     
    MintTree117 and JanTuts like this.