Byte Vs. Integer

Pretty simple question. If I have a very basic value that I don’t need to exceed 100, what type of variable should I use: a byte, or an integer?

I know on the technical end there’s a bit of conversion that needs to happen for a byte to convert to an integer, as almost all Blueprint operations are done in integers. But at the same time, I really don’t need the tremendous size of a 64-bit or 32-bit integer, which are 8-4 times as memory-intensive as a single 8-bit byte: I just need a basic value.

This is probably a completely pointless question to ask because the size of one integer is hardly worth mentioning, but I’m asking anyways because if this item is going to be duplicated around several hundred times, if there is a benefit to using one method over the other, I’d like to know.

Well, I have variables that never go over 100 (say, Gender [1,2], HairStyle [1-10], and so on), and I used byte on it. I’m one of the guys that say that the less bytes you send over the network, the better. Without you noticing, by using Integers you might end up using alot of bandwidth… you might not notice it with 10-50 players, but you will eventually notice something reaching 200+ (taking into account all the other stuff being send/received from/to the server/client)

I’d go with bytes :stuck_out_tongue:

Well, if you have more than only one value, you could combine them into an integer. Like RGBA values are encoded in a 32 bit value.
Just multiply/divide your values by 256, 655535, etc. to access them. (Or bool them against bitmasks) You just incurr this additional operation and for other datatypes like boolean, the savings are even greater.

This way you would also save the protocol overhead of sending multiple bytes. With sending a 32 bit integer you can send 4 gender/hairstyle, etc. data.

If only BP had bit shift/rotate functions. Even assembler can do that :stuck_out_tongue:

That kind of optimization seems very extreme. I just finished making a Blueprint to generate the placement of static meshes for a Greek-style fence, and one of the variables is just a number for however many posts the fence should have. While there is some concern while dragging the slider up and down in the editor, it’s not a lasting problem as the byte for most objects are converted upon being changed, not ticked once per frame. It seems there’s really not much of a difference using Bytes or integers, even though bytes require computational conversions while integers cost more memory. The differences are very negligible.

The only thing you might want to consider is that many C++ and C compilers align memory on whole-word (that is, 32 or 64 bit, depending on the processor) boundaries. Fetching memory is much, much more efficient this way. Therefore, if you have two byte sized variables; you might expect them to only take up two bytes of memory; but when you get into alignment, it could easily still be eight or sixteen bytes regardless of their size. For an IDE like UE4, this is likely taken care of behind the scenes.

I painfully remember days, say twenty years or more back, when we used to use odd combinations of unions and structs just to get around this. It’s laughable to think that this is going to be a problem on a modern computer, outside of edge cases; but it does still exist. (It’s a very real concern if you’re working with something like an Arduino.) For many of my projects, I’ll choose an int or float over a byte simply because look-up time is faster for them, and there is no guarantee of memory saving (only a possibility).

If you’re really worried about the memory ratio between an int and a byte, though, it’s probably time to upgrade your rig, man!

The network today is already very established, a byte here and there isn’t going to make a difference. Use whatever works.

I do not agree on this one. There’s still a lot of people who haven’t access to fast broadband.
Also it’s not about using a few bytes vars here and there, but eventually replace all the integers that don’t need to be so. Using more space than what required is a bad practice/poor coding anyway. IMO using byte or uint8 should always be considered when you don’t need to exceed 255

lol most people around the world barely have speed enough to play a youtube video at 720p;
That is not “very stabilished” network, not all… That is something around 200kB/s max cap.

Developing on NWN1 for a persistent server gave some real insight into how Bioware set up their networking. Their requirement is 22kbps for each any every client connection (run serially for each client). So for the OP this is the first number you need to figure out (my budget is set to 54kbps). After that it is simply a matter of what variables need to be stuffed into the stream spec. The difference in speed between a Byte and an Integer is nothing and the memory is nothing in a 32 or 64bit system as mentioned above.

In my real life job I work in the Programmable Logic Controller (PLC) industry and am very good at what I do. There are Bools, Bytes, Ints (16 and 32), Floats…simple as that. When doing telemetry (networking) it is extremely important to do most of the traffic in 16bit Integer (Floats are local use only. Radio Telemetry is usually 1200/4800/9600baud and handles other than Ints poorly). So bools and bits are ‘packed’ into an integer before sending and ‘unpacked’ on the other end to be used. Since ~1998 I have rarely used Bytes any longer because of the additional pain it is to work with them. Most compare and math functions/blocks are either in Integer or Float…which means doing masking on the Ints with Hex to do math for Bytes…Really worth the effort? Lastly, to troubleshoot someones logic when they used Bytes is a pain in the ***.

So my solution while I was in Unity (based upon Bioware NWN1 experiences) was to run a change request heartbeat on 500ms intervals for variables that only change so often. I run these with Ints and let the code do the work when the info has changed. I will be using the same technique in UE4 but just have not gotten there yet in the conversion process.

Just my two coppers. lol

32 bit integer is 4 bytes.
4 bytes
if you have a 1 kb line, you can transfer 250 integers per second.
Let me give you a hint, today none has a 1 kb line.

On 144th place we have “Niger” (I never even heard of em), they have 0.6/0.2 MB
That is 600000/200000 b/s. i.e. you can download 150000 integers/s or

I sincerly think that an integer here and there isn’t going to make any difference.
In that case, you would merit more spending time trying to optimise other things, such as creating well defined structures and refraining from sending redundant information.
Putting effort to change integers to bytes really isn’t worth it. It may have been worth it 10 years ago on embedded systems, but this is 2017. If they don’t have the Internet speed to deal with a few computers, then that’ll be the least problem. They most likely won’t be able to sustain your game with their PC either ;D

edit:
But of course, using unsigned integers is something I would recommend, but not for memory reasons, but because a uint is positive, and it should always be positive. So using a uint on for example a persons HP would make a lot of sense, since it’s not supposed to go below 0.

I should’ve mentioned in my post above, a 720p youtube video is definitely more than 200 kB/s.

720p

  • Resolution: 1280x720
  • Video Bitrate Range: 1,500 - 4,000 Kbps

Well, 200kB IS 2000 Kbps. Again, it is not “definitely more”; it’s the average range of (1500 to 4000 kbps).

And 4000 kbps is 500 kB/s.
Although I must correct myself, the reference I used was for YouTube’s live stream. Which is not using the same bitrate as their videos.
The videos has a higher bitrate which is YouTube recommended upload encoding settings - YouTube Help

Type Video Bitrate, Standard Frame Rate (24, 25, 30) Video Bitrate, High Frame Rate (48, 50, 60)
720p 5 Mbps 7.5 Mbps

So a normal 720p video with 24-30 fps would require 5 Mbps, which is 0.625 MB/s

Edit:
Don’t forget we’re comparing with the worst case scenario, which is far from the average case.

If internet today were any good, nobody would have to pay for services like this to play: http://www.wtfast.com
If all we need were only bandwidth we’d be good; outside South Korea, internet services are pretty much very poor and the more data you transfer the less happy players you’ll have.
LoL is a very very lightweight game and still can’t run properly on a lot of those 2Mb connections because the world wide web is total mess, what one takes for granted is almost impossible for others…
It’s like, graphical fidelity, the more you have it the less people will be able to run it. If he can reduce traffic as much as possible, I say go for it.

Sorry but are you aware of what that website does? It has absolutely nothing to do with the bandwitdh as the amount of information that needs to be transferred for your game to function properly, has to remain the same.
The only thing that website could possibly do, is to attempt to decide a smarter route by choosing a “smart” DNS, thereby hopefully - potentially - lowering the latency, which is the end-to-end delay.
And according to a partially untrustworthy source, League of Legends uses about ~3.5kB in and 0.6kB out per second (~6-7 MB incoming data and ~1 MB outgoing for a 30 min game quoted from Kbox, software engineer at LoL).
http://forums.euw.leagueoflegends.com/board/showthread.php?t=191041
So whether LoL lags due to insufficient bandwitdhs or not is up to you to verify if you’re interested. I’m just saying, The internet today will seriously not be affected if you use bytes instead of integers.
Like I said before, you’re better off just using integers, thereby speeding up your development process allowing you to spend more time on optimising things that actually do matter.

Dude, those are averages.
According to that map, Germany has 28.8 Mb/s. That doesn’t mean that everyone has that speed. It means that there’s a few people that are at 100Mb/s and a shitload that are at lower speeds.
Especially in rural areas in Germany, you will generally get a value between 2Mb/s and 16Mb/s.

You should also consider mobile usage. A throttled mobile phone will have a speed somewhere between 32Kb/s and 386kb/s.
However, that’s only in theory, as the way throttling is done makes the connection useless even when you aren’t breaking the speed boundaries, as the provider just makes your connection drop a percentage of your packets. So let’s disregard it.
Even unthrottled, you will hugely impact how the game is played. An LTE connection might be capable of 100Mb/s, but if you actually use the full bandwidth here I can guarantee you that no one will play the game, as the average data limit would be burned through in mere seconds. Even a difference between 512kb/s and 2mb/s will be felt by a player at the end of the month, depending on how long sessions in the game are. Data limits tend to be in a ballpark between 100MB and 10GB currently, at least in countries that have data limits.

But remember, people aren’t just connecting to the internet to play one game. It’s nice if your player has a 2Mb/s connection and your game only uses 1.5Mb/s, but what happens if their connection goes unstable for a momentum and drops to 1Mb/s? Or what if their email program is synchronizing in the background and using 0.1Mb/s? And then they add a voice chat program and suddenly you are over the limit.

It makes quite a difference when your game is, for example, an RTS and uses either 64 B/s or 256 B/s to update the status of a single unit. That’s either 512 b/s or 2kb/s.
Get 1024 units moving and you are suddenly at either 512 kb/s or 2mb/s.
(I have no idea what the real ballparks are here, so I’m just focusing on the possible size differences of optimized vs unoptimized integers. These numbers feel plausible for multiple ticks per second, though.)

Yes those are averages indeed, the example also shows the 144th place of average internet speeds, then you should also remember, there are more users with lower speeds than users with high speed in those countries.
I would “almost” agree with you when it comes to mobile games. But I still stick to my reasoning. It is more worth spending time optimising what you send, rather than how you send it. For example, you might not need to send an update of the a persons HP every frame in a mobile game, you could instead of updating the movement every frame, you can increase the movement speed and update it every other frame instead.

Of course, the way I explained it is very simple and a real implementation is more challenging, but I hope I got my point across.
In other words, focus on what actually matters instead of on minor details that doesn’t have any major impact. Prioritise your time.

I stumbled upon this old thread. But i think the question is still relevant and the answer may not be obvious for coding beginners:
Byte is almost certainly slower to process than integer. So if you think about making a property on an actor a byte or a integer, always go with the integer. Only use byte in arrays or structures, were you perform operations on large sets of data. Only in this instance you will actually save memory and also can improve performance by using SIMD instructions.

For networking you also don’t save any bandwidth as you should quantize values befor sending them. So you pack your integer into a byte on network serialization if you know it never exceeds 255.

If you work in blueprints only, this is all not relevant to you. If you need to optimize for performance or memory usage the first step would be to translate the piece of code, which needs to optimizations to C++ anyway.

2 Likes