Skip to content

Commit

Permalink
Merge pull request #14 from codetiger/main
Browse files Browse the repository at this point in the history
Performance optimizations and code cleanup
  • Loading branch information
codetiger authored Dec 18, 2024
2 parents f18b84e + 204f01b commit d8f7678
Show file tree
Hide file tree
Showing 18 changed files with 620 additions and 432 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ use datalogic_rs::JsonLogic;
use serde_json::json;

fn main() {
let engine = JsonLogic::new();

// Complex discount rule example
let discount_rule = json!({
"if": [
Expand All @@ -56,7 +54,7 @@ fn main() {
});

let rule = Rule::from_value(&discount_rule).unwrap();
let price = engine.apply(&rule, &data).unwrap();
let price = JsonLogic::apply(&rule, &data).unwrap();
assert_eq!(price, json!(90.0)); // 25% off 120
}
```
Expand Down
4 changes: 1 addition & 3 deletions benches/jsonlogic_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ lazy_static::lazy_static! {
}

fn bench_apply_all_rules(c: &mut Criterion) {
let engine = JsonLogic::new();

let mut group = c.benchmark_group("jsonlogic_rules");
group.sampling_mode(criterion::SamplingMode::Linear);
group.sample_size(50);

group.bench_function("apply_all_rules", |b| {
b.iter(|| {
for (rule, data, expected) in TEST_CASES.iter() {
if let Ok(result) = engine.apply(rule, data) {
if let Ok(result) = JsonLogic::apply(rule, data) {
assert_eq!(result, *expected);
}
}
Expand Down
3 changes: 1 addition & 2 deletions examples/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ lazy_static::lazy_static! {
}

fn main() {
let engine = JsonLogic::new();
let iterations = 1e5 as u32;

let start = Instant::now();

for _ in 0..iterations {
for (rule, data, expected) in TEST_CASES.iter() {
if let Ok(result) = engine.apply(rule, data) {
if let Ok(result) = JsonLogic::apply(rule, data) {
assert_eq!(result, *expected);
}
}
Expand Down
8 changes: 3 additions & 5 deletions examples/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use datalogic_rs::*;
use serde_json::json;

fn main() {
let engine = JsonLogic::new();

// Example 1: Dynamic Pricing Rules
let pricing_logic = json!({
"if": [
Expand Down Expand Up @@ -112,14 +110,14 @@ fn main() {

// Test the rules
let pricing_rule = Rule::from_value(&pricing_logic).unwrap();
let final_price = engine.apply(&pricing_rule, &pricing_data).unwrap();
let final_price = JsonLogic::apply(&pricing_rule, &pricing_data).unwrap();
println!("Final price after discounts: ${}", final_price);

let loan_eligibility_rule = Rule::from_value(&loan_eligibility).unwrap();
let is_eligible = engine.apply(&loan_eligibility_rule, &loan_data).unwrap();
let is_eligible = JsonLogic::apply(&loan_eligibility_rule, &loan_data).unwrap();
println!("Loan application approved: {}", is_eligible);

let fraud_check_rule = Rule::from_value(&fraud_check).unwrap();
let is_fraudulent = engine.apply(&fraud_check_rule, &transaction_data).unwrap();
let is_fraudulent = JsonLogic::apply(&fraud_check_rule, &transaction_data).unwrap();
println!("Transaction flagged as fraudulent: {}", is_fraudulent);
}
6 changes: 2 additions & 4 deletions examples/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,18 @@ fn main() {
]
});

let json_logic = JsonLogic::new();

// Convert logic to Rule
let rule = Rule::from_value(&logic).unwrap();

// Warm-up
for _ in 0..100 {
json_logic.apply(&rule, &data).unwrap();
JsonLogic::apply(&rule, &data).unwrap();
}

// Measure performance of the new apply_rule function
let start = Instant::now();
for _ in 0..100000 {
json_logic.apply(&rule, &data).unwrap();
JsonLogic::apply(&rule, &data).unwrap();
}
let duration_apply_rule = start.elapsed();

Expand Down
4 changes: 1 addition & 3 deletions examples/specific.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use datalogic_rs::*;
use serde_json::json;

fn main() {
let engine = JsonLogic::new();

// Test both escaped dot and regular dot navigation
let test_cases = vec![
// Test 1: Escaped dot should look up exact key
Expand All @@ -22,7 +20,7 @@ fn main() {

for (logic, data, expected) in test_cases {
let rule = Rule::from_value(&logic).unwrap();
let result = engine.apply(&rule, &data).unwrap();
let result = JsonLogic::apply(&rule, &data).unwrap();
println!("Rule: {}", logic);
println!("Result: {}", result);
println!("Expected: {}", expected);
Expand Down
14 changes: 5 additions & 9 deletions examples/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ use datalogic_rs::*;
use serde_json::json;

fn main() {
let engine = JsonLogic::new();

let logic = json!({
"==": [
{"var": "user.age"},
21
]
});
"map": [[5, 6, 7], { "+": [{ "var": [] }, 1] }]
});

let data = json!({
"user": {
Expand All @@ -19,6 +14,7 @@ fn main() {
});

let rule = Rule::from_value(&logic).unwrap();
let result = engine.apply(&rule, &data).unwrap();
println!("Is user 21? {}", result); // Prints: Is user 21? true
println!("Rule: {:?}", rule);
let result = JsonLogic::apply(&rule, &data).unwrap();
println!("Result: {}", result);
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl JsonLogic {
Self {}
}

pub fn apply(&self, rule: &Rule, data: &Value) -> JsonLogicResult {
pub fn apply(rule: &Rule, data: &Value) -> JsonLogicResult {
rule.apply(data)
}
}
Loading

0 comments on commit d8f7678

Please sign in to comment.