Skip to content

Commit b0b1fd1

Browse files
authored
PHPC-2146: Refactor typemap struct and BSON encoding/decoding of zvals (#1369)
* Avoid re-parsing bson when converting to zval A lot of occurrences start out with a bson_t*, then call bson_get_data on it to pass it to php_phongo_bson_to_zval_ex. This however creates a new bson reader and creates a new bson_t* from the given data. This is unnecessary and we should pass the original bson_t* in these instances. * Refactor typemap struct * Extract object initialisation from document visitor * Refactor document and array visitors * Fix wrong signature in array visitor declaration * Rename ce member for consistency * Add exception thrown comment * Fix clang-format
1 parent ae18d5c commit b0b1fd1

21 files changed

+308
-273
lines changed

src/BSON/Javascript.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ HashTable* php_phongo_javascript_get_properties_hash(phongo_compat_object_handle
9494
php_phongo_bson_state state;
9595

9696
PHONGO_BSON_INIT_STATE(state);
97-
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
97+
if (!php_phongo_bson_to_zval_ex(intern->scope, &state)) {
9898
zval_ptr_dtor(&state.zchild);
9999
goto failure;
100100
}
@@ -194,7 +194,7 @@ static PHP_METHOD(MongoDB_BSON_Javascript, getScope)
194194

195195
PHONGO_BSON_INIT_STATE(state);
196196

197-
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
197+
if (!php_phongo_bson_to_zval_ex(intern->scope, &state)) {
198198
zval_ptr_dtor(&state.zchild);
199199
return;
200200
}
@@ -220,7 +220,7 @@ static PHP_METHOD(MongoDB_BSON_Javascript, jsonSerialize)
220220
php_phongo_bson_state state;
221221

222222
PHONGO_BSON_INIT_STATE(state);
223-
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
223+
if (!php_phongo_bson_to_zval_ex(intern->scope, &state)) {
224224
zval_ptr_dtor(&state.zchild);
225225
return;
226226
}
@@ -244,7 +244,7 @@ static PHP_METHOD(MongoDB_BSON_Javascript, serialize)
244244
PHONGO_PARSE_PARAMETERS_NONE();
245245

246246
if (intern->scope && intern->scope->len) {
247-
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
247+
if (!php_phongo_bson_to_zval_ex(intern->scope, &state)) {
248248
zval_ptr_dtor(&state.zchild);
249249
return;
250250
}

src/BSON/functions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ PHP_FUNCTION(toPHP)
6565
return;
6666
}
6767

68-
if (!php_phongo_bson_to_zval_ex((const unsigned char*) data, data_len, &state)) {
68+
if (!php_phongo_bson_data_to_zval_ex((const unsigned char*) data, data_len, &state)) {
6969
zval_ptr_dtor(&state.zchild);
7070
php_phongo_bson_typemap_dtor(&state.map);
7171
RETURN_NULL();

src/MongoDB/BulkWrite.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ static void php_phongo_bulkwrite_extract_id(bson_t* doc, zval** return_value)
4040
php_phongo_bson_state state;
4141

4242
PHONGO_BSON_INIT_STATE(state);
43-
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
43+
state.map.root.type = PHONGO_TYPEMAP_NATIVE_ARRAY;
4444

45-
if (!php_phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &state)) {
45+
if (!php_phongo_bson_to_zval_ex(doc, &state)) {
4646
goto cleanup;
4747
}
4848

@@ -632,7 +632,7 @@ static HashTable* php_phongo_bulkwrite_get_debug_info(phongo_compat_object_handl
632632
if (intern->let) {
633633
zval zv;
634634

635-
if (!php_phongo_bson_to_zval(bson_get_data(intern->let), intern->let->len, &zv)) {
635+
if (!php_phongo_bson_to_zval(intern->let, &zv)) {
636636
zval_ptr_dtor(&zv);
637637
goto done;
638638
}

src/MongoDB/ClientEncryption.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ static void phongo_clientencryption_create_datakey(php_phongo_clientencryption_t
4040
static void phongo_clientencryption_encrypt(php_phongo_clientencryption_t* clientencryption, zval* zvalue, zval* zciphertext, zval* options);
4141
static void phongo_clientencryption_decrypt(php_phongo_clientencryption_t* clientencryption, zval* zciphertext, zval* zvalue);
4242

43-
#define RETVAL_BSON_T(reply) \
44-
do { \
45-
php_phongo_bson_state state; \
46-
PHONGO_BSON_INIT_STATE(state); \
47-
if (!php_phongo_bson_to_zval_ex(bson_get_data(&(reply)), (reply).len, &state)) { \
48-
zval_ptr_dtor(&state.zchild); \
49-
goto cleanup; \
50-
} \
51-
RETVAL_ZVAL(&state.zchild, 0, 1); \
43+
#define RETVAL_BSON_T(reply) \
44+
do { \
45+
php_phongo_bson_state state; \
46+
PHONGO_BSON_INIT_STATE(state); \
47+
if (!php_phongo_bson_to_zval_ex(&(reply), &state)) { \
48+
zval_ptr_dtor(&state.zchild); \
49+
goto cleanup; \
50+
} \
51+
RETVAL_ZVAL(&state.zchild, 0, 1); \
5252
} while (0)
5353

5454
#define RETVAL_OPTIONAL_BSON_T(reply) \

src/MongoDB/Command.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static HashTable* php_phongo_command_get_debug_info(phongo_compat_object_handler
156156
if (intern->bson) {
157157
zval zv;
158158

159-
if (!php_phongo_bson_to_zval(bson_get_data(intern->bson), intern->bson->len, &zv)) {
159+
if (!php_phongo_bson_to_zval(intern->bson, &zv)) {
160160
zval_ptr_dtor(&zv);
161161
goto done;
162162
}

src/MongoDB/Cursor.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static PHP_METHOD(MongoDB_Driver_Cursor, setTypeMap)
9090
if (restore_current_element && mongoc_cursor_current(intern->cursor)) {
9191
const bson_t* doc = mongoc_cursor_current(intern->cursor);
9292

93-
if (!php_phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &intern->visitor_data)) {
93+
if (!php_phongo_bson_to_zval_ex(doc, &intern->visitor_data)) {
9494
php_phongo_cursor_free_current(intern);
9595
}
9696
}
@@ -223,7 +223,7 @@ static PHP_METHOD(MongoDB_Driver_Cursor, next)
223223
}
224224

225225
if (mongoc_cursor_next(intern->cursor, &doc)) {
226-
if (!php_phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &intern->visitor_data)) {
226+
if (!php_phongo_bson_to_zval_ex(doc, &intern->visitor_data)) {
227227
/* Free invalid result, but don't return as we want to free the
228228
* session if the intern is exhausted. */
229229
php_phongo_cursor_free_current(intern);
@@ -278,7 +278,7 @@ static PHP_METHOD(MongoDB_Driver_Cursor, rewind)
278278
doc = mongoc_cursor_current(intern->cursor);
279279

280280
if (doc) {
281-
if (!php_phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &intern->visitor_data)) {
281+
if (!php_phongo_bson_to_zval_ex(doc, &intern->visitor_data)) {
282282
/* Free invalid result, but don't return as we want to free the
283283
* session if the intern is exhausted. */
284284
php_phongo_cursor_free_current(intern);

src/MongoDB/Monitoring/CommandFailedEvent.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getReply)
9494

9595
PHONGO_PARSE_PARAMETERS_NONE();
9696

97-
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &state)) {
97+
if (!php_phongo_bson_to_zval_ex(intern->reply, &state)) {
9898
zval_ptr_dtor(&state.zchild);
9999
return;
100100
}
@@ -223,7 +223,7 @@ static HashTable* php_phongo_commandfailedevent_get_debug_info(phongo_compat_obj
223223
sprintf(operation_id, "%" PRIu64, intern->operation_id);
224224
ADD_ASSOC_STRING(&retval, "operationId", operation_id);
225225

226-
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &reply_state)) {
226+
if (!php_phongo_bson_to_zval_ex(intern->reply, &reply_state)) {
227227
zval_ptr_dtor(&reply_state.zchild);
228228
goto done;
229229
}

src/MongoDB/Monitoring/CommandStartedEvent.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_CommandStartedEvent, getCommand)
4444

4545
PHONGO_PARSE_PARAMETERS_NONE();
4646

47-
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->command), intern->command->len, &state)) {
47+
if (!php_phongo_bson_to_zval_ex(intern->command, &state)) {
4848
zval_ptr_dtor(&state.zchild);
4949
return;
5050
}
@@ -202,7 +202,7 @@ static HashTable* php_phongo_commandstartedevent_get_debug_info(phongo_compat_ob
202202
*is_temp = 1;
203203
array_init_size(&retval, 6);
204204

205-
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->command), intern->command->len, &command_state)) {
205+
if (!php_phongo_bson_to_zval_ex(intern->command, &command_state)) {
206206
zval_ptr_dtor(&command_state.zchild);
207207
goto done;
208208
}

src/MongoDB/Monitoring/CommandSucceededEvent.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_CommandSucceededEvent, getReply)
8282

8383
PHONGO_PARSE_PARAMETERS_NONE();
8484

85-
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &state)) {
85+
if (!php_phongo_bson_to_zval_ex(intern->reply, &state)) {
8686
zval_ptr_dtor(&state.zchild);
8787
return;
8888
}
@@ -204,7 +204,7 @@ static HashTable* php_phongo_commandsucceededevent_get_debug_info(phongo_compat_
204204
sprintf(operation_id, "%" PRIu64, intern->operation_id);
205205
ADD_ASSOC_STRING(&retval, "operationId", operation_id);
206206

207-
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &reply_state)) {
207+
if (!php_phongo_bson_to_zval_ex(intern->reply, &reply_state)) {
208208
zval_ptr_dtor(&reply_state.zchild);
209209
goto done;
210210
}

src/MongoDB/Monitoring/ServerHeartbeatSucceededEvent.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_ServerHeartbeatSucceededEvent, getRe
6666

6767
PHONGO_PARSE_PARAMETERS_NONE();
6868

69-
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &state)) {
69+
if (!php_phongo_bson_to_zval_ex(intern->reply, &state)) {
7070
zval_ptr_dtor(&state.zchild);
7171
return;
7272
}
@@ -126,7 +126,7 @@ static HashTable* php_phongo_serverheartbeatsucceededevent_get_debug_info(phongo
126126
ADD_ASSOC_LONG_EX(&retval, "port", intern->host.port);
127127
ADD_ASSOC_BOOL_EX(&retval, "awaited", intern->awaited);
128128

129-
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &reply_state)) {
129+
if (!php_phongo_bson_to_zval_ex(intern->reply, &reply_state)) {
130130
zval_ptr_dtor(&reply_state.zchild);
131131
goto done;
132132
}

0 commit comments

Comments
 (0)