﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35408	Optimize post-migrate permission creation	Adam Johnson	Adam Johnson	"I have often seen `django.contrib.auth.management.create_permissions()` take a significant amount of time in test run profiles. It can be optimized by batching more of its operations, including making `ContentTypeManager.get_for_models()` use batch creation.

For a comparison, I profiled 1518 of Django’s tests in modules called “models”:

{{{
$ python -m cProfile -o profile runtests.py --parallel 1 *model*

$ python -m pstats profile <<< 'sort cumtime
stats 10000' | less
}}}

Before optimization stats:

* Total 11,938,857 function calls taking 5.349 seconds.
* 88 calls to `create_permissions()` take 456ms, ~8.5% of the total time.

After optimization stats:

* Total 11,359,071 function calls taking 5.035 seconds.
* 88 calls to `create_permissions()` now take 239ms, ~4.7% of the toal time.
* 217ms and 579,786 function calls saved.

Optimization is limited because the `post_migrate` signal runs once per migrated app config, so there’s no chance to bulk create *all* content types and permissions at once. If we introduced a new “all migrated apps” signal, that could reduce runtime further by batching all creation."	Cleanup/optimization	closed	contrib.auth	dev	Normal	fixed			Ready for checkin	1	0	0	0	0	0
