Memoize instances typcasters in typecaster map
All of the default typecasters are stateless, so allocating a new instance for each field becomes extremely costly, especially for larger models. To reduce this, we can memoize the TYPECASTER_MAP.
This should be a backwards compatible change with a speed boost. The only people who might have trouble are those who modified the typecaster map. This is a frozen constant, though, so nobody should be touching this.
This is currently about 10% quicker on a model with 93 fields. The benchmark creates a model and then calls to_json. I also ran the benchmark on a smaller model with only 12 String fields and saw a 1-2% improvement.
+1 I was just about to open a PR with the exact same change!
🤖 Beep boop! Just sending a ping ❤️😄