Skip to content

Commit 9373449

Browse files
De-intern Sha
1 parent 1575ef4 commit 9373449

File tree

5 files changed

+24
-123
lines changed

5 files changed

+24
-123
lines changed

collector/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use chrono::NaiveDate;
2-
pub use database::{Commit, PatchName, QueryLabel, Sha};
2+
pub use database::{Commit, PatchName, QueryLabel};
33
use serde::Deserialize;
44
use std::cmp::PartialOrd;
55
use std::fmt;

database/src/lib.rs

Lines changed: 3 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -141,106 +141,9 @@ impl<'de> Deserialize<'de> for Date {
141141
}
142142
}
143143

144-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
145-
pub enum Sha {
146-
/// Straight-up bytes of the 40-long hex-encoded sha
147-
Hex([u8; 20]),
148-
/// Usually a string ID provided by the user.
149-
Raw(RawSha),
150-
}
151-
152-
intern!(pub struct RawSha);
153-
154-
impl PartialEq<str> for Sha {
155-
fn eq(&self, other: &str) -> bool {
156-
self.to_string() == other
157-
}
158-
}
159-
160-
fn hex_decode(s: &str) -> Option<[u8; 20]> {
161-
let mut in_progress = 0;
162-
let mut v = [0; 20];
163-
for (idx, ch) in s.chars().enumerate() {
164-
let offset = if idx % 2 == 0 { 4 } else { 0 };
165-
in_progress |= (ch.to_digit(16)? as u8) << offset;
166-
if idx % 2 != 0 {
167-
v[idx / 2] = in_progress;
168-
in_progress = 0;
169-
}
170-
}
171-
Some(v)
172-
}
173-
174-
impl<'a> From<&'a str> for Sha {
175-
fn from(s: &'a str) -> Sha {
176-
if let Some(v) = hex_decode(s) {
177-
return Sha::Hex(v);
178-
}
179-
180-
Sha::Raw(s.into())
181-
}
182-
}
183-
184-
impl Serialize for Sha {
185-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
186-
where
187-
S: serde::ser::Serializer,
188-
{
189-
serializer.collect_str(&self)
190-
}
191-
}
192-
193-
impl<'de> Deserialize<'de> for Sha {
194-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
195-
where
196-
D: serde::de::Deserializer<'de>,
197-
{
198-
use serde::de::Visitor;
199-
struct ShaVisitor;
200-
impl<'de> Visitor<'de> for ShaVisitor {
201-
type Value = Sha;
202-
203-
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
204-
f.write_str("a string")
205-
}
206-
207-
fn visit_str<E>(self, s: &str) -> Result<Sha, E> {
208-
Ok(s.into())
209-
}
210-
211-
fn visit_borrowed_str<E>(self, s: &'de str) -> Result<Sha, E> {
212-
Ok(s.into())
213-
}
214-
}
215-
deserializer.deserialize_str(ShaVisitor)
216-
}
217-
}
218-
219-
impl fmt::Debug for Sha {
220-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
221-
write!(f, "{}", self)
222-
}
223-
}
224-
225-
impl fmt::Display for Sha {
226-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
227-
match *self {
228-
Sha::Hex(hex) => {
229-
for &b in hex.iter() {
230-
write!(f, "{:x}{:x}", b >> 4, b & 0xf)?;
231-
}
232-
}
233-
Sha::Raw(raw) => {
234-
write!(f, "{}", raw)?;
235-
}
236-
}
237-
Ok(())
238-
}
239-
}
240-
241-
#[derive(Debug, Copy, Clone, serde::Deserialize, serde::Serialize)]
144+
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
242145
pub struct Commit {
243-
pub sha: Sha,
146+
pub sha: String,
244147
pub date: Date,
245148
}
246149

@@ -709,7 +612,7 @@ impl Index {
709612
}
710613

711614
pub fn commits(&self) -> Vec<Commit> {
712-
let mut commits = self.commits.map.keys().copied().collect::<Vec<_>>();
615+
let mut commits = self.commits.map.keys().cloned().collect::<Vec<_>>();
713616
commits.sort();
714617
commits
715618
}

site/src/api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub mod data {
108108
}
109109

110110
pub mod graph {
111-
use collector::{Bound, Sha};
111+
use collector::Bound;
112112
use serde::{Deserialize, Serialize};
113113
use std::collections::HashMap;
114114

@@ -135,7 +135,7 @@ pub mod graph {
135135
pub benchmarks: HashMap<String, HashMap<String, Vec<(String, Vec<GraphData>)>>>,
136136
pub max: HashMap<String, f32>,
137137
pub colors: Vec<String>,
138-
pub commits: Vec<Sha>,
138+
pub commits: Vec<String>,
139139
}
140140
}
141141

site/src/load.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use database::Date;
2626
use crate::api::github;
2727
use collector;
2828
use database::Pool;
29-
pub use database::{ArtifactId, Commit, Crate, Sha};
29+
pub use database::{ArtifactId, Commit, Crate};
3030

3131
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
3232
pub enum MissingReason {
@@ -73,7 +73,7 @@ pub struct Keys {
7373
pub struct Config {
7474
pub keys: Keys,
7575
#[serde(default)]
76-
pub skip: HashSet<Sha>,
76+
pub skip: HashSet<String>,
7777
}
7878

7979
pub struct InputData {
@@ -167,8 +167,7 @@ impl InputData {
167167
.cloned()
168168
.filter(|c| now.signed_duration_since(c.time) < Duration::days(29))
169169
.filter_map(|c| {
170-
let sha = c.sha.as_str().into();
171-
if have.contains(&sha) || self.config.skip.contains(&sha) {
170+
if have.contains(&c.sha) || self.config.skip.contains(&c.sha) {
172171
None
173172
} else {
174173
Some((c, MissingReason::Sha))
@@ -208,7 +207,7 @@ impl InputData {
208207
ret
209208
},
210209
)
211-
.filter(|c| !have.contains(&c.0.sha.as_str().into())) // we may have not updated the try-commits file
210+
.filter(|c| !have.contains(&c.0.sha)) // we may have not updated the try-commits file
212211
.chain(missing)
213212
.collect::<Vec<_>>();
214213

site/src/server.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ use crate::interpolate::Interpolated;
4545
use crate::load::{Config, InputData};
4646
use crate::selector::{self, PathComponent, Tag};
4747
use collector::api::collected;
48-
use collector::Sha;
4948
use db::{ArtifactId, Lookup};
5049
use parking_lot::RwLock;
5150

@@ -251,12 +250,12 @@ fn prettify_log(log: &str) -> Option<String> {
251250

252251
pub async fn handle_status_page(data: Arc<InputData>) -> status::Response {
253252
let idx = data.index.load();
254-
let last_commit = *idx.commits().last().unwrap();
253+
let last_commit = idx.commits().last().unwrap().clone();
255254

256255
let mut benchmark_state = data
257256
.conn()
258257
.await
259-
.get_error(ArtifactId::from(last_commit).lookup(&idx).unwrap())
258+
.get_error(ArtifactId::from(last_commit.clone()).lookup(&idx).unwrap())
260259
.await
261260
.into_iter()
262261
.map(|(name, error)| {
@@ -300,8 +299,8 @@ pub async fn handle_next_commit(data: Arc<InputData>) -> collector::api::next_co
300299
}
301300

302301
struct CommitIdxCache {
303-
commit_idx: RefCell<HashMap<Sha, u16>>,
304-
commits: RefCell<Vec<Sha>>,
302+
commit_idx: RefCell<HashMap<String, u16>>,
303+
commits: RefCell<Vec<String>>,
305304
}
306305

307306
impl CommitIdxCache {
@@ -312,15 +311,15 @@ impl CommitIdxCache {
312311
}
313312
}
314313

315-
fn into_commits(self) -> Vec<Sha> {
314+
fn into_commits(self) -> Vec<String> {
316315
std::mem::take(&mut *self.commits.borrow_mut())
317316
}
318317

319-
fn lookup(&self, commit: Sha) -> u16 {
318+
fn lookup(&self, commit: String) -> u16 {
320319
*self
321320
.commit_idx
322321
.borrow_mut()
323-
.entry(commit)
322+
.entry(commit.clone())
324323
.or_insert_with(|| {
325324
let idx = self.commits.borrow().len();
326325
self.commits.borrow_mut().push(commit);
@@ -365,7 +364,7 @@ fn to_graph_data<'a>(
365364
pub async fn handle_graph(body: graph::Request, data: &InputData) -> ServerResult<graph::Response> {
366365
let cc = CommitIdxCache::new();
367366
let range = data.data_range(body.start.clone()..=body.end.clone());
368-
let commits: Arc<Vec<_>> = Arc::new(range.iter().map(|&c| c.into()).collect());
367+
let commits: Arc<Vec<_>> = Arc::new(range.iter().map(|c| c.clone().into()).collect());
369368

370369
let stat_selector = selector::Selector::One(body.stat.clone());
371370

@@ -556,13 +555,13 @@ impl DateData {
556555
}
557556

558557
DateData {
559-
date: if let ArtifactId::Commit(c) = commit {
558+
date: if let ArtifactId::Commit(c) = &commit {
560559
Some(c.date)
561560
} else {
562561
None
563562
},
564563
commit: match commit {
565-
ArtifactId::Commit(c) => c.sha.to_string(),
564+
ArtifactId::Commit(c) => c.sha,
566565
ArtifactId::Artifact(i) => i,
567566
},
568567
data,
@@ -704,9 +703,9 @@ pub async fn handle_self_profile(
704703

705704
let mut commits = vec![index
706705
.commits()
707-
.iter()
706+
.into_iter()
708707
.find(|c| c.sha == *body.commit.as_str())
709-
.map(|c| database::ArtifactId::Commit(*c))
708+
.map(|c| database::ArtifactId::Commit(c))
710709
.or_else(|| {
711710
index
712711
.artifacts()
@@ -719,9 +718,9 @@ pub async fn handle_self_profile(
719718
commits.push(
720719
index
721720
.commits()
722-
.iter()
721+
.into_iter()
723722
.find(|c| c.sha == *bc.as_str())
724-
.map(|c| database::ArtifactId::Commit(*c))
723+
.map(|c| database::ArtifactId::Commit(c))
725724
.or_else(|| {
726725
index
727726
.artifacts()

0 commit comments

Comments
 (0)