> No. The coercion system has a cache of its own. It does keep strong > references to the coercion maps. They don't have homsets, though. The > problem might be that you keep extracting coercion maps from the coercion > system: > > if all(R.has_coerce_map_from(S) for R in self._rings): > return CallableConvertMap(S, self, > lambda x: self.element_class(tuple(R.coerce_map_from(S)(x) > for R in self._rings), parent=self > ), > parent_as_first_arg=False) > > If you rewrite this as: > > if all(R.has_coerce_map_from(S) for R in self._rings): > coerce_maps = tuple( R.coerce_map_from(S) for R in > self._rings) > return CallableConvertMap(S, self, > lambda x: self.element_class( tuple(m(x) for m in > coerce_maps), parent=self), > parent_as_first_arg=False) > > is a lot faster. Now the lifetime of the homsets is tied (via storing them > in a closure) to the lifetime of the coercion map that needs them, and the > coercion system itself can cache that map for as long as needed. There are > other ways of writing this that are probably even faster. I would imagine > that > > lambda x: self.element_class( tuple( R(x) for R in > self._rings),parent=self) > > would be even faster (and possibly more memory efficient, because now > everything is left to the coercion system instead of trying to extract > information from the coercion system in the form of maps). >
You are right. The first rewrite results in equally fast code on both versions of Sage, which in turn is slightly faster than my old version. The second rewrite indeed speeds things up even more. The question, now, is why the coercion system is invoked at all: I'm doing arithmetic within my own ring. The culprit, I guess, is the expression 1/self in element.pyx. Does a ring keep track of its own multiplicative identity element? -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.