active_attr icon indicating copy to clipboard operation
active_attr copied to clipboard

Memoize instances typcasters in typecaster map

Open film42 opened this issue 9 years ago • 2 comments

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.

film42 avatar Feb 08 '16 20:02 film42

+1 I was just about to open a PR with the exact same change!

fxfilmxf avatar Jul 07 '16 18:07 fxfilmxf

🤖 Beep boop! Just sending a ping ❤️😄

film42 avatar May 15 '17 14:05 film42