11.12.2013, 12:19
(
Последний раз редактировалось Emmet_; 14.12.2013 в 20:35.
)
Packed strings in PAWN
Introduction
Packed strings have been a feature in SA-MP and PAWN since the beginning. However, many people are unaware of packed strings and the memory reduction! This tutorial will teach you the basics of packed strings, and how to manipulate them accordingly.
What is a packed string?
A packed string is an array that stores data in each byte, rather than each cell, opposed to regular arrays. Packed strings are stored in Little Endian (meaning, lower byte first) and can only hold ASCII characters from 0 to 255, and anything exceeding those numbers will just wrap around.
Picture this code:
'a' is stored in cell 0, 'b' is stored in cell 1, and so on. A cell is basically 4 bytes, so if we do the math, that string above is approximately 20 bytes in size, so each character is stored in a 4 byte set.
However, with this code:
'a' is stored in byte 0, 'b' is stored in byte 1, and so on. Basically, that string above is only 8 bytes in size and contains only 2 cells!
You're probably wondering why that string isn't 5 bytes. Using "char" automatically rounds the number to the next nearest multiple of 4 (for example, 1 turns into 4, 3 turns into 4, 5 turns into 8, 23 turns into 24, and so on).
So you're saving 3-4 times the memory with packed strings!
Areas of use
You might think that packed arrays are useless. If so, then you're wrong. There are many uses for packed arrays, and not just for memory reduction!
Packed strings are fluently supported in pure PAWN. However, most of the SA-MP natives do NOT support packed strings, such as format, GetPlayerName, etc.
If you plan on using packed strings, you have to rely on "strpack" and "strunpack" and the other string functions, which you can find in string.inc.
Unpacked arrays in PAWN store data in each cell. Packed arrays in PAWN store data in each byte, which means that you can't use square brackets, like you do with unpacked arrays, to access and obtain data inside packed arrays.
Also, to set a packed string, you have to place an exclamation mark before the string, to denote that it's a packed input.
Conclusion
There is more information on packed strings in the PAWN language guide - you can get it here:
https://sampforum.blast.hk/showthread.php?tid=289258
You can also read the "String Manipulation" guide, which covers most information about packed strings:
http://www.compuphase.com/pawn/String_Manipulation.pdf
Anyways, I hope you learned something new today (and if you didn't, go sit in the corner and think about what you've done). Thanks for reading!
Introduction
Packed strings have been a feature in SA-MP and PAWN since the beginning. However, many people are unaware of packed strings and the memory reduction! This tutorial will teach you the basics of packed strings, and how to manipulate them accordingly.
What is a packed string?
A packed string is an array that stores data in each byte, rather than each cell, opposed to regular arrays. Packed strings are stored in Little Endian (meaning, lower byte first) and can only hold ASCII characters from 0 to 255, and anything exceeding those numbers will just wrap around.
Picture this code:
pawn Код:
new string[5];
string[0] = 'a';
string[1] = 'b';
string[2] = 'c';
string[3] = 'd';
string[4] = '\0';
However, with this code:
pawn Код:
new string[5 char];
string{0} = 'a';
string{1} = 'b';
string{2} = 'c';
string{3} = 'd';
string{4} = '\0';
You're probably wondering why that string isn't 5 bytes. Using "char" automatically rounds the number to the next nearest multiple of 4 (for example, 1 turns into 4, 3 turns into 4, 5 turns into 8, 23 turns into 24, and so on).
pawn Код:
// This array is 5 cells big and 20 bytes in size.
new string[5] = "abcd";
// This array is 2 cells big and 8 bytes in size.
new string[5 char] = !"abcd"
Areas of use
You might think that packed arrays are useless. If so, then you're wrong. There are many uses for packed arrays, and not just for memory reduction!
- Sparse arrays
Firstly, read my tutorial on sparse arrays here:
https://sampforum.blast.hk/showthread.php?tid=480439
Sparse arrays are simply arrays that have most of it's data empty very often. However, you can simply save some memory by using packed arrays!
pawn Код:#define MAX_ITEMS (64)
// 8,192 cells = 32,768 bytes!
new gData[MAX_ITEMS][128];
// 2,048 bytes = 8,192 cells!
new gData[MAX_ITEMS][128 char]; - Infrequently used strings
Very often, you'll find yourself saving strings into memory and infrequently using them (e.g. rarely using them). Packed arrays are a great use for these type of arrays.
- Mass data storage
Going back to the "sparse arrays" section above, if you have mass data storage then it's best to use packed strings.
- Memory reduction!
Packed arrays save you 4 times less the memory, so why store data which is 4 times more memory consuming, when you can use packed arrays?
Packed strings are fluently supported in pure PAWN. However, most of the SA-MP natives do NOT support packed strings, such as format, GetPlayerName, etc.
If you plan on using packed strings, you have to rely on "strpack" and "strunpack" and the other string functions, which you can find in string.inc.
- Formatting strings
The format and printf functions don't support packed strings, so you'll have to use strpack:
pawn Код:new
string[128 char];
strpack(string, "Hello world!");
pawn Код:new
string[128 char],
temp[128]
;
strpack(string, "Emmet");
strunpack(temp, string);
format(temp, sizeof(temp), "%s likes to eat %s.", temp, "Big Macs");
strpack(string, temp);
Edit: As of December 14, 2013, you can now use format with packed strings, using my library!
https://sampforum.blast.hk/showthread.php?tid=481257
- Functions that ARE supported
All of the string functions in string.inc support both packed and unpacked arrays.
The fread and valstr function also accepts an optional "pack" parameter for support with packed arrays, so there's no need to worry whether they will work with them or not.
Unpacked arrays in PAWN store data in each cell. Packed arrays in PAWN store data in each byte, which means that you can't use square brackets, like you do with unpacked arrays, to access and obtain data inside packed arrays.
pawn Код:
// Incorrect.
if (g_PackedString[0] != '\0')
{
g_PackedString[0] = 'h';
g_PackedString[1] = 'i';
}
// Correct!
if (g_PackedString{0} != '\0')
{
g_PackedString{0} = 'h';
g_PackedString{1} = 'i';
}
pawn Код:
// Incorrect. This will try and store the string in each cell, which is not what we want!
g_PackedString = "Hello world.";
// Correct!
g_PackedString = !"Hello world.";
There is more information on packed strings in the PAWN language guide - you can get it here:
https://sampforum.blast.hk/showthread.php?tid=289258
You can also read the "String Manipulation" guide, which covers most information about packed strings:
http://www.compuphase.com/pawn/String_Manipulation.pdf
Anyways, I hope you learned something new today (and if you didn't, go sit in the corner and think about what you've done). Thanks for reading!