Opened 2 years ago

Last modified 2 years ago

#9680 enhancement new

— at twisted.logger.Logger creates too many new instances when declared within a classVersion 4

Reported by: L. Daniel Burr Owned by:
Priority: normal Milestone:
Component: core Keywords:
Cc: Branch:
Author:

Description (last modified by L. Daniel Burr)

Situation
In https://github.com/twisted/twisted/blob/trunk/src/twisted/logger/_logger.py#L100 a new instance of the Logger is created each and every time it is accessed as a descriptor. This can result in a lot of unnecessary object creation.

Target
When a Logger instance is created within a class declaration, all subsequent attribute accesses should return the same Logger instance.

Proposal
As a first step, stop creating a new instance of the Logger, and instead set the namespace, source, and observer attributes on the existing Logger instance. This will be sufficient for purposes of stemming the object creation tide, but will still be doing pointless work, in that the namespace, source, and observer never change.

As a second step, add an internal flag to the Logger class, which the __get__ method can check to see if it has already configured the Logger instance.

Lastly, adjust the private _loggerFor function to not rely on Logger.__get__ to create a Logger instance.

Change History (4)

comment:1 Changed 2 years ago by L. Daniel Burr

Description: modified (diff)

comment:2 Changed 2 years ago by L. Daniel Burr

Description: modified (diff)

comment:3 Changed 2 years ago by L. Daniel Burr

Description: modified (diff)

comment:4 Changed 2 years ago by L. Daniel Burr

Description: modified (diff)
Note: See TracTickets for help on using tickets.