We say that a subring R 0 of a ring R is semi-invariant if R 0 is the ring of invariants in R under some set of ring endomorphisms of some ring containing R. We show that R 0 is semi-invariant if and only if there is a ring S ⊇ R and a set X ⊆ S such that R 0 = Cent R (X) := {r ∈ R : xr = rx ∀x ∈ X}; in particular, centralizers of subsets of R are semi-invariant subrings.We prove that a semi-invariant subring R 0 of a semiprimary (resp. right perfect) ring R is again semiprimary (resp. right perfect) and satisfies Jac(R 0 ) n ⊆ Jac(R) for some n ∈ N. This result holds for other families of semiperfect rings, but the semiperfect analogue fails in general. In order to overcome this, we specialize to Hausdorff linearly topologized rings and consider topologically semi-invariant subrings. This enables us to show that any topologically semiinvariant subring (e.g. a centralizer of a subset) of a semiperfect ring that can be endowed with a "good" topology (e.g. an inverse limit of semiprimary rings) is semiperfect.Among the applications: (1) The center of a semiprimary (resp. right perfect) ring is semiprimary (resp. right perfect). (2) If M is a finitely presented module over a "good" semiperfect ring (e.g. an inverse limit of semiprimary rings), then End(M ) is semiperfect, hence M has a Krull-Schmidt decomposition. (This generalizes results of Bjork and Rowen; see [5], [24], [23].) (3) If ρ is a representation of a monoid or a ring over a module with a "good" semiperfect endomorphism ring (in the sense of (2)), then ρ has a Krull-Schmidt decomposition. (4) If S is a "good" commutative semiperfect ring and R is an S-algebra that is f.p. as an S-module, then R is semiperfect. (5) Let R ⊆ S be rings and let M be a right S-module. If End(M R ) is semiprimary (resp. right perfect), then End(M S ) is semiprimary (resp. right perfect).1 4 This proposition is a refinement of [13, Pr. 2.7], which asserts that under the same assumptions End(M S ) is a rationally closed subring of End(M R ).