Skip to content
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

ext/pgsql: using ZPP api for calls. #14099

Merged
merged 1 commit into from
May 15, 2024
Merged

ext/pgsql: using ZPP api for calls. #14099

merged 1 commit into from
May 15, 2024

Conversation

devnexen
Copy link
Member

@devnexen devnexen commented May 1, 2024

No description provided.

@devnexen devnexen force-pushed the pgsql_zpp branch 2 times, most recently from 851d6c6 to e3f3972 Compare May 1, 2024 22:05
@devnexen devnexen marked this pull request as ready for review May 1, 2024 23:27
@SakiTakamachi
Copy link
Member

I did some testing to improve BCMath performance, but in most cases using UNEXPECTED slowed it down. It's probably preventing the compiler from optimizing.

On the other hand, there were some areas where it was quite effective. In my opinion, when adding EXPECTED or UNEXPECTED, it is better to measure the performance, even if it is rough.

@devnexen
Copy link
Member Author

devnexen commented May 2, 2024

To be honest it was mostly negligible most of the time (why I kept as separate commit too), I may not keep it.

if (ZSTR_LEN(tmp) > 0 && *(query + ZSTR_LEN(tmp) - 1) != '\n') {
strlcat(query, "\n", ZSTR_LEN(tmp) + 2);
zquery = zend_string_alloc(ZSTR_LEN(tmp) + 2, false);
if (UNEXPECTED(!zquery)) {
Copy link
Member

Choose a reason for hiding this comment

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

Note that zquery cannot be NULL because zend_string_alloc cannot fail.

@devnexen
Copy link
Member Author

ping :)

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

I only made a quick check, I might've missed some things, but you can start from this.
Please fix these mistakes. There's a lot of repeated mistakes so after fixing what I found please double check everything.

}
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
Copy link
Member

Choose a reason for hiding this comment

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

This no longer accepts pg_close(null) anymore, but the signature is function pg_close(?PgSql\Connection $connection = null): true {} so it should be allowed.
The same remark holds in other places too.

}
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STRING(query, query_len)
Z_PARAM_ZVAL(pv_param_arr)
Copy link
Member

Choose a reason for hiding this comment

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

the old parameter parsing using "a" for array, but you now use zval?

ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
Z_PARAM_STRING(query, query_len)
Z_PARAM_ZVAL(pv_param_arr)
Copy link
Member

Choose a reason for hiding this comment

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

Same here

}
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STRING(stmtname, stmtname_len)
Z_PARAM_ZVAL(pv_param_arr)
Copy link
Member

Choose a reason for hiding this comment

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

Same here

ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
Z_PARAM_STRING(stmtname, stmtname_len)
Z_PARAM_ZVAL(pv_param_arr)
Copy link
Member

Choose a reason for hiding this comment

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

Same here

ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
Z_PARAM_STR(table)
Z_PARAM_ZVAL(ids)
Copy link
Member

Choose a reason for hiding this comment

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

Array

}
ZEND_PARSE_PARAMETERS_START(2, 5)
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
Z_PARAM_STR(table)
Copy link
Member

Choose a reason for hiding this comment

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

Path

Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
Z_PARAM_STR(table)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(ids)
Copy link
Member

Choose a reason for hiding this comment

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

Array

}
ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
Z_PARAM_STR(table_name)
Copy link
Member

Choose a reason for hiding this comment

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

Path

}
ZEND_PARSE_PARAMETERS_START(3, 5)
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
Z_PARAM_STR(table_name)
Copy link
Member

Choose a reason for hiding this comment

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

Path

@nielsdos
Copy link
Member

@devnexen Note that we have Z_PARAM_OBJ_OF_CLASS_OR_NULL ;) No need to use the EX thing

@devnexen
Copy link
Member Author

@devnexen Note that we have Z_PARAM_OBJ_OF_CLASS_OR_NULL ;) No need to use the EX thing

You mean Z_PARAM_OBJECT_OF_CLASS_OR_NULL ?

@nielsdos
Copy link
Member

@devnexen Note that we have Z_PARAM_OBJ_OF_CLASS_OR_NULL ;) No need to use the EX thing

You mean Z_PARAM_OBJECT_OF_CLASS_OR_NULL ?

Oops, yes

@devnexen devnexen force-pushed the pgsql_zpp branch 2 times, most recently from 71692f6 to 4309af0 Compare May 13, 2024 22:00
@devnexen devnexen changed the title [WIP] ext/pgsql: using ZPP api for calls. ext/pgsql: using ZPP api for calls. May 13, 2024
Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

Note that if I say "The same remark holds in other places too." it means that there are many other places where it applies, but not necessarily all of the places.
Also a nit: the old code was already using ZPP, ZPP just stands for "Zend Parameter Parsing", the PR title should be "using fast ZPP API for calls"

RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(pgsql_link, pgsql_link_ce)
Copy link
Member

Choose a reason for hiding this comment

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

This wasn't nullable before, but you made it nullable now. Looking at the stub, it should not be nullable.

Copy link
Member Author

Choose a reason for hiding this comment

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

Cool, at least I learnt a lot of new possible param I did not know existed (esp. path).

RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(pgsql_link, pgsql_link_ce)
Copy link
Member

Choose a reason for hiding this comment

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

Should not be nullable.

RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(pgsql_link, pgsql_link_ce)
Copy link
Member

Choose a reason for hiding this comment

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

Not nullable

RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(pgsql_link, pgsql_link_ce)
Copy link
Member

Choose a reason for hiding this comment

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

Should not be nullable

RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(pgsql_link, pgsql_link_ce)
Copy link
Member

Choose a reason for hiding this comment

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

Should not be nullable.

RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(4, 5)
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(pgsql_link, pgsql_link_ce)
Copy link
Member

Choose a reason for hiding this comment

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

Should not be nullable

RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(pgsql_link, pgsql_link_ce)
Copy link
Member

Choose a reason for hiding this comment

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

Should not be nullable

RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(2, 5)
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(pgsql_link, pgsql_link_ce)
Copy link
Member

Choose a reason for hiding this comment

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

Should not be nullable

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

Only one remark remaining

if (ZSTR_LEN(tmp) > 0 && *(query + ZSTR_LEN(tmp) - 1) != '\n') {
strlcat(query, "\n", ZSTR_LEN(tmp) + 2);
zend_string *zquery = zend_string_alloc(ZSTR_LEN(tmp) + 2, false);
memcpy(ZSTR_VAL(zquery), ZSTR_VAL(tmp), ZSTR_LEN(tmp) + 2);
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong, no?
You're copying 1 byte outside of the tmp string. It should be + 1 instead of + 2.

Copy link
Member Author

Choose a reason for hiding this comment

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

true

}
if (PQputCopyData(pgsql, query, (int)strlen(query)) != 1) {
efree(query);
if (PQputCopyData(pgsql, ZSTR_VAL(tmp), ZSTR_LEN(tmp)) != 1) {
Copy link
Member

Choose a reason for hiding this comment

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

The use of ZSTR_LEN(tmp) is wrong, because you need to include the '\n' if you added it in the if-check above.

}
if (PQputCopyData(pgsql, query, (int)strlen(query)) != 1) {
efree(query);
if (PQputCopyData(pgsql, ZSTR_VAL(zquery), ZSTR_LEN(tmp) > 0 ? ZSTR_LEN(tmp) + 1 : 0) != 1) {
Copy link
Member

Choose a reason for hiding this comment

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

I see what you do here, and I'm not sure it matters that we can send over an extra NUL terminator.
However, wouldn't it be better to simply adjust the length of the string zquery if it didn't have \n at the end initially?
I also realize now that you only need one additional byte instead of 2 for zend_string_alloc.

}
if (PQputCopyData(pgsql, query, (int)strlen(query)) != 1) {
efree(query);
if (PQputCopyData(pgsql, ZSTR_VAL(zquery), ZSTR_LEN(tmp) > 0 ? ZSTR_LEN(tmp) + 1 : 0) != 1) {
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this what we want?

Suggested change
if (PQputCopyData(pgsql, ZSTR_VAL(zquery), ZSTR_LEN(tmp) > 0 ? ZSTR_LEN(tmp) + 1 : 0) != 1) {
if (PQputCopyData(pgsql, ZSTR_VAL(zquery), ZSTR_LEN(zquery)) != 1) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes that s the point of the length changes :) I forgot to update before going to sleep it seems.

Copy link
Member

Choose a reason for hiding this comment

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

Relatable :P

@devnexen devnexen merged commit 0218af8 into php:master May 15, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants