Advertisement

Calculating how many buffers needed

Started by June 09, 2015 12:47 AM
8 comments, last by ankhd 9 years, 6 months ago
Hi.
I would like to calculate the number of buffers required to split up a large buffer.

Is this the way to do it.

Int largebuffersize = 4000000;//bytes
Int packetsize =1024;
int numberofbuffers = largebuffersize / packetsize;

Int remainder = largebuffersize % packetsize;

If(remainder >0)
Numberbuffers++;

Is this the way I should do it.

It looks fine to me. You could try it with smaller numbers and see if it lines up with your mental understanding:

Try a largebuffersize of these values and see if it returns what you're expecting

0

256

1023

1024

1025

2047

2048

2049

- Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

Advertisement

It'll be wrong with a largebuffersize of 0. You'll want to either ensure largebuffersize is always at least 1, or make the "remainder" code only run if largebuffersize is > 0.

Hello to all my stalkers.


It'll be wrong with a largebuffersize of 0.

Why? Numbuffers ends up = 0. Did you perhaps miss that NumBuffers++ is conditional? wink.png

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Largebuffersize = 0 will give 0 buffer size. But that will never happen, I'll never get to this bit of code if largebuffersize == 0.

It's only for my loadbar.

I was looking if there is other ways to do it, but no loops.

Oh and yes your data set checks out ok

It's correct, but you can do that without conditional:

numberofbuffers = (largebuffersize + packetsize - 1 )/ packetsize;
Advertisement


It'll be wrong with a largebuffersize of 0.

Why? Numbuffers ends up = 0. Did you perhaps miss that NumBuffers++ is conditional? wink.png

Oops, posting when I should have been sleeping. Thanks for spotting the mistake :)

Hello to all my stalkers.

If you are ever in doubt write a unit test to confirm :)

The only concern I've got is that they are magic numbers. Why did you pick those sizes?

Why did you pick 4000000 bytes for the buffer size? Why not 3000000 or 5000000 or (1<<22)? If you are looking for something about the size in memory, why not a full allocated page, perhaps on Windows using a size based on GetLargePageMinimum() allocated with VirtualAlloc()?

Why a packet size of 1024? Why not 1280 or 1500 or 1492? I'm not sure what your packets are, but those three numbers are used for different types of network transmission sizes as well-reasoned values.

While the numbers you picked are probably fine for whatever project you are on, it is a good idea professionally to make numbers configurable and to base them on well-reasoned values, documenting the reason for the sizes.

Ok. I'll spill the beans.

What I'm doing is that I have 8 file there is 6 files under 2kb and a .dds 0r Jpg(blend Map) that is 1024 * 1024(4000000 4mb thats the 4 mil it was a bit larger but was the last buffer I looked at) RGBA no mipmaps and a 256 X 256 .dds only file.

The 6 none texture files are made up of google protocol buffers that can be read into a std::string(the whole file will be in this string).

I then have a class that hold all serialized strings in a map, The key is the filename and the string is the data. This maps my 8 files into a class. I then for each file(string) I Zlib the buffers.

Some thing like this.

.


class FileItem
{
public:
std::string SerializedCommpressedFile;
int64_t UncompressedSize;//you need to know this to un compress
int32_t NumberOfPacket;//calculated from a passed in packet size

//setters and thinks
};



//this contains all the files a level needs and is serialized and compressed with Zlib
class LevelMapPack
{
std::map<std::string, FileItem> CompressedSerializedFiles;//key == file name

//other thinks
};

And the plan was to open image file store them in a google protocol byte(cross platform it say's)array. And at the end have a std::string with over 4mb of data.

Works great. Now all this makes up a level that the server loads for each running game(and sends them to clients, I have a post in networking, work inprogress).

A empty map with the basic is about 6mb could grow to maybe 20mb these size's are before Zlib gets to them(well 1 blend maps zipped from 4194462 bytes to 31450 bytes) not all will zip to that amount.

So at the end of all this I have a Serialized compressed level that can be sent over the wire.

Spot on Watson.

Why a packet size of 1024? Why not 1280 or 1500 or 1492? I'm not sure what your packets are, but those three numbers are used for different types of network transmission sizes as well-reasoned values.

yes my network packet(not really a packet but streamed packets only split whe put in the send and recieve queue) is 1200 bytes but just copy 1024 bytes plus some other stuff and also protocol buffers packs some data as well depending on size and layout of the buffer.

Some room to play. Which so far has plugged straight into my network layer far better then http did, But thats for my networking post after I finnish about a week or more.

I was looking for this. numberofbuffers = (largebuffersize + packetsize - 1 )/ packetsize; For my load bar and may use it for checking the current file packet sent or not

This topic is closed to new replies.

Advertisement