Skip to content

bpo-41902: Add fast path for long_div if the divisor is one#22480

Closed
corona10 wants to merge 2 commits into
python:masterfrom
corona10:bpo-41902-long
Closed

bpo-41902: Add fast path for long_div if the divisor is one#22480
corona10 wants to merge 2 commits into
python:masterfrom
corona10:bpo-41902-long

Conversation

@corona10

@corona10 corona10 commented Oct 1, 2020

Copy link
Copy Markdown
Member

@vstinner

vstinner commented Oct 1, 2020

Copy link
Copy Markdown
Member

Can you please run benchmarks? For example, benchmark:

  • x//1
  • x//2
  • x//y where y is made of more than 1 digit

For multiple values of x:

  • single digit
  • two digits

Comment thread Lib/test/test_long.py
@support.cpython_only
def test_division_with_one(self):
for n in range(10000000):
self.assertEqual(n, n // 1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suggest to test assertIs().

Comment thread Lib/test/test_long.py

@support.cpython_only
def test_division_with_one(self):
for n in range(10000000):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well, I don't think that it's worth it to test 10000000 numbers. Just test a bunch of various numbers made of a single digit. Like:

(-MASK, -10, -5, 0, 1, 2, 42, MASK)

Comment thread Lib/test/test_long.py


@support.cpython_only
def test_division_with_one(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you test assertIs(), please add a comment mentioning bpo-41902 and explain that it's a micro-optimization which returns x for x//1 if x is an int made of a single digit.

Comment thread Lib/test/test_long.py
def test_division_with_one(self):
for n in range(10000000):
self.assertEqual(n, n // 1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Test also that assertEqual(type(x//1), int) where x is an int subclass.

Comment thread Objects/longobject.c
long_div(PyObject *a, PyObject *b)
{
PyLongObject *div;
if (b == _PyLong_One && PyLong_CheckExact(a)) {

@pablogsal pablogsal Oct 1, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This branch may impact the normal path, we certainly should benchmark this with PGO and cpu isolation.

@vstinner

vstinner commented Oct 1, 2020

Copy link
Copy Markdown
Member

@mdickinson doesn't like this approach, see the discussion on the issue: https://bugs.python.org/issue41902#msg377773

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.

5 participants