Skip to content

Conversation

@phsym
Copy link
Contributor

@phsym phsym commented Nov 8, 2025

This pull request enhances the optimizer to support constant propagation for map access expressions, enabling compile-time evaluation of property and index lookups when the map is a constant known at compile/optimization time. It also adds comprehensive tests to ensure these optimizations work as intended.

Optimizer enhancements for constant map access:

  • Added support for optimizing property access (var.property) when var is a constant map, allowing direct replacement with the corresponding value at compile time.
  • Added support for optimizing index access (var[property]) when var is a constant map and property is a string or a constant string variable, enabling constant folding for these expressions.
  • Property access on constant map literals #{a: 42}[property] when property is a constant string variable is also optimized at compile time, removing the need for runtime lookup.

Testing improvements:

  • Added a new test, test_optimizer_const_map, which verifies that constant map optimizations work for both property and index access patterns, including dynamic key access.

@phsym phsym changed the title Enhance optimizer to support constant map expressions Enhance optimizer to support constant maps Nov 8, 2025
@phsym phsym force-pushed the const_map_optimize branch 4 times, most recently from d64c3c2 to 29ab96d Compare November 8, 2025 16:54
@schungx
Copy link
Collaborator

schungx commented Nov 9, 2025

Just a minor nitpick in the comments.

And is_some_and is not supported in MSRV so better avoid it.

Great work. This will be very useful in cases where constant maps are used as a form of enum or avoiding the creation of a large number of constant variables.

My own scripts have tons of those so this will be very helpful.

@phsym phsym force-pushed the const_map_optimize branch from 29ab96d to 4e9d836 Compare November 9, 2025 09:18
@phsym phsym force-pushed the const_map_optimize branch from 4e9d836 to d5afecd Compare November 9, 2025 09:26
@phsym phsym requested a review from schungx November 9, 2025 09:26
@phsym
Copy link
Contributor Author

phsym commented Nov 9, 2025

@schungx thank you for your quick feedback. I made the updates based on your comments

@schungx schungx merged commit 6548c2b into rhaiscript:main Nov 9, 2025
41 of 42 checks passed
@phsym phsym deleted the const_map_optimize branch November 9, 2025 15:27
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