8000 gh-111662: Update socket module to use AC for optimizing performance by wrongnull · Pull Request #111661 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-111662: Update socket module to use AC for optimizing performance #111661

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

Merged
merged 17 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Speed up the socket.htons function
14 changes: 11 additions & 3 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6396,11 +6396,19 @@ Convert a 32-bit integer from network to host byte order.");


static PyObject *
socket_htons(PyObject *self, PyObject *args)
Copy link
Member

Choose a reason for hiding this comment

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

/*[clinic input]
_socket.socket.htons
    x: int
    /

Convert a 16-bit unsigned integer from host to network byte order.
[clinic start generated code]*/

Use AC instead of writing manual parsing.
I got a similar performance gain in my local environment.

AS-IS: 5000000 loops, best of 5: 60.9 nsec per loop
TO-BE: 10000000 loops, best of 5: 23.3 nsec per loop

Copy link
Member

Choose a reason for hiding this comment

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

@wrongnull
You should take a look at the https://devguide.python.org/development-tools/clinic/ if you're not familiar with Argument Clinic :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm pretty sure a similar approach can be applied to all other functions with only one argument. If yes, I'll be happy to make such a commit in this branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@corona10 seems ready. clinic argument was also applied to other functions where it was beneficial

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if ValueError is more preferable if negative value is passed to ntohl and htonl functions

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if ValueError is more preferable if negative value is passed to ntohl and htonl functions

Please don't change the exception type; it will occur in user regression.
Can I know where the type of exception will be changed if AC is applied?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if ValueError is more preferable if negative value is passed to ntohl and htonl functions

Please don't change the exception type; it will occur in user regression. Can I know where the type of exception will be changed if AC is applied?

Exception type will change to ValueError from OverflowError in htonl and ntohl functions after applying usigned_long argument converter.

Copy link
Member

Choose a reason for hiding this comment

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

Exception type will change to ValueError from OverflowError in htonl and ntohl functions after applying usigned_long argument converter.

In that case, I would like to recommend not to touch it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exception type will change to ValueError from OverflowError in htonl and ntohl functions after applying usigned_long argument converter.

In that case, I would like to recommend not to touch it.

OK, I reverted this changes

socket_htons(PyObject *self, PyObject *arg)
{
int x;

if (!PyArg_ParseTuple(args, "i:htons", &x)) {
if (!PyLong_Check(arg)) {
return PyErr_Format(PyExc_TypeError,
"expected int, %s found",
Py_TYPE(arg)->tp_name);
}

x = PyLong_AsInt(arg);

if (x == -1 && PyErr_Occurred()) {
return NULL;
}
if (x < 0) {
Expand Down Expand Up @@ -7220,7 +7228,7 @@ static PyMethodDef socket_methods[] = {
{"ntohl", socket_ntohl,
METH_O, ntohl_doc},
{"htons", socket_htons,
METH_VARARGS, htons_doc},
METH_O, htons_doc},
{"htonl", socket_htonl,
METH_O, htonl_doc},
{"inet_aton", socket_inet_aton,
Expand Down
0