﻿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
33124	Avoid accessing ConnectionsHandler.__getitem__ until it's strictly necessary.	Keryn Knight	Keryn Knight	"Follows on from #33025.

Most of the codebase does the correct thing as far as I can see, preferring `conn(ection) = connections[alias]` and holding a reference to that, when it's used multiple times. Most of the codebase also has touching `connections[alias]` as the last component in `if` branches etc too, so that short circuiting can occur to avoid touching the `Local`.

There's a few places where that isn't the case though. eg:
{{{
empty_strings_as_null = connections[db].features.interprets_empty_strings_as_nulls
...
if val is None or (val == '' and empty_strings_as_null):
}}}
or
{{{
can_ignore_conflicts = (
    connections[db].features.supports_ignore_conflicts and
    self.through._meta.auto_created is not False
)
}}}
or
{{{
compiler = connections[db].ops.compiler('SQLCompiler')(
    self.query, connections[db], db
)
}}}

Whilst I've not gone to the effort of benchmarking each of them, each of them has the possibility to slightly improve things, I think.

I'll be pushing a PR shortly with a bunch of commits that cover all of those I've found (outside of tests, where things are more gnarly and could be tackled separately, if there's an impetus to do so), so that CI can prove things work, and so that it can be decided which, if any of them, are worth it."	Cleanup/optimization	closed	Database layer (models, ORM)	dev	Normal	fixed			Ready for checkin	1	0	0	0	0	0
