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

Implement push rule evaluation in Rust. #13838

Merged
merged 40 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
c2d0c27
Implement push rules as Rust native types.
erikjohnston Sep 4, 2022
dcea8d8
Remove python baserules
erikjohnston Sep 9, 2022
62caed9
Newsfile
erikjohnston Sep 9, 2022
071953f
Update rust/src/push/mod.rs
erikjohnston Sep 12, 2022
6605754
Merge remote-tracking branch 'origin/develop' into erikj/rust_push_rules
erikjohnston Sep 12, 2022
4d97349
Code review
erikjohnston Sep 13, 2022
e828b34
Apply suggestions from code review
erikjohnston Sep 19, 2022
47a3dcf
Apply suggestions from code review
erikjohnston Sep 19, 2022
68689fa
Apply suggestions from code review
erikjohnston Sep 19, 2022
a109a77
Remove newsfile
erikjohnston Sep 19, 2022
dbeb21b
Remove old todo
erikjohnston Sep 19, 2022
0e10bb3
Swap actions
erikjohnston Sep 19, 2022
1e05843
Add back module docstring
erikjohnston Sep 19, 2022
2945dc0
Merge remote-tracking branch 'origin/develop' into erikj/rust_push_rules
erikjohnston Sep 19, 2022
415bbd6
Remove debug logging
erikjohnston Sep 19, 2022
86515a9
Handle unrecognized actions
erikjohnston Sep 19, 2022
b57be51
Correctly handle unknwon conditions
erikjohnston Sep 19, 2022
a691a2d
Lint
erikjohnston Sep 19, 2022
b730876
Implement push rule evaluation in Rust
erikjohnston Sep 19, 2022
d62a7ec
Ignore unknown conditions to fix unit tests
erikjohnston Sep 19, 2022
65f55a2
Update rust/src/push/evaluator.rs
erikjohnston Sep 20, 2022
b7ed027
Merge remote-tracking branch 'origin/develop' into erikj/rust_push_eval2
erikjohnston Sep 20, 2022
e75b755
Newsfile
erikjohnston Sep 20, 2022
bc3b216
Fix rust test
erikjohnston Sep 20, 2022
da1dc66
Fast path
erikjohnston Sep 21, 2022
42ab02e
Benchmarks
erikjohnston Sep 21, 2022
4da5b04
Merge remote-tracking branch 'origin/develop' into erikj/rust_push_eval2
erikjohnston Sep 24, 2022
e6d9341
Speed up
erikjohnston Sep 24, 2022
bef2298
Add a Matcher type
erikjohnston Sep 24, 2022
c51bf1e
Tidy up
erikjohnston Sep 24, 2022
307e77a
Remove unnecessary optimisation
erikjohnston Sep 25, 2022
f760ada
Comment
erikjohnston Sep 25, 2022
9df6d37
clippy
erikjohnston Sep 25, 2022
e4cd075
Copyright headers
erikjohnston Sep 25, 2022
0035f14
Update rust/Cargo.toml
erikjohnston Sep 28, 2022
a503245
Merge remote-tracking branch 'origin/develop' into erikj/rust_push_eval2
erikjohnston Sep 29, 2022
1034e7f
Review comments
erikjohnston Sep 29, 2022
de494f2
Fix merge
erikjohnston Sep 29, 2022
23b307d
Apply suggestions from code review
erikjohnston Sep 29, 2022
09b728d
Fix test
erikjohnston Sep 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/13838.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port push rules to using Rust.
4 changes: 3 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ rust-version = "1.58.1"

[lib]
name = "synapse"
crate-type = ["cdylib"]
# We generate a `cdylib` for Python and a standard `lib` for running
# tests/benchmarks.
crate-type = ["lib", "cdylib"]

[package.metadata.maturin]
# This is where we tell maturin where to place the built library.
Expand Down
149 changes: 149 additions & 0 deletions rust/benches/evaluator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright 2022 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![feature(test)]
use synapse::push::{
evaluator::PushRuleEvaluator, Condition, EventMatchCondition, FilteredPushRules, PushRules,
};
use test::Bencher;

extern crate test;

#[bench]
fn bench_match_exact(b: &mut Bencher) {
let flattened_keys = [
("type".to_string(), "m.text".to_string()),
("room_id".to_string(), "!room:server".to_string()),
("content.body".to_string(), "test message".to_string()),
]
.into_iter()
.collect();

let eval = PushRuleEvaluator::py_new(
flattened_keys,
10,
0,
Default::default(),
Default::default(),
true,
)
.unwrap();

let condition = Condition::Known(synapse::push::KnownCondition::EventMatch(
EventMatchCondition {
key: "room_id".into(),
pattern: Some("!room:server".into()),
pattern_type: None,
},
));

let matched = eval.match_condition(&condition, None, None).unwrap();
assert!(matched, "Didn't match");

b.iter(|| eval.match_condition(&condition, None, None).unwrap());
}

#[bench]
fn bench_match_word(b: &mut Bencher) {
let flattened_keys = [
("type".to_string(), "m.text".to_string()),
("room_id".to_string(), "!room:server".to_string()),
("content.body".to_string(), "test message".to_string()),
]
.into_iter()
.collect();

let eval = PushRuleEvaluator::py_new(
flattened_keys,
10,
0,
Default::default(),
Default::default(),
true,
)
.unwrap();

let condition = Condition::Known(synapse::push::KnownCondition::EventMatch(
EventMatchCondition {
key: "content.body".into(),
pattern: Some("test".into()),
pattern_type: None,
},
));

let matched = eval.match_condition(&condition, None, None).unwrap();
assert!(matched, "Didn't match");

b.iter(|| eval.match_condition(&condition, None, None).unwrap());
}

#[bench]
fn bench_match_word_miss(b: &mut Bencher) {
let flattened_keys = [
("type".to_string(), "m.text".to_string()),
("room_id".to_string(), "!room:server".to_string()),
("content.body".to_string(), "test message".to_string()),
]
.into_iter()
.collect();

let eval = PushRuleEvaluator::py_new(
flattened_keys,
10,
0,
Default::default(),
Default::default(),
true,
)
.unwrap();

let condition = Condition::Known(synapse::push::KnownCondition::EventMatch(
EventMatchCondition {
key: "content.body".into(),
pattern: Some("foobar".into()),
pattern_type: None,
},
));

let matched = eval.match_condition(&condition, None, None).unwrap();
assert!(!matched, "Didn't match");

b.iter(|| eval.match_condition(&condition, None, None).unwrap());
}

#[bench]
fn bench_eval_message(b: &mut Bencher) {
let flattened_keys = [
("type".to_string(), "m.text".to_string()),
("room_id".to_string(), "!room:server".to_string()),
("content.body".to_string(), "test message".to_string()),
]
.into_iter()
.collect();

let eval = PushRuleEvaluator::py_new(
flattened_keys,
10,
0,
Default::default(),
Default::default(),
true,
)
.unwrap();

let rules =
FilteredPushRules::py_new(PushRules::new(Vec::new()), Default::default(), false, false);

b.iter(|| eval.run(&rules, Some("bob"), Some("person")));
}
40 changes: 40 additions & 0 deletions rust/benches/glob.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2022 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![feature(test)]

use synapse::push::utils::{glob_to_regex, GlobMatchType};
use test::Bencher;

extern crate test;

#[bench]
fn bench_whole(b: &mut Bencher) {
b.iter(|| glob_to_regex("test", GlobMatchType::Whole));
}

#[bench]
fn bench_word(b: &mut Bencher) {
b.iter(|| glob_to_regex("test", GlobMatchType::Word));
}

#[bench]
fn bench_whole_wildcard_run(b: &mut Bencher) {
b.iter(|| glob_to_regex("test***??*?*?foo", GlobMatchType::Whole));
}

#[bench]
fn bench_word_wildcard_run(b: &mut Bencher) {
b.iter(|| glob_to_regex("test***??*?*?foo", GlobMatchType::Whole));
}
2 changes: 1 addition & 1 deletion rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() -> Result<(), std::io::Error> {

for entry in entries {
if entry.is_dir() {
dirs.push(entry)
dirs.push(entry);
} else {
paths.push(entry.to_str().expect("valid rust paths").to_string());
}
Expand Down
1 change: 1 addition & 0 deletions rust/src/push/base_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ pub const BASE_APPEND_UNDERRIDE_RULES: &[PushRule] = &[
priority_class: 1,
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::RelationMatch {
rel_type: Cow::Borrowed("m.thread"),
event_type_pattern: None,
sender: None,
sender_type: Some(Cow::Borrowed("user_id")),
})]),
Expand Down
Loading