Skip to content

Commit c7c016b

Browse files
authored
Replace once_cell:: to std::sync:: (#580)
1 parent b8013ec commit c7c016b

File tree

14 files changed

+98
-91
lines changed

14 files changed

+98
-91
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ exclude = [
2626
addr = { version = "0.15", default-features = false, features = ["psl"], optional = true }
2727
url = "2.5"
2828
percent-encoding = "2.1"
29-
once_cell = "1.8"
3029
regex = "1.12.2"
3130
bitflags = { version = "2.10.0", features = ["serde"] }
3231
itertools = "0.13"

benches/bench_rules.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use criterion::*;
2-
use once_cell::sync::Lazy;
2+
use std::sync::LazyLock;
33

44
use adblock::{Engine, FilterSet};
55

66
#[path = "../tests/test_utils.rs"]
77
mod test_utils;
88
use test_utils::rules_from_lists;
99

10-
static DEFAULT_LISTS: Lazy<Vec<String>> =
11-
Lazy::new(|| rules_from_lists(&["data/easylist.to/easylist/easylist.txt"]).collect());
10+
static DEFAULT_LISTS: LazyLock<Vec<String>> =
11+
LazyLock::new(|| rules_from_lists(&["data/easylist.to/easylist/easylist.txt"]).collect());
1212

1313
fn bench_string_hashing(filters: &Vec<String>) -> adblock::utils::Hash {
1414
let mut dummy: adblock::utils::Hash = 0;

src/blocker.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Holds [`Blocker`], which handles all network-based adblocking queries.
22
33
use memchr::{memchr as find_char, memrchr as find_char_reverse};
4-
use once_cell::sync::Lazy;
54
use serde::Serialize;
65
use std::collections::HashSet;
76
use std::ops::DerefMut;
7+
use std::sync::OnceLock;
88

99
use crate::filters::fb_network_builder::NetworkFilterListId;
1010
use crate::filters::filter_data_context::FilterDataContextRef;
@@ -63,7 +63,10 @@ pub struct BlockerResult {
6363

6464
// only check for tags in tagged and exception rule buckets,
6565
// pass empty set for the rest
66-
static NO_TAGS: Lazy<HashSet<String>> = Lazy::new(HashSet::new);
66+
fn get_no_tags() -> &'static HashSet<String> {
67+
static NO_TAGS: OnceLock<HashSet<String>> = OnceLock::new();
68+
NO_TAGS.get_or_init(&HashSet::new)
69+
}
6770

6871
/// Stores network filters for efficient querying.
6972
pub struct Blocker {
@@ -186,13 +189,16 @@ impl Blocker {
186189
// Always check important filters
187190
let important_filter = self
188191
.importants()
189-
.check(request, &NO_TAGS, &mut regex_manager);
192+
.check(request, get_no_tags(), &mut regex_manager);
190193

191194
// only check the rest of the rules if not previously matched
192195
let filter = if important_filter.is_none() && !matched_rule {
193196
self.tagged_filters_all()
194197
.check(request, &self.tags_enabled, &mut regex_manager)
195-
.or_else(|| self.filters().check(request, &NO_TAGS, &mut regex_manager))
198+
.or_else(|| {
199+
self.filters()
200+
.check(request, get_no_tags(), &mut regex_manager)
201+
})
196202
} else {
197203
important_filter
198204
};
@@ -213,7 +219,7 @@ impl Blocker {
213219

214220
let redirect_filters =
215221
self.redirects()
216-
.check_all(request, &NO_TAGS, regex_manager.deref_mut());
222+
.check_all(request, get_no_tags(), regex_manager.deref_mut());
217223

218224
// Extract the highest priority redirect directive.
219225
// 1. Exceptions - can bail immediately if found
@@ -339,7 +345,7 @@ impl Blocker {
339345
.map(|param| (param, true))
340346
.collect();
341347

342-
let filters = removeparam_filters.check_all(request, &NO_TAGS, regex_manager);
348+
let filters = removeparam_filters.check_all(request, get_no_tags(), regex_manager);
343349
let mut rewrite = false;
344350
for removeparam_filter in filters {
345351
if let Some(removeparam) = &removeparam_filter.modifier_option {

src/content_blocking.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use crate::filters::network::{NetworkFilter, NetworkFilterMask, NetworkFilterMas
55
use crate::lists::ParsedFilter;
66

77
use memchr::{memchr as find_char, memmem};
8-
use once_cell::sync::Lazy;
98
use regex::Regex;
109
use serde::{Deserialize, Serialize};
10+
use std::sync::LazyLock;
1111

1212
use std::collections::HashSet;
1313
use std::convert::{TryFrom, TryInto};
@@ -305,10 +305,12 @@ impl TryFrom<NetworkFilter> for CbRuleEquivalent {
305305
type Error = CbRuleCreationFailure;
306306

307307
fn try_from(v: NetworkFilter) -> Result<Self, Self::Error> {
308-
static SPECIAL_CHARS: Lazy<Regex> =
309-
Lazy::new(|| Regex::new(r##"([.+?^${}()|\[\]\\])"##).unwrap());
310-
static REPLACE_WILDCARDS: Lazy<Regex> = Lazy::new(|| Regex::new(r##"\*"##).unwrap());
311-
static TRAILING_SEPARATOR: Lazy<Regex> = Lazy::new(|| Regex::new(r##"\^$"##).unwrap());
308+
static SPECIAL_CHARS: LazyLock<Regex> =
309+
LazyLock::new(|| Regex::new(r##"([.+?^${}()|\[\]\\])"##).unwrap());
310+
static REPLACE_WILDCARDS: LazyLock<Regex> =
311+
LazyLock::new(|| Regex::new(r##"\*"##).unwrap());
312+
static TRAILING_SEPARATOR: LazyLock<Regex> =
313+
LazyLock::new(|| Regex::new(r##"\^$"##).unwrap());
312314
if let Some(raw_line) = &v.raw_line {
313315
if v.is_redirect() {
314316
return Err(CbRuleCreationFailure::NetworkRedirectUnsupported);

src/cosmetic_filter_utils.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ use memchr::memchr as find_char;
99
/// This should only be called once `selector` has been verified to start with either a "#" or "."
1010
/// character.
1111
pub(crate) fn key_from_selector(selector: &str) -> Option<String> {
12-
use once_cell::sync::Lazy;
1312
use regex::Regex;
14-
15-
static RE_PLAIN_SELECTOR: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[#.][\w\\-]+").unwrap());
16-
static RE_PLAIN_SELECTOR_ESCAPED: Lazy<Regex> =
17-
Lazy::new(|| Regex::new(r"^[#.](?:\\[0-9A-Fa-f]+ |\\.|\w|-)+").unwrap());
18-
static RE_ESCAPE_SEQUENCE: Lazy<Regex> =
19-
Lazy::new(|| Regex::new(r"\\([0-9A-Fa-f]+ |.)").unwrap());
13+
use std::sync::LazyLock;
14+
15+
static RE_PLAIN_SELECTOR: LazyLock<Regex> =
16+
LazyLock::new(|| Regex::new(r"^[#.][\w\\-]+").unwrap());
17+
static RE_PLAIN_SELECTOR_ESCAPED: LazyLock<Regex> =
18+
LazyLock::new(|| Regex::new(r"^[#.](?:\\[0-9A-Fa-f]+ |\\.|\w|-)+").unwrap());
19+
static RE_ESCAPE_SEQUENCE: LazyLock<Regex> =
20+
LazyLock::new(|| Regex::new(r"\\([0-9A-Fa-f]+ |.)").unwrap());
2021

2122
// If there are no escape characters in the selector, just take the first class or id token.
2223
let mat = RE_PLAIN_SELECTOR.find(selector);

src/filters/abstract_network.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use memchr::memrchr as find_char_reverse;
22

33
use super::network::NetworkFilterError;
44

5-
use once_cell::sync::Lazy;
65
use regex::Regex;
6+
use std::sync::LazyLock;
77

88
/// For now, only support `$removeparam` with simple alphanumeric/dash/underscore patterns.
9-
static VALID_PARAM: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-zA-Z0-9_\-]+$").unwrap());
9+
static VALID_PARAM: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[a-zA-Z0-9_\-]+$").unwrap());
1010

1111
#[derive(Clone, Copy)]
1212
pub(crate) enum NetworkFilterLeftAnchor {

src/filters/cosmetic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,10 +621,10 @@ mod css_validation {
621621
selector: &str,
622622
accept_abp_selectors: bool,
623623
) -> Result<Vec<CosmeticFilterOperator>, CosmeticFilterError> {
624-
use once_cell::sync::Lazy;
625624
use regex::Regex;
626-
static RE_SIMPLE_SELECTOR: Lazy<Regex> =
627-
Lazy::new(|| Regex::new(r"^[#.]?[A-Za-z_][\w-]*$").unwrap());
625+
use std::sync::LazyLock;
626+
static RE_SIMPLE_SELECTOR: LazyLock<Regex> =
627+
LazyLock::new(|| Regex::new(r"^[#.]?[A-Za-z_][\w-]*$").unwrap());
628628

629629
if RE_SIMPLE_SELECTOR.is_match(selector) {
630630
return Ok(vec![CosmeticFilterOperator::CssSelector(

src/filters/network.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
//! modification.
33
44
use memchr::memchr as find_char;
5-
use once_cell::sync::Lazy;
65
use regex::Regex;
76
use serde::{Deserialize, Serialize};
7+
use std::sync::LazyLock;
88
use thiserror::Error;
99

1010
use std::fmt;
@@ -18,7 +18,7 @@ use crate::request;
1818
use crate::utils::{self, Hash, TokensBuffer};
1919

2020
/// For now, only support `$removeparam` with simple alphanumeric/dash/underscore patterns.
21-
static VALID_PARAM: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-zA-Z0-9_\-]+$").unwrap());
21+
static VALID_PARAM: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[a-zA-Z0-9_\-]+$").unwrap());
2222

2323
#[non_exhaustive]
2424
#[derive(Debug, Error, PartialEq, Clone)]
@@ -641,7 +641,7 @@ impl NetworkFilter {
641641
// and then the pattern.
642642
// TODO - this could be made more efficient if we could match between two
643643
// indices. Once again, we have to do more work than is really needed.
644-
static SEPARATOR: Lazy<Regex> = Lazy::new(|| Regex::new("[/^*]").unwrap());
644+
static SEPARATOR: LazyLock<Regex> = LazyLock::new(|| Regex::new("[/^*]").unwrap());
645645
if let Some(first_separator) = SEPARATOR.find(pattern) {
646646
let first_separator_start = first_separator.start();
647647
// NOTE: `first_separator` shall never be -1 here since `IS_REGEX` is true.
@@ -825,8 +825,8 @@ impl NetworkFilter {
825825
/// emulate the behavior of hosts-style blocking.
826826
pub fn parse_hosts_style(hostname: &str, debug: bool) -> Result<Self, NetworkFilterError> {
827827
// Make sure the hostname doesn't contain any invalid characters
828-
static INVALID_CHARS: Lazy<Regex> =
829-
Lazy::new(|| Regex::new("[/^*!?$&(){}\\[\\]+=~`\\s|@,'\"><:;]").unwrap());
828+
static INVALID_CHARS: LazyLock<Regex> =
829+
LazyLock::new(|| Regex::new("[/^*!?$&(){}\\[\\]+=~`\\s|@,'\"><:;]").unwrap());
830830
if INVALID_CHARS.is_match(hostname) {
831831
return Err(NetworkFilterError::FilterParseError);
832832
}

src/regex_manager.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use regex::{
88
bytes::Regex as BytesRegex, bytes::RegexBuilder as BytesRegexBuilder,
99
bytes::RegexSet as BytesRegexSet, bytes::RegexSetBuilder as BytesRegexSetBuilder, Regex,
1010
};
11+
use std::sync::LazyLock;
1112

1213
use std::collections::HashMap;
1314
use std::fmt;
@@ -173,16 +174,15 @@ pub(crate) fn compile_regex<'a, I>(
173174
where
174175
I: Iterator<Item = &'a str> + ExactSizeIterator,
175176
{
176-
use once_cell::sync::Lazy;
177177
// Escape special regex characters: |.$+?{}()[]\
178-
static SPECIAL_RE: Lazy<Regex> =
179-
Lazy::new(|| Regex::new(r"([\|\.\$\+\?\{\}\(\)\[\]])").unwrap());
178+
static SPECIAL_RE: LazyLock<Regex> =
179+
LazyLock::new(|| Regex::new(r"([\|\.\$\+\?\{\}\(\)\[\]])").unwrap());
180180
// * can match anything
181-
static WILDCARD_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\*").unwrap());
181+
static WILDCARD_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\*").unwrap());
182182
// ^ can match any separator or the end of the pattern
183-
static ANCHOR_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\^(.)").unwrap());
183+
static ANCHOR_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\^(.)").unwrap());
184184
// ^ can match any separator or the end of the pattern
185-
static ANCHOR_RE_EOL: Lazy<Regex> = Lazy::new(|| Regex::new(r"\^$").unwrap());
185+
static ANCHOR_RE_EOL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\^$").unwrap());
186186

187187
let mut escaped_patterns = Vec::with_capacity(filters.len());
188188
for filter_str in filters {

0 commit comments

Comments
 (0)