Lol byte alignment has always gotten me.
The archive I'm working with stores null-terminated strings that are byte-aligned to the nearest 4 bytes.
So if the string is 9 chars (including the null-byte), it will take up 12 bytes to store.
If it takes 8 chars, it just takes 8 bytes.
The current way of parsing is just to save the current position, read the string, save the current position, do math to get the string length, and then read some extra bytes to cover the alignment.
Is there a better way?
The Forum is up for sale: XeNTaX Forum looking for new owner
quickbms byte alignment
- Dinoguy1000
- Site Admin
- Posts: 786
- Joined: Mon Sep 13, 2004 1:55 am
- Has thanked: 154 times
- Been thanked: 163 times
Re: quickbms byte alignment
Wow, that's a retarded way to store strings; it offers absolutely no space savings over just saving the byte length of the string (and since, on average, the string field will overrun the actual string length, it ends up using *more* space in most cases).
-
finale00
- M-M-M-Monster veteran

- Posts: 2382
- Joined: Sat Apr 09, 2011 1:22 am
- Has thanked: 170 times
- Been thanked: 307 times
Re: quickbms byte alignment
They're just losing at most 3 bytes per string it's cool.Dinoguy1000 wrote:Wow, that's a retarded way to store strings; it offers absolutely no space savings over just saving the byte length of the string (and since, on average, the string field will overrun the actual string length, it ends up using *more* space in most cases).
The retarded part is when they're duplicating the string for no seemingly good reason so that in every entry the same string appears twice
-
howfie
- double-veteran

- Posts: 929
- Joined: Fri Jul 08, 2011 12:06 pm
- Location: Torrance, CA
- Has thanked: 10 times
- Been thanked: 274 times
Re: quickbms byte alignment
you would be surprised how bad some games are coded... how inefficient or silly they sometimes do things. for example, saints row third, all spaceship models use a vertex format that has 0xFF 0x00 0x00 0x00 for weights and 0x00 0xFF 0xFF 0xFF for bone indices. Takes 20,000 vertices and that's 40 KB of waste for something where all points are equally weighted and there is only one bone. Why use bones and weights at all? Why not just use another vertex format instead?
- Dinoguy1000
- Site Admin
- Posts: 786
- Joined: Mon Sep 13, 2004 1:55 am
- Has thanked: 154 times
- Been thanked: 163 times
Re: quickbms byte alignment
Not really, game development is a very weird world. For instance, I don't know about modern games, but back when memory constraints were super-tight, it apparently wasn't all that unusual for a senior/experienced developer to make the game allocate a block of memory that never got used and that the rest of the dev team didn't know about, so that if the deadline was looming and the team couldn't get memory usage under the target, said dev could just comment out the allocation for that block of memory and poof, the game meets the memory target and can ship on time (and of course the rest of the dev team would then hold that dev in awe for fixing something in five minutes that the rest of them had spent days or weeks on in vain, but it's not like that was ever a reason for using that technique...[/snipe]).howfie wrote:you would be surprised how bad some games are coded... how inefficient or silly they sometimes do things.
Until you consider the total number of strings and the average length of each string. If a game has 10,000 strings at an average length of 25 bytes per string (pulled those figures out of my ass, so they probably don't reflect reality in any way), that means that, on average, each string requires 29 bytes of storage space (28 bytes for the string itself in 4-byte blocks, and one byte for the number of blocks); this adds up to 290k space required to store the strings. If a byte length format is used to store them instead, average length per string drops to 26 bytes (25 for the string and 1 for the string length), or 260k overall - a reduction of 30k in size, or over 10% less used space.finale00 wrote:They're just losing at most 3 bytes per string it's cool.Dinoguy1000 wrote:Wow, that's a retarded way to store strings; it offers absolutely no space savings over just saving the byte length of the string (and since, on average, the string field will overrun the actual string length, it ends up using *more* space in most cases).
Of course, then you have devs turning around and doing stupid things like duplicating every string, which completely destroys any savings that might be gained from optimizing the storage format.
-
finale00
- M-M-M-Monster veteran

- Posts: 2382
- Joined: Sat Apr 09, 2011 1:22 am
- Has thanked: 170 times
- Been thanked: 307 times
Re: quickbms byte alignment
Ok so I did some math.
And that gives me my padding that I should add. 7 commands to evaluate that simple expression!
Maybe I am not breaking down the expression properly.
Code: Select all
(4 - (strLen % 4)) % 4
Code: Select all
# get the length of the string
math strlen = END
math strlen -= BEGIN
# modulus 4
math rem = nameLen
math rem %= 4
# now put it all together
math pad = 4
math pad -= rem1
math pad %= 4
Maybe I am not breaking down the expression properly.
- aluigi
- VVIP member

- Posts: 1916
- Joined: Thu Dec 08, 2005 12:26 pm
- Location: www.ZENHAX.com
- Has thanked: 4 times
- Been thanked: 661 times
- Contact:
Re: quickbms byte alignment
isn't the Padding command enough for this job?
padding 4
note that the alignment calculation is made from offset 0.
the math command has the 'x' operator that does alignment works, for example:
math TMP = 7
math TMP x= 4
print "%TMP%"
padding 4
note that the alignment calculation is made from offset 0.
the math command has the 'x' operator that does alignment works, for example:
math TMP = 7
math TMP x= 4
print "%TMP%"
-
finale00
- M-M-M-Monster veteran

- Posts: 2382
- Joined: Sat Apr 09, 2011 1:22 am
- Has thanked: 170 times
- Been thanked: 307 times
