Commit a0679b5
committed
Cache metadata-filtered class_traits()/traits() results per class
Filtering traits by metadata (e.g. class_traits(config=True)) was recomputed
from scratch on every call — the single largest cost of Application startup
(~25-30%), invoked ~45x per startup for results that are static per class
(from Application._classes_with_config_traits, KVArgParseConfigLoader.
_add_arguments, and each Configurable._load_config).
class_traits()/traits() now delegate to a shared classmethod that memoizes the
filtered dict per class and returns a .copy(), preserving the existing
"fresh dict" contract — the cached dict never escapes by reference. cls._traits
is frozen after class creation (add_traits() builds a new class rather than
mutating), so the only way a filtered result can change is a post-hoc metadata
mutation via tag()/set_metadata(); those bump a module-level generation counter
and stale cache entries (older than the current generation) are recomputed.
Only constant (non-callable, hashable) filters are cached; callable predicates
stay on the uncached path.
Measured (Python 3.11): class_traits(config=True) ~8.3us -> ~1.3us per call.
Note: the cache is invalidated by the supported post-construction metadata APIs
(tag()/set_metadata()). Mutating trait.metadata as a raw dict after the class
has already been queried is not reflected until the next generation bump; this
pattern is not used in traitlets and is vanishingly rare in practice.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VaKDJ3fpGf7anQeYeBsJbk1 parent f50040a commit a0679b5
2 files changed
Lines changed: 92 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
937 | 937 | | |
938 | 938 | | |
939 | 939 | | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
940 | 966 | | |
941 | 967 | | |
942 | 968 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
62 | 68 | | |
63 | 69 | | |
64 | 70 | | |
| |||
871 | 877 | | |
872 | 878 | | |
873 | 879 | | |
| 880 | + | |
874 | 881 | | |
875 | 882 | | |
876 | 883 | | |
| |||
894 | 901 | | |
895 | 902 | | |
896 | 903 | | |
| 904 | + | |
897 | 905 | | |
898 | 906 | | |
899 | 907 | | |
| |||
1030 | 1038 | | |
1031 | 1039 | | |
1032 | 1040 | | |
| 1041 | + | |
| 1042 | + | |
1033 | 1043 | | |
1034 | 1044 | | |
1035 | 1045 | | |
| |||
1339 | 1349 | | |
1340 | 1350 | | |
1341 | 1351 | | |
| 1352 | + | |
1342 | 1353 | | |
1343 | 1354 | | |
1344 | 1355 | | |
| |||
1802 | 1813 | | |
1803 | 1814 | | |
1804 | 1815 | | |
1805 | | - | |
1806 | | - | |
1807 | 1816 | | |
1808 | | - | |
| 1817 | + | |
| 1818 | + | |
| 1819 | + | |
| 1820 | + | |
| 1821 | + | |
1809 | 1822 | | |
1810 | | - | |
1811 | | - | |
1812 | | - | |
1813 | | - | |
1814 | | - | |
| 1823 | + | |
| 1824 | + | |
| 1825 | + | |
| 1826 | + | |
| 1827 | + | |
| 1828 | + | |
| 1829 | + | |
| 1830 | + | |
| 1831 | + | |
| 1832 | + | |
| 1833 | + | |
| 1834 | + | |
| 1835 | + | |
| 1836 | + | |
| 1837 | + | |
| 1838 | + | |
| 1839 | + | |
| 1840 | + | |
| 1841 | + | |
| 1842 | + | |
| 1843 | + | |
| 1844 | + | |
| 1845 | + | |
| 1846 | + | |
| 1847 | + | |
| 1848 | + | |
| 1849 | + | |
| 1850 | + | |
| 1851 | + | |
| 1852 | + | |
| 1853 | + | |
| 1854 | + | |
| 1855 | + | |
| 1856 | + | |
| 1857 | + | |
| 1858 | + | |
| 1859 | + | |
| 1860 | + | |
| 1861 | + | |
| 1862 | + | |
| 1863 | + | |
| 1864 | + | |
| 1865 | + | |
| 1866 | + | |
1815 | 1867 | | |
1816 | 1868 | | |
1817 | 1869 | | |
1818 | 1870 | | |
1819 | 1871 | | |
| 1872 | + | |
| 1873 | + | |
1820 | 1874 | | |
1821 | 1875 | | |
1822 | 1876 | | |
| |||
1935 | 1989 | | |
1936 | 1990 | | |
1937 | 1991 | | |
1938 | | - | |
1939 | | - | |
1940 | 1992 | | |
1941 | | - | |
1942 | | - | |
1943 | | - | |
1944 | | - | |
1945 | | - | |
1946 | | - | |
1947 | | - | |
1948 | | - | |
1949 | | - | |
1950 | | - | |
1951 | | - | |
| 1993 | + | |
1952 | 1994 | | |
1953 | | - | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
1954 | 1998 | | |
1955 | 1999 | | |
1956 | 2000 | | |
| |||
0 commit comments