From 23654dcd6fed6ed5b396ffaa18c62140d114e520 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Wed, 24 Apr 2024 19:44:31 -0700 Subject: [PATCH 1/2] src/sage/ext/memory_allocator.p*: Remove, deprecated in #31591 (2021) --- src/sage/ext/memory_allocator.pxd | 145 ------------------------ src/sage/ext/memory_allocator.pyx | 180 ------------------------------ 2 files changed, 325 deletions(-) delete mode 100644 src/sage/ext/memory_allocator.pxd delete mode 100644 src/sage/ext/memory_allocator.pyx diff --git a/src/sage/ext/memory_allocator.pxd b/src/sage/ext/memory_allocator.pxd deleted file mode 100644 index 5fc54a1cd27..00000000000 --- a/src/sage/ext/memory_allocator.pxd +++ /dev/null @@ -1,145 +0,0 @@ -cimport cython -from libc.stdint cimport uintptr_t - - -cdef extern from *: - int unlikely(int) nogil # defined by Cython - - -cdef inline void* align(void* ptr, size_t alignment) noexcept: - """ - Round up ``ptr`` to the nearest multiple of ``alignment``, which - must be a power of 2 - """ - cdef uintptr_t x = ptr - x = (x + alignment - 1) & ~(alignment - 1) - return x - - -@cython.final -cdef class MemoryAllocator: - cdef size_t n - cdef size_t size - cdef void** pointers - cdef void* static_pointers[16] # If n <= 16, store pointers here - - cdef void* malloc(self, size_t size) except? NULL - cdef void* calloc(self, size_t nmemb, size_t size) except? NULL - cdef void* allocarray(self, size_t nmemb, size_t size) except? NULL - cdef void* realloc(self, void* ptr, size_t size) except? NULL - cdef void* reallocarray(self, void* ptr, size_t nmemb, - size_t size) except? NULL - - cdef int resize(self, size_t new_size) except -1 - cdef void** find_pointer(self, void* ptr) except NULL - - cdef inline int enlarge_if_needed(self) except -1: - r""" - Enlarge the list of pointers if needed such that there is at - least one free entry. - """ - if unlikely(self.n >= self.size): - return self.resize(self.size * 2) - - cdef inline void* aligned_malloc(self, size_t alignment, - size_t size) except? NULL: - r""" - Returns new aligned pointer. Stores it to be automatically freed later. - - Alignment must be a power of two. - - .. NOTE:: - - If you want to allocate multiple (small) aligned arrays with the - same alignment and really want to be memory efficient, you can - allocate one large aligned array instead. - - TESTS:: - - sage: cython( # needs sage.misc.cython - ....: ''' - ....: from sage.ext.memory_allocator cimport MemoryAllocator - ....: cdef MemoryAllocator mem = MemoryAllocator() - ....: cdef void* ptr - ....: for i in range(12): - ....: ptr = mem.aligned_malloc(2**i, 4048) - ....: assert ptr == ( ptr) & ~(2**i-1) - ....: ''') - doctest:...: DeprecationWarning: this class is deprecated; - use the class from the python package `memory_allocator` - See https://github.com/sagemath/sage/issues/31591 for details. - """ - cdef size_t extra = alignment - 1 - return align(self.malloc(size + extra), alignment) - - cdef inline void* aligned_calloc(self, size_t alignment, size_t nmemb, - size_t size) except? NULL: - r""" - Returns new aligned pointer. Stores it to be automatically freed later. - - Alignment must be a power of two. - - .. NOTE:: - - If you want to allocate multiple (small) aligned arrays with the - same alignment and really want to be memory efficient, you can - allocate one large aligned array instead. - - TESTS:: - - sage: cython( # needs sage.misc.cython - ....: ''' - ....: from sage.ext.memory_allocator cimport MemoryAllocator - ....: def foo(): - ....: cdef MemoryAllocator mem = MemoryAllocator() - ....: cdef void* ptr - ....: for i in range(12): - ....: ptr = mem.aligned_calloc(2**i, i, 2**i) - ....: assert ptr == ( ptr) & ~(2**i-1) - ....: ''') - sage: foo() # needs sage.misc.cython - doctest:...: DeprecationWarning: this class is deprecated; - use the class from the python package `memory_allocator` - See https://github.com/sagemath/sage/issues/31591 for details. - """ - # Find extra such that (nmemb + extra) * size >= nmemb * size + alignment - 1 - # ⇔ extra * size >= alignment - 1 - # ⇔ extra >= ceil( (alignment - 1) / size) - # ⇔ extra >= (alignment - 1 + size - 1) // size - cdef size_t extra = (alignment + size - 2) // size - return align(self.calloc(nmemb + extra, size), alignment) - - cdef inline void* aligned_allocarray(self, size_t alignment, size_t nmemb, - size_t size) except? NULL: - r""" - Returns new aligned pointer. Stores it to be automatically freed later. - - Alignment must be a power of two. - - .. NOTE:: - - If you want to allocate multiple (small) aligned arrays with the - same alignment and really want to be memory efficient, you can - allocate one large aligned array instead. - - TESTS:: - - sage: cython( # needs sage.misc.cython - ....: ''' - ....: from sage.ext.memory_allocator cimport MemoryAllocator - ....: def foo(): - ....: cdef MemoryAllocator mem = MemoryAllocator() - ....: cdef void* ptr - ....: for i in range(12): - ....: ptr = mem.aligned_allocarray(2**i, i, 2**i) - ....: assert ptr == ( ptr) & ~(2**i-1) - ....: ''') - sage: foo() # random # might raise deprecation warning # needs sage.misc.cython - sage: foo() # needs sage.misc.cython - """ - # Find extra such that (nmemb + extra) * size >= nmemb * size + alignment - 1 - # ⇔ extra * size >= alignment - 1 - # ⇔ extra >= ceil( (alignment - 1) / size) - # ⇔ extra >= (alignment - 1 + size - 1) // size - cdef size_t extra = (alignment + size - 2) // size - return align(self.allocarray(nmemb + extra, size), alignment) diff --git a/src/sage/ext/memory_allocator.pyx b/src/sage/ext/memory_allocator.pyx deleted file mode 100644 index 0c2814658e9..00000000000 --- a/src/sage/ext/memory_allocator.pyx +++ /dev/null @@ -1,180 +0,0 @@ -# sage.doctest: needs sage.misc.cython - -from cysignals.memory cimport * -from sage.misc.superseded import deprecation - - -cdef class MemoryAllocator: - r""" - An object for memory allocation, whose resources are freed upon - ``__dealloc__``. - - EXAMPLES:: - - sage: cython( - ....: ''' - ....: from sage.ext.memory_allocator cimport MemoryAllocator - ....: cdef MemoryAllocator mem = MemoryAllocator() - ....: cdef void* ptr - ....: for n in range(100): - ....: ptr = mem.malloc(n) - ....: mem.realloc(ptr, 2*n) - ....: mem.calloc(n, n) - ....: ptr = mem.allocarray(n, n) - ....: mem.reallocarray(ptr, n + 1, n) - ....: mem.aligned_malloc(32, (n//32 + 1)*32) - ....: mem.aligned_calloc(16, n, 16) - ....: mem.aligned_allocarray(8, n, 8) - ....: ''') - doctest:...: DeprecationWarning: this class is deprecated; use the class from the python package `memory_allocator` - See https://github.com/sagemath/sage/issues/31591 for details. - """ - def __cinit__(self): - """ - EXAMPLES:: - - sage: cython( - ....: ''' - ....: from sage.ext.memory_allocator cimport MemoryAllocator - ....: def foo(): - ....: cdef MemoryAllocator mem = MemoryAllocator.__new__(MemoryAllocator) - ....: mem.malloc(10000) - ....: print(mem.n) - ....: print(mem.size) - ....: ''') - sage: foo() - doctest:...: DeprecationWarning: this class is deprecated; use the class from the python package `memory_allocator` - See https://github.com/sagemath/sage/issues/31591 for details. - 1 - 16 - """ - deprecation(31591, "this class is deprecated; use the class from the python package `memory_allocator`") - self.n = 0 - self.size = 16 - self.pointers = self.static_pointers - - cdef int resize(self, size_t new_size) except -1: - r""" - Resize the list of pointers to contain ``new_size`` elements. - - It is required that ``new_size`` is at least ``self.n``, but - this condition is not checked. - """ - cdef size_t i - if self.pointers == self.static_pointers: - # Case 1: allocate pointers for the first time - self.pointers = check_allocarray(new_size, sizeof(void*)) - for i in range(self.n): - self.pointers[i] = self.static_pointers[i] - else: - # Case 2: resize pointers - self.pointers = check_reallocarray(self.pointers, new_size, sizeof(void*)) - self.size = new_size - - cdef void** find_pointer(self, void* ptr) except NULL: - r""" - Return the address in the list of stored pointers where ``ptr`` - is stored. If ``ptr`` is not found in the existing pointers and - ``ptr`` is not ``NULL``, then an exception is raised. If ``ptr`` - is ``NULL``, then we simply add ``NULL`` as an additional - pointer and return the address of that. - """ - cdef size_t i = 0 - for i in range(self.n): - if self.pointers[i] == ptr: - return &self.pointers[i] - if ptr != NULL: - raise ValueError("given pointer not found in MemoryAllocator") - self.enlarge_if_needed() - addr = &self.pointers[self.n] - self.n += 1 - return addr - - cdef void* malloc(self, size_t size) except? NULL: - r""" - Returns a new pointer and stores it to be automatically freed later. - """ - self.enlarge_if_needed() - cdef void* val = check_malloc(size) - self.pointers[self.n] = val - self.n += 1 - return val - - cdef void* calloc(self, size_t nmemb, size_t size) except? NULL: - r""" - Returns a new pointer and stores it to be automatically freed later. - """ - self.enlarge_if_needed() - cdef void* val = check_calloc(nmemb, size) - self.pointers[self.n] = val - self.n += 1 - return val - - cdef void* allocarray(self, size_t nmemb, size_t size) except? NULL: - r""" - Returns a new pointer and stores it to be automatically freed later. - """ - self.enlarge_if_needed() - cdef void* val = check_allocarray(nmemb, size) - self.pointers[self.n] = val - self.n += 1 - return val - - cdef void* realloc(self, void* ptr, size_t size) except? NULL: - r""" - Re-allocates `ptr` and automatically frees it later. - - TESTS:: - - sage: cython(''' - ....: from sage.ext.memory_allocator cimport MemoryAllocator - ....: def test_realloc_good(): - ....: cdef MemoryAllocator mem = MemoryAllocator() - ....: ptr = mem.malloc(20) - ....: mem.realloc(ptr, 21) - ....: def test_realloc_NULL(): - ....: cdef MemoryAllocator mem = MemoryAllocator() - ....: mem.realloc(NULL, 21) - ....: def test_realloc_bad(): - ....: cdef MemoryAllocator mem = MemoryAllocator() - ....: cdef MemoryAllocator mem2 = MemoryAllocator() - ....: ptr = mem.malloc(20) - ....: mem2.realloc(ptr, 21) - ....: ''') - sage: test_realloc_good() # random # might raise deprecation warning - sage: test_realloc_good() - sage: test_realloc_NULL() - sage: test_realloc_bad() - Traceback (most recent call last): - ... - ValueError: given pointer not found in MemoryAllocator - """ - cdef void** addr = self.find_pointer(ptr) - cdef void* val = check_realloc(ptr, size) - addr[0] = val - return val - - cdef void* reallocarray(self, void* ptr, size_t nmemb, - size_t size) except? NULL: - r""" - Re-allocates `ptr` and automatically frees it later. - """ - cdef void** addr = self.find_pointer(ptr) - cdef void* val = check_reallocarray(ptr, nmemb, size) - addr[0] = val - return val - - def __dealloc__(self): - r""" - Free the allocated resources - - EXAMPLES:: - - sage: from sage.ext.memory_allocator import MemoryAllocator - sage: _ = MemoryAllocator() - """ - cdef size_t i - for i in range(self.n): - sig_free(self.pointers[i]) - if self.pointers != self.static_pointers: - sig_free(self.pointers) From 0bce7844500c634950b2b79e82cb4acb2f65f700 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Wed, 24 Apr 2024 19:51:52 -0700 Subject: [PATCH 2/2] src/sage/ext/fast_eval.pyx: Remove keyword 'old' deprecated in #32234 (2021); document intention to deprecate 'fast_float' --- src/sage/ext/fast_eval.pyx | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/sage/ext/fast_eval.pyx b/src/sage/ext/fast_eval.pyx index 961b0825c8b..d9f1551495b 100644 --- a/src/sage/ext/fast_eval.pyx +++ b/src/sage/ext/fast_eval.pyx @@ -32,20 +32,20 @@ AUTHORS: from sage.ext.fast_callable import fast_callable, Wrapper -def fast_float(f, *vars, old=None, expect_one_var=False): +def fast_float(f, *vars, expect_one_var=False): """ - Tries to create a function that evaluates f quickly using - floating-point numbers, if possible. There are two implementations - of fast_float in Sage; by default we use the newer, which is - slightly faster on most tests. + Try to create a function that evaluates f quickly using + floating-point numbers, if possible. On failure, returns the input unchanged. + This is an alternative interface to :func:`sage.ext.fast_callable.fast_callable`. + :issue:`32268` proposes to deprecate this function. + INPUT: - ``f`` -- an expression - ``vars`` -- the names of the arguments - - ``old`` -- deprecated, do not use - ``expect_one_var`` -- don't give deprecation warning if ``vars`` is omitted, as long as expression has only one var @@ -68,12 +68,6 @@ def fast_float(f, *vars, old=None, expect_one_var=False): sage: f(1,2) 1.0 """ - if old: - raise ValueError("the old implementation of fast_float has been removed") - if old is not None: - from sage.misc.superseded import deprecation - deprecation(32234, "passing old=False to fast_float is deprecated") - if isinstance(f, (tuple, list)): return tuple([fast_float(x, *vars, expect_one_var=expect_one_var) for x in f])