use Arc to avoid ever needing to actually copy the underlying bytes

This commit is contained in:
Brendan Hansknecht 2023-04-07 16:55:41 -07:00
parent a354860d07
commit 90f4885f4b
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
3 changed files with 14 additions and 12 deletions

View file

@ -1,5 +1,6 @@
use std::cell::Cell;
use std::path::Path;
use std::sync::Arc;
use crate::abilities::SpecializationId;
use crate::exhaustive::{ExhaustiveContext, SketchedRows};
@ -680,14 +681,14 @@ impl Constraints {
&mut self,
type_index: TypeOrVar,
file_path: Box<Path>,
bytes: Vec<u8>,
bytes: Arc<Vec<u8>>,
) -> Constraint {
Constraint::IngestedFile(type_index, file_path, bytes)
}
}
roc_error_macros::assert_sizeof_default!(Constraint, 6 * 8);
roc_error_macros::assert_sizeof_aarch64!(Constraint, 6 * 8);
roc_error_macros::assert_sizeof_default!(Constraint, 4 * 8);
roc_error_macros::assert_sizeof_aarch64!(Constraint, 4 * 8);
impl std::ops::Index<ExpectedTypeIndex> for Constraints {
type Output = Expected<TypeOrVar>;
@ -785,9 +786,7 @@ pub enum Constraint {
Resolve(OpportunisticResolve),
CheckCycle(Index<Cycle>, IllegalCycleMark),
// This is terrible and could be a huge cost to copy.
// Not sure a better way to get the bytes here so we can check if they are valid utf8 or decode properly.
IngestedFile(TypeOrVar, Box<Path>, Vec<u8>),
IngestedFile(TypeOrVar, Box<Path>, Arc<Vec<u8>>),
}
#[derive(Debug, Clone, Copy, Default)]

View file

@ -30,6 +30,7 @@ use std::fmt::{Debug, Display};
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::sync::Arc;
use std::{char, u32};
/// Derives that an opaque type has claimed, to checked and recorded after solving.
@ -103,8 +104,8 @@ pub enum Expr {
loc_elems: Vec<Loc<Expr>>,
},
// The bytes of a file and the expected type annotation.
IngestedFile(Box<Path>, Vec<u8>, Variable),
// An ingested files, it's bytes, and the type variable.
IngestedFile(Box<Path>, Arc<Vec<u8>>, Variable),
// Lookups
Var(Symbol, Variable),
@ -741,7 +742,7 @@ pub fn canonicalize_expr<'a>(
let mut bytes = vec![];
match file.read_to_end(&mut bytes) {
Ok(_) => (
Expr::IngestedFile((*file_path).into(), bytes, var_store.fresh()),
Expr::IngestedFile((*file_path).into(), Arc::new(bytes), var_store.fresh()),
Output::default(),
),
Err(e) => {