[issue46361] Small ints aren't always cached properly

2022-01-16 Thread Mark Dickinson


Change by Mark Dickinson :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46361] Small ints aren't always cached properly

2022-01-16 Thread Mark Dickinson


Mark Dickinson  added the comment:


New changeset 5cd9a162cd02a3d0f1b0a182d80feeb17439e84f by Brandt Bucher in 
branch 'main':
bpo-46361: Fix "small" `int` caching (GH-30583)
https://github.com/python/cpython/commit/5cd9a162cd02a3d0f1b0a182d80feeb17439e84f


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46361] Small ints aren't always cached properly

2022-01-13 Thread Brandt Bucher


Brandt Bucher  added the comment:

The attached PR doesn't seem to have any impact on Decimal performance 
(non-optimized, non-debug build on a fairly quiet laptop):

main:

Convert 262,000 Decimals to "small" ints: 31.7 ms +- 5.3 ms
Convert 256,000 Decimals to 1-digit ints: 29.9 ms +- 3.1 ms
Convert 256,000 Decimals to 2-digit ints: 30.4 ms +- 2.8 ms
Convert 256,000 Decimals to 3-digit ints: 31.2 ms +- 3.1 ms

patched:

Convert 262,000 Decimals to "small" ints: 30.9 ms +- 4.0 ms
Convert 256,000 Decimals to 1-digit ints: 29.5 ms +- 3.0 ms
Convert 256,000 Decimals to 1-digit ints: 30.5 ms +- 2.5 ms
Convert 256,000 Decimals to 1-digit ints: 31.0 ms +- 2.3 ms

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46361] Small ints aren't always cached properly

2022-01-13 Thread Brandt Bucher


Change by Brandt Bucher :


--
keywords: +patch
pull_requests: +28781
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/30583

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46361] Small ints aren't always cached properly

2022-01-13 Thread Mark Dickinson


Mark Dickinson  added the comment:

And there are some similar things going on in rangeobject.c.

https://github.com/python/cpython/blob/1de60155d5d01be2924e72fb68dd13d4fd00acd7/Objects/rangeobject.c#L598

if (r->step == _PyLong_GetOne()) {
return idx;
}

Again, technically "okay", since it's only a fast path and the slow path that 
follows will still do the right thing with a 1 that's not "the" 1, but it feels 
fragile.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46361] Small ints aren't always cached properly

2022-01-13 Thread Mark Dickinson


Mark Dickinson  added the comment:

Hmm. This sort of thing is a little dodgy, though (despite the comment that 
it's "okay"): 

https://github.com/python/cpython/blob/1de60155d5d01be2924e72fb68dd13d4fd00acd7/Modules/mathmodule.c#L939

PyObject *zero = _PyLong_GetZero();  // borrowed ref
for (i = 1; i < nargs; i++) {
/* --- 8< --- snipped code */
if (res == zero) {
/* Fast path: just check arguments.
   It is okay to use identity comparison here. */
Py_DECREF(x);
continue;
}
/* --- 8< --- snipped code*/
}

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46361] Small ints aren't always cached properly

2022-01-13 Thread Mark Dickinson


Mark Dickinson  added the comment:

I don't *think* we currently rely on small integers being cached anywhere in 
the implementation (and neither do we guarantee anywhere in the docs that small 
integers will be cached), so as far as I can tell these omissions shouldn't 
lead to user-visible bugs.

I agree that these cases should be fixed, though.

--
nosy: +mark.dickinson

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46361] Small ints aren't always cached properly

2022-01-12 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46361] Small ints aren't always cached properly

2022-01-12 Thread Brandt Bucher


New submission from Brandt Bucher :

To my surprise, it seems that it's possible to create "small" integers that 
should live in _PyLong_SMALL_INTS, but don't. Here are two examples I've found:

>>> import decimal
>>> i = int(decimal.Decimal(42))  # Modules/_decimal/_decimal.c:dec_as_long
>>> i
42
>>> i is 42
:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False
>>> i = int.from_bytes(bytes([42]))  # 
>>> Objects/longobject.c:_PyLong_FromByteArray
>>> i
42
>>> i is 42
:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False

I'm not entirely sure if this is "allowed" or not, but in any case it seems 
beneficial to reach into the small ints here (provided it doesn't hurt 
performance, of course).

I'm testing out simple fixes for both of these now.

--
assignee: brandtbucher
components: Interpreter Core
messages: 410437
nosy: brandtbucher
priority: normal
severity: normal
status: open
title: Small ints aren't always cached properly
type: behavior
versions: Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com