Skip to content

Conversation

@corona10
Copy link
Member

@corona10 corona10 commented Oct 1, 2020

@vstinner
Copy link
Member

vstinner commented Oct 1, 2020

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

@support.cpython_only
def test_division_with_one(self):
for n in range(10000000):
self.assertEqual(n, n // 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 suggest to test assertIs().


@support.cpython_only
def test_division_with_one(self):
for n in range(10000000):
Copy link
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)



@support.cpython_only
def test_division_with_one(self):
Copy link
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.

def test_division_with_one(self):
for n in range(10000000):
self.assertEqual(n, n // 1)

Copy link
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.

long_div(PyObject *a, PyObject *b)
{
PyLongObject *div;
if (b == _PyLong_One && PyLong_CheckExact(a)) {
Copy link
Member

@pablogsal pablogsal Oct 1, 2020

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
Copy link
Member

vstinner commented Oct 1, 2020

@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