mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 03:44:55 +00:00 
			
		
		
		
	SF bug #1085744: Performance issues with PySequence_Tuple()
* Added missing error checks. * Fixed O(n**2) growth pattern. Modeled after lists to achieve linear amortized resizing. Improves construction of "tuple(it)" when "it" is large and does not have a __len__ method. Other cases are unaffected.
This commit is contained in:
		
							parent
							
								
									8a6a59c58b
								
							
						
					
					
						commit
						4d01259fb2
					
				
					 1 changed files with 14 additions and 4 deletions
				
			
		| 
						 | 
					@ -1427,10 +1427,20 @@ PySequence_Tuple(PyObject *v)
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if (j >= n) {
 | 
							if (j >= n) {
 | 
				
			||||||
			if (n < 500)
 | 
								int oldn = n;
 | 
				
			||||||
				n += 10;
 | 
								/* The over-allocation strategy can grow a bit faster
 | 
				
			||||||
			else
 | 
								   than for lists because unlike lists the 
 | 
				
			||||||
				n += 100;
 | 
								   over-allocation isn't permanent -- we reclaim
 | 
				
			||||||
 | 
								   the excess before the end of this routine.
 | 
				
			||||||
 | 
								   So, grow by ten and then add 25%.
 | 
				
			||||||
 | 
								*/
 | 
				
			||||||
 | 
								n += 10;
 | 
				
			||||||
 | 
								n += n >> 2;
 | 
				
			||||||
 | 
								if (n < oldn) {
 | 
				
			||||||
 | 
									/* Check for overflow */
 | 
				
			||||||
 | 
									PyErr_NoMemory();
 | 
				
			||||||
 | 
									goto Fail; 
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			if (_PyTuple_Resize(&result, n) != 0) {
 | 
								if (_PyTuple_Resize(&result, n) != 0) {
 | 
				
			||||||
				Py_DECREF(item);
 | 
									Py_DECREF(item);
 | 
				
			||||||
				goto Fail;
 | 
									goto Fail;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue