Imprisonment via a criminal justice system seems to be just about the least effective way to treat mentally ill people, if your goal is to protect society from mentally ill people.
Even doing nothing at all would probably have better results.
Of course, if your goal is to be seen as tough on people who do bad things, without regard for what impact that has on society at large, then convicting mentally ill people in criminal courts and sending them to jail is a great plan.
Never mind that a properly designed and funded secure psychiatric program would be far more effective in protecting society, not least because incarceration would be indefinite and dependent on the mental state of the offender, rather than arbitrarily fixed at the term deemed reasonable for criminal punishment.
Why the US has a "justice" system designed by simpletons, when criminologists and psychiatrists have known for decades what works, and such a system is known to be less expensive as well as more protective of society at large, is utterly mystifying, but it's probably based in religion.
It doesn't matter who gets hurt; The victims of crime are worthy of thought only for the purpose of evoking an emotional response in favour of massively oversimplified policies that are demonstrably completely ineffective.
The US already incarcerates a ridiculous proportion of her citizens, and yet still the simpletons clamour for more and longer jail terms, as though that were the only possible response to, or even an effective and sensible response to, the continued existence of violent mentally ill people.