mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 03:44:55 +00:00 
			
		
		
		
	Remove explicit empty tuple reuse in cpickle.
PyTuple_New(0) always returns the same empty tuple from its free list anyway, so we are not saving much here. Plus, the code where this was used is on uncommon run paths.
This commit is contained in:
		
							parent
							
								
									b13e6bcbd8
								
							
						
					
					
						commit
						6bf41e54a4
					
				
					 1 changed files with 6 additions and 12 deletions
				
			
		| 
						 | 
					@ -166,9 +166,6 @@ typedef struct {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* codecs.encode, used for saving bytes in older protocols */
 | 
					    /* codecs.encode, used for saving bytes in older protocols */
 | 
				
			||||||
    PyObject *codecs_encode;
 | 
					    PyObject *codecs_encode;
 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* As the name says, an empty tuple. */
 | 
					 | 
				
			||||||
    PyObject *empty_tuple;
 | 
					 | 
				
			||||||
} PickleState;
 | 
					} PickleState;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Forward declaration of the _pickle module definition. */
 | 
					/* Forward declaration of the _pickle module definition. */
 | 
				
			||||||
| 
						 | 
					@ -205,7 +202,6 @@ _Pickle_ClearState(PickleState *st)
 | 
				
			||||||
    Py_CLEAR(st->name_mapping_3to2);
 | 
					    Py_CLEAR(st->name_mapping_3to2);
 | 
				
			||||||
    Py_CLEAR(st->import_mapping_3to2);
 | 
					    Py_CLEAR(st->import_mapping_3to2);
 | 
				
			||||||
    Py_CLEAR(st->codecs_encode);
 | 
					    Py_CLEAR(st->codecs_encode);
 | 
				
			||||||
    Py_CLEAR(st->empty_tuple);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Initialize the given pickle module state. */
 | 
					/* Initialize the given pickle module state. */
 | 
				
			||||||
| 
						 | 
					@ -321,10 +317,6 @@ _Pickle_InitState(PickleState *st)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    Py_CLEAR(codecs);
 | 
					    Py_CLEAR(codecs);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    st->empty_tuple = PyTuple_New(0);
 | 
					 | 
				
			||||||
    if (st->empty_tuple == NULL)
 | 
					 | 
				
			||||||
        goto error;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  error:
 | 
					  error:
 | 
				
			||||||
| 
						 | 
					@ -1137,8 +1129,9 @@ _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
 | 
				
			||||||
        return -1;
 | 
					        return -1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (n == READ_WHOLE_LINE) {
 | 
					    if (n == READ_WHOLE_LINE) {
 | 
				
			||||||
        PickleState *st = _Pickle_GetGlobalState();
 | 
					        PyObject *empty_tuple = PyTuple_New(0);
 | 
				
			||||||
        data = PyObject_Call(self->readline, st->empty_tuple, NULL);
 | 
					        data = PyObject_Call(self->readline, empty_tuple, NULL);
 | 
				
			||||||
 | 
					        Py_DECREF(empty_tuple);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else {
 | 
					    else {
 | 
				
			||||||
        PyObject *len = PyLong_FromSsize_t(n);
 | 
					        PyObject *len = PyLong_FromSsize_t(n);
 | 
				
			||||||
| 
						 | 
					@ -3774,8 +3767,10 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
 | 
				
			||||||
            /* Check for a __reduce__ method. */
 | 
					            /* Check for a __reduce__ method. */
 | 
				
			||||||
            reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
 | 
					            reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
 | 
				
			||||||
            if (reduce_func != NULL) {
 | 
					            if (reduce_func != NULL) {
 | 
				
			||||||
                reduce_value = PyObject_Call(reduce_func, st->empty_tuple,
 | 
					                PyObject *empty_tuple = PyTuple_New(0);
 | 
				
			||||||
 | 
					                reduce_value = PyObject_Call(reduce_func, empty_tuple,
 | 
				
			||||||
                                             NULL);
 | 
					                                             NULL);
 | 
				
			||||||
 | 
					                Py_DECREF(empty_tuple);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else {
 | 
					            else {
 | 
				
			||||||
                PyErr_Format(st->PicklingError,
 | 
					                PyErr_Format(st->PicklingError,
 | 
				
			||||||
| 
						 | 
					@ -7412,7 +7407,6 @@ pickle_traverse(PyObject *m, visitproc visit, void *arg)
 | 
				
			||||||
    Py_VISIT(st->name_mapping_3to2);
 | 
					    Py_VISIT(st->name_mapping_3to2);
 | 
				
			||||||
    Py_VISIT(st->import_mapping_3to2);
 | 
					    Py_VISIT(st->import_mapping_3to2);
 | 
				
			||||||
    Py_VISIT(st->codecs_encode);
 | 
					    Py_VISIT(st->codecs_encode);
 | 
				
			||||||
    Py_VISIT(st->empty_tuple);
 | 
					 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue