Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ def test_division(self):
self.check_division(710031681576388032, 26769404391308)
self.check_division(1933622614268221, 30212853348836)


@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.

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)

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().


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.

def test_karatsuba(self):
digits = list(range(1, 5)) + list(range(KARATSUBA_CUTOFF,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add fast path for long_div if the divisor is one. Patch by Dong-hee Na.
4 changes: 4 additions & 0 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3695,6 +3695,10 @@ static PyObject *
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.

Py_INCREF(a);
return a;
}

CHECK_BINOP(a, b);

Expand Down