Skip to content

Commit

Permalink
Optimize targeting expression evaluation to bypass inspection when AN…
Browse files Browse the repository at this point in the history
…D/OR resolved
  • Loading branch information
serprex committed Mar 3, 2024
1 parent cb5d3ac commit 9eaa9d5
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions src/rs/src/skill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,6 @@ macro_rules! tgtcore {
}

impl Tgt {
const _not: u32 = 0;
deftgt!(own, _own, 1);
deftgt!(notself, _notself, 2);
deftgt!(all, _all, 3);
Expand Down Expand Up @@ -683,17 +682,18 @@ impl Tgt {
let mut opnum = 0;
let mut vals = [false; 4];
let mut valnum = 0u8;
let mut ignore = 0;

loop {
if (x & 1) == 0 {
let f = (x >> 1) & 31;
x >>= 6;
let res = match f {
Tgt::_not => {
ops[opnum] = (1, valnum);
opnum += 1;
continue;
}
if f == 0 {
ops[opnum] = (1, valnum);
opnum += 1;
continue;
}
vals[valnum as usize] = ignore == 0 && match f {
Tgt::_own => ctx.get_owner(c) == ctx.get_owner(t),
Tgt::_notself => c != t,
Tgt::_all => true,
Expand Down Expand Up @@ -794,7 +794,6 @@ impl Tgt {
}
_ => false,
};
vals[valnum as usize] = res;
valnum += 1;
} else {
ops[opnum] = ((x & 2) as u8, valnum);
Expand All @@ -806,25 +805,34 @@ impl Tgt {
if opnum == 0 {
return vals[valnum as usize - 1];
}
match ops[opnum - 1] {
(0, n) if valnum > n + 1 => {
opnum -= 1;
vals[valnum as usize - 2] &= vals[valnum as usize - 1];
valnum -= 1;
continue;
if valnum <= ops[opnum - 1].1 {
break
}
let op = ops[opnum - 1].0;
match op {
0 | 2 => { // AND | OR
let val = vals[valnum as usize - 1];
if (op == 0) != val {
ignore += 1;
ops[opnum - 1] = (4, valnum);
} else {
valnum -= 1;
ops[opnum - 1] = (3, valnum);
}
}
(1, n) if valnum > n => {
1 => { // NOT
opnum -= 1;
vals[valnum as usize - 1] = !vals[valnum as usize - 1];
continue;
}
(2, n) if valnum > n + 1 => {
3 => { // NEXT
opnum -= 1;
}
4 => { // DROP
opnum -= 1;
vals[valnum as usize - 2] |= vals[valnum as usize - 1];
valnum -= 1;
continue;
ignore -= 1;
}
_ => break,
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
}
Expand Down

0 comments on commit 9eaa9d5

Please sign in to comment.