Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange slowdown on str() after handled FLINT exception #39224

Open
2 tasks done
user202729 opened this issue Dec 29, 2024 · 1 comment
Open
2 tasks done

Strange slowdown on str() after handled FLINT exception #39224

user202729 opened this issue Dec 29, 2024 · 1 comment
Labels

Comments

@user202729
Copy link
Contributor

user202729 commented Dec 29, 2024

sage: R.<x> = Zmod(4)[]
sage: f = x^2 + 3 * x + 1
sage: g = x^2 + x + 1
sage: %time str(f)
CPU times: user 2.34 ms, sys: 0 ns, total: 2.34 ms
Wall time: 2.37 ms
'x^2 + 3*x + 1'
sage: f.gcd(g)
Flint exception (Impossible inverse):
    Cannot invert modulo 2*2
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File nmod_poly_linkage.pxi:599, in sage.rings.polynomial.polynomial_zmod_flint.celement_gcd()

RuntimeError: Aborted

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
Cell In[5], line 1
----> 1 f.gcd(g)

File element.pyx:4743, in sage.structure.element.coerce_binop.new_method()

File polynomial_template.pxi:390, in sage.rings.polynomial.polynomial_zmod_flint.Polynomial_template.gcd()

File nmod_poly_linkage.pxi:605, in sage.rings.polynomial.polynomial_zmod_flint.celement_gcd()

RuntimeError: FLINT gcd calculation failed
sage: %time str(f)
CPU times: user 1.33 s, sys: 9.42 ms, total: 1.33 s
Wall time: 1.34 s
'x^2 + 3*x + 1'

Apparently it becomes fast again after accessing a nonexistent attribute

sage: %time f[0]
CPU times: user 304 ms, sys: 75 μs, total: 304 ms
Wall time: 305 ms
1
sage: %time f.xxx
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File <timed eval>:1

File element.pyx:495, in sage.structure.element.Element.__getattr__()

File element.pyx:508, in sage.structure.element.Element.getattr_from_category()

File getattr.pyx:363, in sage.cpython.getattr.getattr_from_other_class()

AttributeError: 'sage.rings.polynomial.polynomial_zmod_flint.Polynomial_zmod_flint' object has no at
tribute 'xxx'
sage: %time f[0]
CPU times: user 78.8 ms, sys: 0 ns, total: 78.8 ms
Wall time: 79.2 ms
1

Environment

  • OS: Linux
  • Sage Version: latest

Checklist

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.
  • I have read the documentation and troubleshoot guide
@user202729 user202729 changed the title Strange slowdown on str() after handled flint exception Strange slowdown on str() after handled FLINT exception Dec 29, 2024
@user202729
Copy link
Contributor Author

user202729 commented Dec 30, 2024

By benchmarking result it looks like the issue lies in verify_exc_value which repeatedly calls garbage collection.

With some testing

%%cython
from cysignals.signals cimport cysigs
def get_cysigs(): return <long> cysigs.exc_value
def get_cysigs_refcount(): return cysigs.exc_value.ob_refcnt

the value remains nonzero. Also get_cysigs_refcount() returns 2.

Reading the docstring:

The implementation is based on reference counting: it checks whether the exception has been deleted. This means that it will break if the exception is stored somewhere.

Move to sagemath/cysignals#215 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant