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) => {

View File

@ -4165,8 +4165,8 @@ pub fn with_hole<'a>(
match layout {
Layout::Builtin(Builtin::List(elem_layout)) if elem_layout == Layout::U8 => {
let mut elements = Vec::with_capacity_in(bytes.len(), env.arena);
for byte in bytes {
elements.push(ListLiteralElement::Literal(Literal::Byte(byte)));
for byte in bytes.iter() {
elements.push(ListLiteralElement::Literal(Literal::Byte(*byte)));
}
let expr = Expr::Array {
elem_layout,
@ -4179,7 +4179,9 @@ pub fn with_hole<'a>(
assigned,
Expr::Literal(Literal::Str(
// This is safe because we ensure the utf8 bytes are valid earlier in the compiler pipeline.
arena.alloc(unsafe { std::str::from_utf8_unchecked(&bytes) }.to_owned()),
arena.alloc(
unsafe { std::str::from_utf8_unchecked(bytes.as_ref()) }.to_owned(),
),
)),
Layout::STR,
hole,