Skip to content

Commit 4ee6955

Browse files
committed
refactor(capi+go): use a single option for enabling all condition optimizations
1 parent 44de6f3 commit 4ee6955

File tree

4 files changed

+31
-30
lines changed

4 files changed

+31
-30
lines changed

capi/include/yara_x.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
// errors instead of warnings.
4242
#define YRX_ERROR_ON_SLOW_LOOP 8
4343

44-
// Flag passed to [`yrx_compiler_create`] for enabling hoisting. This
45-
// is a compiler optimization that moves invariant expressions out of
46-
// loops, improving performance during the evaluation of rule conditions
47-
// that contains loops.
48-
#define YRX_ENABLE_HOISTING 16
44+
// Flag passed to [`yrx_compiler_create`] for enabling optimizations.
45+
// With this flag the compiler tries to optimize rule conditions by applying
46+
// techniques like common subexpression elimination (CSE) and loop-invariant
47+
// code motion (LICM).
48+
#define YRX_ENABLE_CONDITION_OPTIMIZATION 16
4949

5050
// Types of metadata values.
5151
typedef enum YRX_METADATA_TYPE {

capi/src/compiler.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,20 @@ pub const YRX_ERROR_ON_SLOW_PATTERN: u32 = 4;
4040
/// errors instead of warnings.
4141
pub const YRX_ERROR_ON_SLOW_LOOP: u32 = 8;
4242

43-
/// Flag passed to [`yrx_compiler_create`] for enabling hoisting. This
44-
/// is a compiler optimization that moves invariant expressions out of
45-
/// loops, improving performance during the evaluation of rule conditions
46-
/// that contains loops.
47-
pub const YRX_ENABLE_HOISTING: u32 = 16;
43+
/// Flag passed to [`yrx_compiler_create`] for enabling optimizations.
44+
/// With this flag the compiler tries to optimize rule conditions by applying
45+
/// techniques like common subexpression elimination (CSE) and loop-invariant
46+
/// code motion (LICM).
47+
pub const YRX_ENABLE_CONDITION_OPTIMIZATION: u32 = 16;
4848

4949
fn _yrx_compiler_create<'a>(flags: u32) -> yara_x::Compiler<'a> {
5050
let mut compiler = yara_x::Compiler::new();
5151
if flags & YRX_RELAXED_RE_SYNTAX != 0 {
5252
compiler.relaxed_re_syntax(true);
5353
}
54-
if flags & YRX_ENABLE_HOISTING != 0 {
54+
if flags & YRX_ENABLE_CONDITION_OPTIMIZATION != 0 {
5555
compiler.hoisting(true);
56+
compiler.common_subexpression_elimination(true);
5657
}
5758
if flags & YRX_COLORIZE_ERRORS != 0 {
5859
compiler.colorize_errors(true);

go/compiler.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ func RelaxedReSyntax(yes bool) CompileOption {
9797
}
9898
}
9999

100-
// Hoisting is an option for [NewCompiler] and [Compile] that enables a
101-
// compiler optimization that moves invariant expressions out of loops,
102-
// improving performance during the evaluation of rule conditions that
103-
// contains loops.
104-
func Hoisting(yes bool) CompileOption {
100+
// ConditionOptimization is an option for [NewCompiler] and [Compile] that
101+
// enables the optimization of rule conditions. When this is true the compiler
102+
// applies techniques like common subexpression elimination (CSE) and
103+
// loop-invariant code motion (LICM).
104+
func ConditionOptimization(yes bool) CompileOption {
105105
return func(c *Compiler) error {
106-
c.hoisting = yes
106+
c.conditionOptimization = yes
107107
return nil
108108
}
109109
}
@@ -237,15 +237,15 @@ type bannedModule struct {
237237

238238
// Compiler represent a YARA compiler.
239239
type Compiler struct {
240-
cCompiler *C.YRX_COMPILER
241-
relaxedReSyntax bool
242-
hoisting bool
243-
errorOnSlowPattern bool
244-
errorOnSlowLoop bool
245-
ignoredModules map[string]bool
246-
bannedModules map[string]bannedModule
247-
vars map[string]interface{}
248-
features []string
240+
cCompiler *C.YRX_COMPILER
241+
relaxedReSyntax bool
242+
conditionOptimization bool
243+
errorOnSlowPattern bool
244+
errorOnSlowLoop bool
245+
ignoredModules map[string]bool
246+
bannedModules map[string]bannedModule
247+
vars map[string]interface{}
248+
features []string
249249
}
250250

251251
// NewCompiler creates a new compiler.
@@ -268,8 +268,8 @@ func NewCompiler(opts ...CompileOption) (*Compiler, error) {
268268
flags |= C.YRX_RELAXED_RE_SYNTAX
269269
}
270270

271-
if c.hoisting {
272-
flags |= C.YRX_ENABLE_HOISTING
271+
if c.conditionOptimization {
272+
flags |= C.YRX_ENABLE_CONDITION_OPTIMIZATION
273273
}
274274

275275
if c.errorOnSlowPattern {

go/compiler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ func TestRelaxedReSyntax(t *testing.T) {
5454
assert.Len(t, scanResults.MatchingRules(), 1)
5555
}
5656

57-
func TestHoisting(t *testing.T) {
57+
func TestConditionOptimization(t *testing.T) {
5858
_, err := Compile(`
5959
rule test { condition: true }`,
60-
Hoisting(true))
60+
ConditionOptimization(true))
6161
assert.NoError(t, err)
6262
}
6363

0 commit comments

Comments
 (0)