Skip to content

Commit

Permalink
remove explain relevant code when feature has been disabled (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
GopherJ authored May 21, 2020
1 parent c2e45c5 commit c8c5d83
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "casbin"
version = "0.9.2"
version = "0.9.3"
authors = ["Joey <joey.xf@gmail.com>", "Cheng JIANG <jiang.cheng@vip.163.com>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Add this package to `Cargo.toml` of your project. (Check https://crates.io/crate

```toml
[dependencies]
casbin = { version = "0.8.5", default-features = false, features = ["runtime-async-std", "logging"] }
casbin = { version = "0.9.3", default-features = false, features = ["runtime-async-std", "logging"] }
async-std = { version = "1.5.0", features = ["attributes"] }
env_logger = "0.7.1"
```
Expand Down
45 changes: 29 additions & 16 deletions src/effector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub trait Effector: Send + Sync {

pub trait EffectorStream: Send + Sync {
fn next(&self) -> bool;
#[cfg(feature = "explain")]
fn expl(&self) -> Option<Vec<usize>>;
fn push_effect(&mut self, eft: EffectKind) -> bool;
}
Expand All @@ -22,6 +23,7 @@ pub struct DefaultEffectStream {
expr: String,
idx: usize,
cap: usize,
#[cfg(feature = "explain")]
expl: Vec<usize>,
}

Expand All @@ -46,6 +48,7 @@ impl Effector for DefaultEffector {
expr: expr.to_owned(),
cap,
idx: 0,
#[cfg(feature = "explain")]
expl: Vec::with_capacity(10),
})
}
Expand All @@ -58,13 +61,10 @@ impl EffectorStream for DefaultEffectStream {
self.res
}

#[cfg(feature = "explain")]
#[inline]
fn expl(&self) -> Option<Vec<usize>> {
assert!(self.done);
#[cfg(feature = "logging")]
#[cfg(feature = "explain")]
return None;

if self.expl.is_empty() {
None
} else {
Expand All @@ -73,39 +73,49 @@ impl EffectorStream for DefaultEffectStream {
}

fn push_effect(&mut self, eft: EffectKind) -> bool {
let has_policy = self.cap > 1;

if self.expr == "some(where (p_eft == allow))" {
if eft == EffectKind::Allow {
self.done = true;
self.res = true;

if has_policy {
self.expl.push(self.idx);
#[cfg(feature = "explain")]
{
if self.cap > 1 {
self.expl.push(self.idx);
}
}
}
} else if self.expr == "some(where (p_eft == allow)) && !some(where (p_eft == deny))" {
if eft == EffectKind::Allow {
self.res = true;

if has_policy {
self.expl.push(self.idx);
#[cfg(feature = "explain")]
{
if self.cap > 1 {
self.expl.push(self.idx);
}
}
} else if eft == EffectKind::Deny {
self.done = true;
self.res = false;

if has_policy {
self.expl.push(self.idx);
#[cfg(feature = "explain")]
{
if self.cap > 1 {
self.expl.push(self.idx);
}
}
}
} else if self.expr == "!some(where (p_eft == deny))" {
if eft == EffectKind::Deny {
self.done = true;
self.res = false;

if has_policy {
self.expl.push(self.idx);
#[cfg(feature = "explain")]
{
if self.cap > 1 {
self.expl.push(self.idx);
}
}
}
} else if self.expr == "priority(p_eft) || deny" && eft != EffectKind::Indeterminate {
Expand All @@ -116,8 +126,11 @@ impl EffectorStream for DefaultEffectStream {
}
self.done = true;

if has_policy {
self.expl.push(self.idx);
#[cfg(feature = "explain")]
{
if self.cap > 1 {
self.expl.push(self.idx);
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/enforcer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,16 @@ impl Enforcer {
};
eft_stream.push_effect(eft);
}
Ok((eft_stream.next(), eft_stream.expl()))
Ok((eft_stream.next(), {
#[cfg(feature = "explain")]
{
eft_stream.expl()
}
#[cfg(not(feature = "explain"))]
{
None
}
}))
}
}

Expand Down
1 change: 1 addition & 0 deletions src/logger/default_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl Logger for DefaultLogger {
info!("{}", d);
}

#[cfg(feature = "explain")]
fn print_expl_log(&self, rules: Vec<&Vec<String>>) {
if !self.is_enabled() {
return;
Expand Down
1 change: 1 addition & 0 deletions src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub trait Logger: Send + Sync {
fn is_enabled(&self) -> bool;
fn print_enforce_log(&self, rvals: Vec<String>, authorized: bool, cached: bool);
fn print_mgmt_log(&self, d: &EventData);
#[cfg(feature = "explain")]
fn print_expl_log(&self, rules: Vec<&Vec<String>>);
fn print_status_log(&self, enabled: bool);
}

1 comment on commit c8c5d83

@GopherJ
Copy link
Member Author

Choose a reason for hiding this comment

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

Rust Benchmark

Benchmark suite Current: c8c5d83 Previous: c2e45c5 Ratio
b_benchmark_abac_model 6851 ns/iter (± 1967) 6658 ns/iter (± 809) 1.03
b_benchmark_basic_model 6987 ns/iter (± 1829) 6918 ns/iter (± 1033) 1.01
b_benchmark_cached_abac_model 414 ns/iter (± 109) 396 ns/iter (± 48) 1.05
b_benchmark_cached_key_match 402 ns/iter (± 105) 395 ns/iter (± 63) 1.02
b_benchmark_cached_priority_model 364 ns/iter (± 91) 408 ns/iter (± 43) 0.89
b_benchmark_cached_rbac_model 412 ns/iter (± 62) 415 ns/iter (± 50) 0.99
b_benchmark_cached_rbac_model_large 436 ns/iter (± 79) 407 ns/iter (± 37) 1.07
b_benchmark_cached_rbac_model_medium 374 ns/iter (± 100) 417 ns/iter (± 44) 0.90
b_benchmark_cached_rbac_model_small 366 ns/iter (± 109) 403 ns/iter (± 64) 0.91
b_benchmark_cached_rbac_model_with_domains 412 ns/iter (± 93) 450 ns/iter (± 64) 0.92
b_benchmark_cached_rbac_with_deny 362 ns/iter (± 73) 399 ns/iter (± 49) 0.91
b_benchmark_cached_rbac_with_resource_roles 389 ns/iter (± 143) 392 ns/iter (± 55) 0.99
b_benchmark_key_match 25115 ns/iter (± 4788) 23351 ns/iter (± 3791) 1.08
b_benchmark_priority_model 8237 ns/iter (± 1466) 7736 ns/iter (± 1140) 1.06
b_benchmark_raw 8 ns/iter (± 1) 8 ns/iter (± 0) 1
b_benchmark_rbac_model 10853 ns/iter (± 2477) 11283 ns/iter (± 1310) 0.96
b_benchmark_rbac_model_large 15445730 ns/iter (± 3467288) 17528654 ns/iter (± 3379591) 0.88
b_benchmark_rbac_model_medium 1464631 ns/iter (± 333890) 1669121 ns/iter (± 104148) 0.88
b_benchmark_rbac_model_small 156647 ns/iter (± 37495) 172752 ns/iter (± 7197) 0.91
b_benchmark_rbac_model_with_domains 11477 ns/iter (± 3410) 11267 ns/iter (± 616) 1.02
b_benchmark_rbac_with_deny 18943 ns/iter (± 1515) 17144 ns/iter (± 763) 1.10
b_benchmark_rbac_with_resource_roles 8111 ns/iter (± 1403) 7993 ns/iter (± 951) 1.01
b_benmark_cached_basic_model 405 ns/iter (± 91) 419 ns/iter (± 43) 0.97

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.