Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit ebd9e2d

Browse files
authored
Implement push rule evaluation in Rust. (#13838)
1 parent a466164 commit ebd9e2d

File tree

14 files changed

+894
-403
lines changed

14 files changed

+894
-403
lines changed

changelog.d/13838.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port push rules to using Rust.

rust/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ rust-version = "1.58.1"
1111

1212
[lib]
1313
name = "synapse"
14-
crate-type = ["cdylib"]
14+
# We generate a `cdylib` for Python and a standard `lib` for running
15+
# tests/benchmarks.
16+
crate-type = ["lib", "cdylib"]
1517

1618
[package.metadata.maturin]
1719
# This is where we tell maturin where to place the built library.

rust/benches/evaluator.rs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// Copyright 2022 The Matrix.org Foundation C.I.C.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#![feature(test)]
16+
use synapse::push::{
17+
evaluator::PushRuleEvaluator, Condition, EventMatchCondition, FilteredPushRules, PushRules,
18+
};
19+
use test::Bencher;
20+
21+
extern crate test;
22+
23+
#[bench]
24+
fn bench_match_exact(b: &mut Bencher) {
25+
let flattened_keys = [
26+
("type".to_string(), "m.text".to_string()),
27+
("room_id".to_string(), "!room:server".to_string()),
28+
("content.body".to_string(), "test message".to_string()),
29+
]
30+
.into_iter()
31+
.collect();
32+
33+
let eval = PushRuleEvaluator::py_new(
34+
flattened_keys,
35+
10,
36+
0,
37+
Default::default(),
38+
Default::default(),
39+
true,
40+
)
41+
.unwrap();
42+
43+
let condition = Condition::Known(synapse::push::KnownCondition::EventMatch(
44+
EventMatchCondition {
45+
key: "room_id".into(),
46+
pattern: Some("!room:server".into()),
47+
pattern_type: None,
48+
},
49+
));
50+
51+
let matched = eval.match_condition(&condition, None, None).unwrap();
52+
assert!(matched, "Didn't match");
53+
54+
b.iter(|| eval.match_condition(&condition, None, None).unwrap());
55+
}
56+
57+
#[bench]
58+
fn bench_match_word(b: &mut Bencher) {
59+
let flattened_keys = [
60+
("type".to_string(), "m.text".to_string()),
61+
("room_id".to_string(), "!room:server".to_string()),
62+
("content.body".to_string(), "test message".to_string()),
63+
]
64+
.into_iter()
65+
.collect();
66+
67+
let eval = PushRuleEvaluator::py_new(
68+
flattened_keys,
69+
10,
70+
0,
71+
Default::default(),
72+
Default::default(),
73+
true,
74+
)
75+
.unwrap();
76+
77+
let condition = Condition::Known(synapse::push::KnownCondition::EventMatch(
78+
EventMatchCondition {
79+
key: "content.body".into(),
80+
pattern: Some("test".into()),
81+
pattern_type: None,
82+
},
83+
));
84+
85+
let matched = eval.match_condition(&condition, None, None).unwrap();
86+
assert!(matched, "Didn't match");
87+
88+
b.iter(|| eval.match_condition(&condition, None, None).unwrap());
89+
}
90+
91+
#[bench]
92+
fn bench_match_word_miss(b: &mut Bencher) {
93+
let flattened_keys = [
94+
("type".to_string(), "m.text".to_string()),
95+
("room_id".to_string(), "!room:server".to_string()),
96+
("content.body".to_string(), "test message".to_string()),
97+
]
98+
.into_iter()
99+
.collect();
100+
101+
let eval = PushRuleEvaluator::py_new(
102+
flattened_keys,
103+
10,
104+
0,
105+
Default::default(),
106+
Default::default(),
107+
true,
108+
)
109+
.unwrap();
110+
111+
let condition = Condition::Known(synapse::push::KnownCondition::EventMatch(
112+
EventMatchCondition {
113+
key: "content.body".into(),
114+
pattern: Some("foobar".into()),
115+
pattern_type: None,
116+
},
117+
));
118+
119+
let matched = eval.match_condition(&condition, None, None).unwrap();
120+
assert!(!matched, "Didn't match");
121+
122+
b.iter(|| eval.match_condition(&condition, None, None).unwrap());
123+
}
124+
125+
#[bench]
126+
fn bench_eval_message(b: &mut Bencher) {
127+
let flattened_keys = [
128+
("type".to_string(), "m.text".to_string()),
129+
("room_id".to_string(), "!room:server".to_string()),
130+
("content.body".to_string(), "test message".to_string()),
131+
]
132+
.into_iter()
133+
.collect();
134+
135+
let eval = PushRuleEvaluator::py_new(
136+
flattened_keys,
137+
10,
138+
0,
139+
Default::default(),
140+
Default::default(),
141+
true,
142+
)
143+
.unwrap();
144+
145+
let rules =
146+
FilteredPushRules::py_new(PushRules::new(Vec::new()), Default::default(), false, false);
147+
148+
b.iter(|| eval.run(&rules, Some("bob"), Some("person")));
149+
}

rust/benches/glob.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2022 The Matrix.org Foundation C.I.C.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#![feature(test)]
16+
17+
use synapse::push::utils::{glob_to_regex, GlobMatchType};
18+
use test::Bencher;
19+
20+
extern crate test;
21+
22+
#[bench]
23+
fn bench_whole(b: &mut Bencher) {
24+
b.iter(|| glob_to_regex("test", GlobMatchType::Whole));
25+
}
26+
27+
#[bench]
28+
fn bench_word(b: &mut Bencher) {
29+
b.iter(|| glob_to_regex("test", GlobMatchType::Word));
30+
}
31+
32+
#[bench]
33+
fn bench_whole_wildcard_run(b: &mut Bencher) {
34+
b.iter(|| glob_to_regex("test***??*?*?foo", GlobMatchType::Whole));
35+
}
36+
37+
#[bench]
38+
fn bench_word_wildcard_run(b: &mut Bencher) {
39+
b.iter(|| glob_to_regex("test***??*?*?foo", GlobMatchType::Whole));
40+
}

rust/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn main() -> Result<(), std::io::Error> {
2222

2323
for entry in entries {
2424
if entry.is_dir() {
25-
dirs.push(entry)
25+
dirs.push(entry);
2626
} else {
2727
paths.push(entry.to_str().expect("valid rust paths").to_string());
2828
}

rust/src/push/base_rules.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ pub const BASE_APPEND_UNDERRIDE_RULES: &[PushRule] = &[
262262
priority_class: 1,
263263
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::RelationMatch {
264264
rel_type: Cow::Borrowed("m.thread"),
265+
event_type_pattern: None,
265266
sender: None,
266267
sender_type: Some(Cow::Borrowed("user_id")),
267268
})]),

0 commit comments

Comments
 (0)