Skip to content

Commit 1b3c85b

Browse files
committed
Revert "PalettedBlockArray: allow getting and providing palette as raw bytes, instead of an array" and related commits
It turns out this is only useful if we want to preserve the bytes for direct reuse later. We don't really want to do this as discussed in pmmp/PocketMine-MP#6786. When the data must be decomposed into integers, transformed and repacked to binary, getting the data as binary first is actually worse for performance (assuming that ByteBufferReader is used). So it looks like we're stuck with arrays, for better or worse :( This reverts commit 8e83c8d. This reverts commit 5614724. This reverts commit a26bed6.
1 parent 7356e35 commit 1b3c85b

File tree

5 files changed

+10
-136
lines changed

5 files changed

+10
-136
lines changed

src/PhpPalettedBlockArray.cpp

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,14 @@ static bool palette_data_from_array(HashTable* paletteHt, std::vector<Block>& pa
5555
return true;
5656
}
5757

58-
static bool palette_data_from_string(zend_string* paletteZstr, std::vector<Block>& palette, zend_class_entry* exception_ce) {
59-
if ((ZSTR_LEN(paletteZstr) % sizeof(Block)) != 0) {
60-
zend_throw_exception_ex(exception_ce, 0, "palette length in bytes must be a multiple of %zu, but have %zu bytes", sizeof(Block), ZSTR_LEN(paletteZstr));
61-
return false;
62-
}
63-
64-
auto paletteEntries = ZSTR_LEN(paletteZstr) / sizeof(Block);
65-
Block* paletteValues = reinterpret_cast<Block*>(ZSTR_VAL(paletteZstr));
66-
palette.assign(paletteValues, paletteValues + paletteEntries);
67-
68-
return true;
69-
}
70-
71-
static bool paletted_block_array_from_data(zval *return_value, zend_long bitsPerBlock, zend_string *wordArrayZstr, HashTable *paletteHt, zend_string* paletteZstr) {
58+
static bool paletted_block_array_from_data(zval *return_value, zend_long bitsPerBlock, zend_string *wordArrayZstr, HashTable *paletteHt) {
7259
object_init_ex(return_value, paletted_block_array_entry);
7360
paletted_block_array_obj *intern = fetch_from_zend_object<paletted_block_array_obj>(Z_OBJ_P(return_value));
7461

7562
gsl::span<uint8_t> wordArray((uint8_t*)ZSTR_VAL(wordArrayZstr), (uint8_t*)(ZSTR_VAL(wordArrayZstr) + ZSTR_LEN(wordArrayZstr)));
7663
std::vector<Block> palette;
7764

78-
assert(paletteHt != nullptr || paletteZstr != nullptr);
79-
80-
if (paletteHt != nullptr && !palette_data_from_array(paletteHt, palette, paletted_block_array_load_exception_entry)) {
81-
return false;
82-
} else if (paletteZstr != nullptr && !palette_data_from_string(paletteZstr, palette, paletted_block_array_load_exception_entry)) {
65+
if (!palette_data_from_array(paletteHt, palette, paletted_block_array_load_exception_entry)) {
8366
return false;
8467
}
8568

@@ -116,17 +99,6 @@ static void paletted_block_array_get_palette(zval *object, zval *return_value) {
11699
}
117100
}
118101

119-
static void paletted_block_array_get_palette_bytes(zval* object, zval* return_value) {
120-
paletted_block_array_obj *intern = fetch_from_zend_object<paletted_block_array_obj>(Z_OBJ_P(object));
121-
122-
auto palette = intern->container.getPalette();
123-
124-
const Block* paletteValues = palette.data();
125-
126-
ZVAL_STRINGL(return_value, reinterpret_cast<const char*>(paletteValues), palette.size_bytes());
127-
}
128-
129-
130102
static bool paletted_block_array_set_palette(zval* object, HashTable* paletteHt) {
131103
paletted_block_array_obj* intern = fetch_from_zend_object<paletted_block_array_obj>(Z_OBJ_P(object));
132104

@@ -237,7 +209,7 @@ static int paletted_block_array_unserialize(zval *object, zend_class_entry *ce,
237209
goto end;
238210
}
239211

240-
if (!paletted_block_array_from_data(object, Z_LVAL_P(bitsPerBlock), Z_STR_P(wordArray), Z_ARRVAL_P(palette), nullptr)) {
212+
if (!paletted_block_array_from_data(object, Z_LVAL_P(bitsPerBlock), Z_STR_P(wordArray), Z_ARRVAL_P(palette))) {
241213
goto end;
242214
}
243215

@@ -282,16 +254,15 @@ PALETTED_BLOCK_ARRAY_METHOD(__construct) {
282254
PALETTED_BLOCK_ARRAY_METHOD(fromData) {
283255
zend_long bitsPerBlock = 1;
284256
zend_string *wordArrayZstr;
285-
HashTable* paletteHt;
286-
zend_string* paletteZstr;
257+
zval *paletteZarray;
287258

288259
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 3, 3)
289260
Z_PARAM_LONG(bitsPerBlock)
290261
Z_PARAM_STR(wordArrayZstr)
291-
Z_PARAM_ARRAY_HT_OR_STR(paletteHt, paletteZstr)
262+
Z_PARAM_ARRAY(paletteZarray)
292263
ZEND_PARSE_PARAMETERS_END();
293264

294-
paletted_block_array_from_data(return_value, bitsPerBlock, wordArrayZstr, paletteHt, paletteZstr);
265+
paletted_block_array_from_data(return_value, bitsPerBlock, wordArrayZstr, Z_ARRVAL_P(paletteZarray));
295266
}
296267

297268
PALETTED_BLOCK_ARRAY_METHOD(getWordArray) {
@@ -306,12 +277,6 @@ PALETTED_BLOCK_ARRAY_METHOD(getPalette) {
306277
paletted_block_array_get_palette(getThis(), return_value);
307278
}
308279

309-
PALETTED_BLOCK_ARRAY_METHOD(getPaletteBytes) {
310-
zend_parse_parameters_none_throw();
311-
312-
paletted_block_array_get_palette_bytes(getThis(), return_value);
313-
}
314-
315280
PALETTED_BLOCK_ARRAY_METHOD(setPalette) {
316281
zval* paletteZarray;
317282

@@ -428,4 +393,4 @@ void register_paletted_block_array_class() {
428393
ce.unserialize = paletted_block_array_unserialize;
429394
paletted_block_array_entry = zend_register_internal_class(&ce);
430395
paletted_block_array_entry->ce_flags |= ZEND_ACC_FINAL;
431-
}
396+
}

stubs/pocketmine/world/format/PalettedBlockArray.stub.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct(int $fillEntry){}
1414
* @param int[] $palette
1515
* @throws PalettedBlockArrayLoadException if the provided data is invalid in any way
1616
*/
17-
public static function fromData(int $bitsPerBlock, string $wordArray, array|string $palette) : \pocketmine\world\format\PalettedBlockArray{}
17+
public static function fromData(int $bitsPerBlock, string $wordArray, array $palette) : \pocketmine\world\format\PalettedBlockArray{}
1818

1919
public function getWordArray() : string{}
2020

@@ -23,12 +23,6 @@ public function getWordArray() : string{}
2323
*/
2424
public function getPalette() : array{}
2525

26-
/**
27-
* Returns the palette as a byte array of serialized little-endian unsigned integers
28-
* This is faster than getPalette() since it doesn't have to turn the palette into an array
29-
*/
30-
public function getPaletteBytes() : string{}
31-
3226
/**
3327
* The input array must be the same size as the current palette
3428
* Keys are ignored. Only input order is considered.

stubs/pocketmine/world/format/PalettedBlockArray_arginfo.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 474344accc11ae782ff1b7d3ba695bce5bfa3fc1 */
2+
* Stub hash: 4fe7c13a98dedd91dd796dd270d9351e51f1dcbf */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_pocketmine_world_format_PalettedBlockArray___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, fillEntry, IS_LONG, 0)
@@ -8,7 +8,7 @@ ZEND_END_ARG_INFO()
88
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_pocketmine_world_format_PalettedBlockArray_fromData, 0, 3, pocketmine\\world\\format\\PalettedBlockArray, 0)
99
ZEND_ARG_TYPE_INFO(0, bitsPerBlock, IS_LONG, 0)
1010
ZEND_ARG_TYPE_INFO(0, wordArray, IS_STRING, 0)
11-
ZEND_ARG_TYPE_MASK(0, palette, MAY_BE_ARRAY|MAY_BE_STRING, NULL)
11+
ZEND_ARG_TYPE_INFO(0, palette, IS_ARRAY, 0)
1212
ZEND_END_ARG_INFO()
1313

1414
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_pocketmine_world_format_PalettedBlockArray_getWordArray, 0, 0, IS_STRING, 0)
@@ -17,8 +17,6 @@ ZEND_END_ARG_INFO()
1717
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_pocketmine_world_format_PalettedBlockArray_getPalette, 0, 0, IS_ARRAY, 0)
1818
ZEND_END_ARG_INFO()
1919

20-
#define arginfo_class_pocketmine_world_format_PalettedBlockArray_getPaletteBytes arginfo_class_pocketmine_world_format_PalettedBlockArray_getWordArray
21-
2220
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_pocketmine_world_format_PalettedBlockArray_setPalette, 0, 1, IS_VOID, 0)
2321
ZEND_ARG_TYPE_INFO(0, palette, IS_ARRAY, 0)
2422
ZEND_END_ARG_INFO()
@@ -58,7 +56,6 @@ ZEND_METHOD(pocketmine_world_format_PalettedBlockArray, __construct);
5856
ZEND_METHOD(pocketmine_world_format_PalettedBlockArray, fromData);
5957
ZEND_METHOD(pocketmine_world_format_PalettedBlockArray, getWordArray);
6058
ZEND_METHOD(pocketmine_world_format_PalettedBlockArray, getPalette);
61-
ZEND_METHOD(pocketmine_world_format_PalettedBlockArray, getPaletteBytes);
6259
ZEND_METHOD(pocketmine_world_format_PalettedBlockArray, setPalette);
6360
ZEND_METHOD(pocketmine_world_format_PalettedBlockArray, getMaxPaletteSize);
6461
ZEND_METHOD(pocketmine_world_format_PalettedBlockArray, getBitsPerBlock);
@@ -73,7 +70,6 @@ static const zend_function_entry class_pocketmine_world_format_PalettedBlockArra
7370
ZEND_ME(pocketmine_world_format_PalettedBlockArray, fromData, arginfo_class_pocketmine_world_format_PalettedBlockArray_fromData, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
7471
ZEND_ME(pocketmine_world_format_PalettedBlockArray, getWordArray, arginfo_class_pocketmine_world_format_PalettedBlockArray_getWordArray, ZEND_ACC_PUBLIC)
7572
ZEND_ME(pocketmine_world_format_PalettedBlockArray, getPalette, arginfo_class_pocketmine_world_format_PalettedBlockArray_getPalette, ZEND_ACC_PUBLIC)
76-
ZEND_ME(pocketmine_world_format_PalettedBlockArray, getPaletteBytes, arginfo_class_pocketmine_world_format_PalettedBlockArray_getPaletteBytes, ZEND_ACC_PUBLIC)
7773
ZEND_ME(pocketmine_world_format_PalettedBlockArray, setPalette, arginfo_class_pocketmine_world_format_PalettedBlockArray_setPalette, ZEND_ACC_PUBLIC)
7874
ZEND_ME(pocketmine_world_format_PalettedBlockArray, getMaxPaletteSize, arginfo_class_pocketmine_world_format_PalettedBlockArray_getMaxPaletteSize, ZEND_ACC_PUBLIC)
7975
ZEND_ME(pocketmine_world_format_PalettedBlockArray, getBitsPerBlock, arginfo_class_pocketmine_world_format_PalettedBlockArray_getBitsPerBlock, ZEND_ACC_PUBLIC)

tests/PalettedBlockArray/palette-bytes.phpt

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/PalettedBlockArray/palette-from-data-bytes.phpt

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)