Commit 5a4fcb7
committed
Skip redundant re-validation of constructor kwargs
HasTraits.__init__ validated every trait kwarg twice: once via setattr in the
fast loop, then again via _cross_validate + set_trait. The second pass is only
needed for traits that actually have a cross-validator (@Validate handler or a
deprecated _<name>_validate method); for the common case with none it re-ran
validate() on an already-validated value.
The second loop now guards on the same condition _cross_validate itself uses
(key in self._trait_validators or a _<name>_validate attribute exists). For
traits without a cross-validator it records the already-stored (possibly
coerced) value for the notification instead of re-validating, so notification
payloads are byte-for-byte identical.
Measured (Python 3.11): instantiation with kwargs and no cross-validators
~1.2-1.4x faster.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VaKDJ3fpGf7anQeYeBsJbk1 parent 6941a69 commit 5a4fcb7
1 file changed
Lines changed: 11 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1394 | 1394 | | |
1395 | 1395 | | |
1396 | 1396 | | |
1397 | | - | |
1398 | | - | |
1399 | | - | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
1400 | 1408 | | |
1401 | 1409 | | |
1402 | 1410 | | |
| |||
0 commit comments