Skip to content

refactor(azure_policy): reduce AliasRegistry allocations via Rc sharing#725

Open
anakrish wants to merge 1 commit into
microsoft:mainfrom
anakrish:alias-registry-opt
Open

refactor(azure_policy): reduce AliasRegistry allocations via Rc sharing#725
anakrish wants to merge 1 commit into
microsoft:mainfrom
anakrish:alias-registry-opt

Conversation

@anakrish
Copy link
Copy Markdown
Collaborator

@anakrish anakrish commented May 15, 2026

Summary

Eliminates cloning two 73K-entry BTreeMaps on every Azure Policy compilation by sharing the AliasRegistry through a reference-counted pointer.

Changes

Compiler — replaces two owned map fields (alias_map, alias_modifiable) with Option<Rc<AliasRegistry>>. The three compile_*_with_aliases entry points now take Rc<AliasRegistry> instead of two separate maps:

// Before: two cloned maps (73K entries each)
compile_policy_definition_with_aliases(&defn, registry.alias_map(), registry.alias_modifiable_map())

// After: cheap Rc clone
compile_policy_definition_with_aliases(&defn, Rc::clone(&registry))

AliasRegistryalias_map() and alias_modifiable_map() now return &BTreeMap references instead of clones.

Safe indexing — replaces direct [..] slicing with .get() + error propagation in ingest_alias_entries and build_object_from_keys, satisfying the crate-wide deny(clippy::indexing_slicing) lint without #[allow] overrides.

Null-tolerant deserialization — custom deserializer for AliasEntry.paths handles the ~97% of real Azure catalog entries where az provider list emits "paths": null.

Scope

All changes are within the azure_policy feature gate. No impact on the core engine, FFI bindings, or other language backends.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Optimizes the Azure Policy AliasRegistry to reduce allocations on the load and compile paths, and hardens JSON deserialization against null arrays emitted by az provider list. The compiler now stores Rc<BTreeMap<...>> for alias data so repeated compilations share a single 73K-entry map instead of cloning, and ingest_alias_entries works on borrowed &str slices for short-name derivation.

Changes:

  • alias_map() / alias_modifiable_map() return cached Rc<BTreeMap>; compiler holds the Rc instead of an owned clone.
  • ingest_alias_entries derives short names via borrowed slices and safe .get()-based indexing, reducing per-alias allocations.
  • Adds deserialize_null_as_empty_vec for resourceTypes/aliases/paths/apiVersions, and replaces an #[allow(clippy::indexing_slicing)] block in build_object_from_keys with safe lookups.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/languages/azure_policy/aliases/mod.rs Adds Rc cache for alias maps, refactors short-name derivation, adds lowercase-key lookup helpers.
src/languages/azure_policy/aliases/types.rs Adds deserialize_null_as_empty_vec and applies it to four Vec fields.
src/languages/azure_policy/compiler/core.rs Compiler fields switched from BTreeMap to Rc<BTreeMap>.
src/languages/azure_policy/compiler/mod.rs compile_*_with_aliases signatures updated to take Rc<BTreeMap>.
src/languages/azure_policy/compiler/effects.rs Replaces unchecked indexing in build_object_from_keys with safe .get() + error propagation.
examples/regorus/azure_policy.rs Adjusts example to bind the returned Rc map before iterating.
tests/azure_policy/mod.rs Makes alias_registry mutable to satisfy new &mut self accessors.

Comment thread src/languages/azure_policy/aliases/mod.rs Outdated
Comment thread src/languages/azure_policy/aliases/mod.rs Outdated
@anakrish
Copy link
Copy Markdown
Collaborator Author

@copilot review this PR from different perspectives not considered before.

@anakrish anakrish force-pushed the alias-registry-opt branch 3 times, most recently from cf77d19 to 6f2fd04 Compare May 15, 2026 20:50
@anakrish anakrish changed the title refactor: optimize AliasRegistry with Rc-shared maps and reduced allocations refactor(azure_policy): reduce AliasRegistry allocations and simplify compiler interface May 15, 2026
@anakrish anakrish force-pushed the alias-registry-opt branch from 6f2fd04 to 70b2b38 Compare May 15, 2026 21:14
@anakrish anakrish changed the title refactor(azure_policy): reduce AliasRegistry allocations and simplify compiler interface refactor(azure_policy): reduce AliasRegistry allocations via Rc sharing May 15, 2026
@anakrish anakrish requested a review from Copilot May 15, 2026 21:30
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Comment thread src/languages/azure_policy/aliases/mod.rs
Comment thread src/languages/azure_policy/compiler/effects.rs Outdated
Comment thread src/languages/azure_policy/compiler/effects.rs
@anakrish anakrish force-pushed the alias-registry-opt branch 2 times, most recently from 91aa927 to fdae5db Compare May 15, 2026 22:53
@anakrish anakrish requested a review from Copilot May 15, 2026 23:08
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

Replace the compiler's two cloned BTreeMap fields (alias_map and
alias_modifiable) with a single Option<Rc<AliasRegistry>>. This
eliminates cloning two 73K-entry maps on every compilation by sharing
the registry through a reference-counted pointer.

Additional improvements:
- alias_map()/alias_modifiable_map() return &BTreeMap (zero-copy)
- Safe .get() indexing in ingest_alias_entries and build_object_from_keys
- Null-tolerant deserialization for AliasEntry.paths (~97% of real
  Azure catalog entries emit "paths": null)

All changes are within the azure_policy feature gate.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

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.

2 participants