Skip to content

ext/openssl: various arrays optimisations. #18377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions ext/openssl/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1063,17 +1063,20 @@ PHP_FUNCTION(openssl_x509_parse)
add_assoc_string(return_value, "signatureTypeSN", (char*)OBJ_nid2sn(sig_nid));
add_assoc_string(return_value, "signatureTypeLN", (char*)OBJ_nid2ln(sig_nid));
add_assoc_long(return_value, "signatureTypeNID", sig_nid);
array_init(&subitem);

int x509_count = X509_PURPOSE_get_count();
array_init_size(&subitem, x509_count);

/* NOTE: the purposes are added as integer keys - the keys match up to the X509_PURPOSE_SSL_XXX defines
in x509v3.h */
for (i = 0; i < X509_PURPOSE_get_count(); i++) {
for (i = 0; i < x509_count; i++) {
int id, purpset;
char * pname;
X509_PURPOSE * purp;
zval subsub;

array_init(&subsub);
array_init_size(&subsub, 3);
zend_hash_real_init_packed(Z_ARRVAL(subsub));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this gives visible performance improvement, why don't you create array_init variants (e.g. array_init_packed and array_init_size_packed)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just might. Not huge differences by any mean but I ran those

image


purp = X509_PURPOSE_get0(i);
id = X509_PURPOSE_get_id(purp);
Expand Down Expand Up @@ -4138,15 +4141,16 @@ PHP_FUNCTION(openssl_seal)
ZEND_TRY_ASSIGN_REF_NEW_STR(sealdata, zend_string_init((char*)buf, len1 + len2, 0));
efree(buf);

ekeys = zend_try_array_init(ekeys);
ekeys = zend_try_array_init_size(ekeys, nkeys);
if (!ekeys) {
EVP_CIPHER_CTX_free(ctx);
goto clean_exit;
}
zend_hash_real_init_packed(Z_ARRVAL_P(ekeys));

for (i=0; i<nkeys; i++) {
eks[i][eksl[i]] = '\0';
add_next_index_stringl(ekeys, (const char*)eks[i], eksl[i]);
add_index_stringl(ekeys, i, (const char*)eks[i], eksl[i]);
efree(eks[i]);
eks[i] = NULL;
}
Expand Down Expand Up @@ -4292,6 +4296,7 @@ PHP_FUNCTION(openssl_get_curve_names)
}

array_init(return_value);
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
for (i = 0; i < len; i++) {
sname = OBJ_nid2sn(curves[i].nid);
if (sname != NULL) {
Expand Down
7 changes: 4 additions & 3 deletions ext/openssl/openssl_backend_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ void php_openssl_add_assoc_name_entry(zval * val, char * key, X509_NAME * name,
if (Z_TYPE_P(data) == IS_ARRAY) {
add_next_index_stringl(data, (const char *)to_add, to_add_len);
} else if (Z_TYPE_P(data) == IS_STRING) {
array_init(&tmp);
add_next_index_str(&tmp, zend_string_copy(Z_STR_P(data)));
add_next_index_stringl(&tmp, (const char *)to_add, to_add_len);
array_init_size(&tmp, 2);
zend_hash_real_init_packed(Z_ARRVAL(tmp));
add_index_str(&tmp, 0, zend_string_copy(Z_STR_P(data)));
add_index_stringl(&tmp, 1, (const char *)to_add, to_add_len);
zend_hash_str_update(Z_ARRVAL(subitem), sname, strlen(sname), &tmp);
}
} else {
Expand Down
7 changes: 4 additions & 3 deletions ext/openssl/xp_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1752,16 +1752,17 @@ static int php_openssl_capture_peer_certs(php_stream *stream,
chain = SSL_get_peer_cert_chain(sslsock->ssl_handle);

if (chain && sk_X509_num(chain) > 0) {
int i;
array_init(&arr);
int i, num_chains = sk_X509_num(chain);
array_init_size(&arr, num_chains);
zend_hash_real_init_packed(Z_ARRVAL(arr));

for (i = 0; i < sk_X509_num(chain); i++) {
X509 *mycert = X509_dup(sk_X509_value(chain, i));

object_init_ex(&zcert, php_openssl_certificate_ce);
cert_object = Z_OPENSSL_CERTIFICATE_P(&zcert);
cert_object->x509 = mycert;
add_next_index_zval(&arr, &zcert);
add_index_zval(&arr, i, &zcert);
}

} else {
Expand Down
Loading