#optimization #proc-macro #expression #rust

macro macro_optim

A Rust procedural macro crate for optimizing and simplifying various expressions at compile time

1 stable release

1.0.0 Sep 14, 2024

#394 in Procedural macros

Apache-2.0

7KB
54 lines

macro_optim

macro_optim is a Rust procedural macro crate designed to optimize mathematical expressions with procedural macros, used for heavy mathematical computations, like rendering. It can handle addition and multiplication of integer and floating-point literals by evaluating them and replacing them with the result. It optimizes the code at compile time.

Features

  • Compile-time Expression Optimization: Optimize integer and floating-point addition and multiplication.
  • Arithmetic Evaluation: Automatically replace constant expressions with their evaluated results.

Installation

Add macro_optim to your Cargo.toml:

[dependencies]
macro_optim = "1.0.0"  # Replace with the appropriate version

Usage

To use the macro_optim attribute macro, annotate your code with #[macro_optim]. This macro will evaluate and replace constant expressions in the annotated item.

Example

use macro_optim::macro_optim;

#[macro_optim]
fn optimized_expr() -> i32 {
    let result = 2 + 3 * 4; // This will be optimized to 14
    result
}

fn main() {
    println!("{}", optimized_expr()); // Prints 14
}

In the example above, the expression 2 + 3 * 4 will be evaluated to 14 at compile time.

Limitations

  • Currently supports only addition and multiplication.
  • Supports only integer and floating-point literals.
  • Complex expressions or non-literal values are not optimized.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any enhancements or bug fixes.

License

This crate is licensed under the Apache 2.0 License. See the LICENSE file for more details.

Dependencies

~210–650KB
~15K SLoC