Skip to content

Commit

Permalink
perf(extract): use SyncString instead of String (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac authored Aug 28, 2024
1 parent bf6bacd commit 2499f76
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/extract/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{
http::random_ua, walk::Script, ApiKeyExtractor, Config, ScriptMessage, ScriptReceiver,
};

use super::{error::DownloadScriptDiagnostic, ApiKeyError};
use super::{error::DownloadScriptDiagnostic, util::SyncString, ApiKeyError};

#[derive(Debug)]
pub enum ApiKeyMessage {
Expand Down Expand Up @@ -249,14 +249,16 @@ impl ApiKeyCollector {
// convert into an ApiKeyError and send over channel
if !api_keys.is_empty() {
let num_keys = api_keys.len();
let url_string = url.to_string();
let url_string = SyncString::from(url.to_string());
let source = Arc::new(
NamedSource::new(url_string.clone(), script.to_string())
.with_language("javascript"),
);
let api_keys = api_keys
.into_iter()
.map(|api_key| ApiKeyError::new(api_key, url_string.clone(), &source, &self.config))
.map(|api_key| {
ApiKeyError::new(api_key, url_string.clone().into(), &source, &self.config)
})
.collect::<Vec<_>>();
self.sender
.send(ApiKeyMessage::Keys(api_keys))
Expand Down
6 changes: 3 additions & 3 deletions src/extract/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ pub struct ApiKeyError {
pub rule_id: String,
pub secret: String,
pub key_name: Option<String>,
pub url: String,
pub url: Arc<String>,
}

impl ApiKeyError {
pub fn new(
api_key: ApiKey<'_>,
url: String,
url: Arc<String>,
source: &Arc<NamedSource<String>>,
config: &Config,
) -> Self {
Expand Down Expand Up @@ -101,7 +101,7 @@ impl Serialize for ApiKeyError {
key.serialize_field("secret", &self.secret)?;
key.serialize_field("line", &line)?;
key.serialize_field("column", &column)?;
key.serialize_field("script_url", &self.url)?;
key.serialize_field("script_url", self.url.as_ref())?;

key.end()
}
Expand Down
1 change: 1 addition & 0 deletions src/extract/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod collector;
mod error;
mod extractor;
mod util;
mod visit;

pub use collector::{ApiKeyCollector, ApiKeyMessage, ApiKeyReceiver, ApiKeySender};
Expand Down
72 changes: 72 additions & 0 deletions src/extract/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use std::{borrow::Borrow, ops::Deref, sync::Arc};

/// Nominal type for [`Arc<String>`].
///
/// Needed because [`Arc<String>`] doesn't implement [`AsRef<str>`]
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SyncString(Arc<String>);

impl Deref for SyncString {
type Target = String;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl From<SyncString> for Arc<String> {
fn from(s: SyncString) -> Self {
s.0
}
}

impl From<String> for SyncString {
fn from(s: String) -> Self {
Self(Arc::new(s))
}
}

impl Clone for SyncString {
fn clone(&self) -> Self {
Self(Arc::clone(&self.0))
}
}

impl AsRef<String> for SyncString {
fn as_ref(&self) -> &String {
&self.0
}
}

impl AsRef<str> for SyncString {
fn as_ref(&self) -> &str {
&self.0
}
}

impl Borrow<str> for SyncString {
fn borrow(&self) -> &str {
&self.0
}
}

impl Borrow<String> for SyncString {
fn borrow(&self) -> &String {
&self.0
}
}

impl PartialEq<str> for SyncString {
fn eq(&self, other: &str) -> bool {
self.0.as_ref() == other
}
}

impl serde::Serialize for SyncString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}

0 comments on commit 2499f76

Please sign in to comment.