Opened 6 years ago

Last modified 5 years ago

#14592 new enhancement

Use lazy strings for error messages in coercion

Reported by: SimonKing Owned by: tbd
Priority: major Milestone: sage-6.4
Component: performance Keywords: lazy error message coercion
Cc: nbruin Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: #14585 Stopgaps:

Description

At #14585, I propose some code that makes sage.misc.lazy_string.lazy_string faster. I think it makes sense to use it for error messages in the coercion model. This could accelerate stuff, because some error messages rely on the string representation of a parent (which is costly), but will never be printed (because the error is usually caught).

The purpose of this ticket is to use #14585 in the coercion code.

Change History (7)

comment:1 Changed 6 years ago by SimonKing

I don't know of striking benchmarks yet. But it could be that the following taken from #11900 may be useful:

sage: %time D = J0(46).endomorphism_ring().discriminant()
sage: W.<z> = CyclotomicField(13)
sage: %time M = Matrix(W, 2, 3, [10^30*(1-z)^13, 1, 2, 3, 4, z]).echelon_form()
sage: %time L = EllipticCurve('960d1').prove_BSD()

and

sage: def test(E):
....:     for p in prime_range(10000):
....:         if p != 389:
....:             G = E.change_ring(GF(p)).abelian_group()
....:             
sage: E = EllipticCurve('389a')
sage: %time test(E)

In all these examples,

        raise TypeError, "no common canonical parent for objects with parents: '%s' and '%s'"%(xp, yp)

at the end of the cpdef method sage.structure.coerce.CoercionModel_cache_maps.canonical_coercion is called a couple of hundred times. I don't know how noticeable a speed-up would be, though.

Last edited 6 years ago by SimonKing (previous) (diff)

comment:2 follow-up: Changed 6 years ago by SimonKing

I see no gain in the (not posted) patches I tried. Even though certain type errors are raised a couple of hundred times, it is still not noticeable if we save "a couple of hundred times 1ms" in a computation that takes a couple of seconds.

Do you have a striking example, in which computing a string representation for creating an error message really hurts?

comment:3 in reply to: ↑ 2 Changed 6 years ago by nbruin

Replying to SimonKing:

Do you have a striking example, in which computing a string representation for creating an error message really hurts?

If there's an error message that includes the string representation of a matrix you'd see a huge difference (but it looks like changing the matrix string rep is the more appropriate solution) if you manage to produce and catch that error in the inner loop.

Otherwise, we should just keep this ticket on the backburner and know it's an optimization we can use if it matters.

There are pros and cons besides performance to this approach:

  • With a lazy string message you have access to the objects involved in the error itself, even if they were produced in cython code (for python code you could probably get at them with a debugger and the stackframe). Sometimes picking apart the objects themselves helps in understanding where they're coming from (or you can look them up in memory using gc.get_referrers).
  • More complicated objects might be kept alive with lazy strings, because they hold references. Exceptions tend to be rather transient, though, and the frame stack (which would also still be alive in the try...except) would probably hold references as well.

It seemed like such an obvious speedup for python's "easier to ask forgiveness" programming style that produces and catches exceptions left and right, but if it doesn't seem to have a significant impact, we probably shouldn't bother.

comment:4 Changed 6 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:5 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:6 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:7 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4
Note: See TracTickets for help on using tickets.