Skip to content

Lazy type-as-constructor get! defaults (closes #202) - #215

Draft
rodrigomha wants to merge 1 commit into
mainfrom
rh/lazy_get_defaults
Draft

Lazy type-as-constructor get! defaults (closes #202)#215
rodrigomha wants to merge 1 commit into
mainfrom
rh/lazy_get_defaults

Conversation

@rodrigomha

Copy link
Copy Markdown
Contributor

Closes #202.

The 3-arg get!(dict, key, default) evaluates default eagerly on every call, allocating a
throwaway collection even on the common hit path (the key already present). Per odow's suggestion
in #202, a type is itself a zero-arg constructor, so get!(Set{…}, dict, key) builds the default
lazily - only on a miss - with no anonymous closure. Simpler than both the eager form and the
get!(() -> …, dict, key) closure form.

Sites converted

All are insert-then-mutate (a following push! / _add_to_branch_map!), so a fresh instance per
miss is required - the type-constructor form provides exactly that (never a shared const).

  • network_models/network_reductions.jl - reduction tracker wiring + constraint map/submap (the hottest, per-arc/per-type)
  • ac_transmission_models/branch_constructor.jl - inter-area branch typed-dict assembly (per branch)
  • operation/template_validation.jl - bus regulators, outage types, per-type validation (per element)

Pure mechanical swaps (11 lines, 11±). Compile-verified locally; leaving as draft for CI to run
the full suite (local cross-package dev env currently can't resolve main's pins - unrelated to this change).

🤖 Generated with Claude Code

https://claude.ai/code/session_01W1bssU5YnY8eZWQD9apDBM

…oses #202)

The 3-arg get!(dict, key, default) evaluates `default` eagerly on every call,
allocating a throwaway collection even on the common hit path. Per odow's
suggestion in #202, a type is itself a zero-arg constructor, so get!(Set{...}, dict, key)
/ get!(Dict{...}, dict, key) / get!(Vector{String}, dict, key) build the default
lazily (only on a miss) with no anonymous closure - simpler than get!(() -> ..., ...).

Sites (all insert-then-mutate, so a fresh instance per miss is required - the
type-constructor form provides exactly that):
- network_models/network_reductions.jl: reduction tracker + constraint map wiring
- ac_transmission_models/branch_constructor.jl: inter-area branch typed-dict assembly
- operation/template_validation.jl: bus regulators, outage types, per-type validation
@github-actions

Copy link
Copy Markdown

Performance Results

Version Precompile Time
Main 2.925744972
This Branch 3.055562725
Version Build Time
Main-Build Time Precompile 86.409492456
Main-Build Time Postcompile 1.327196526
This Branch-Build Time Precompile 90.969732135
This Branch-Build Time Postcompile 1.60098835
Version Solve Time
Main-Solve Time Precompile 414.381860839
Main-Solve Time Postcompile 379.147266033
This Branch-Solve Time Precompile 2094.995438905
This Branch-Solve Time Postcompile 2066.969596988

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use lazy get!(f, dict, key) to avoid eager-default allocations on the hit path

1 participant