Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/collections/array_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,23 @@ impl<V, A: Allocator + HashbrownAllocator + Clone + Default> StringHashMap<V, A>
pub fn put_assume_capacity(&mut self, key: &[u8], value: V) {
let _ = self.put(key, value);
}

pub fn get_or_put_value(&mut self, key: &[u8], value: V) -> Result<&mut V, AllocError> {
use hashbrown::hash_map::RawEntryMut;
let hash = self.hash_key(key);
Ok(
match self
.inner
.raw_entry_mut()
.from_key_hashed_nocheck(hash, key)
{
RawEntryMut::Occupied(e) => e.into_mut(),
RawEntryMut::Vacant(e) => {
e.insert_hashed_nocheck(hash, owned_key::<A>(key), value).1
}
},
)
}
}

/// `StringHashMap::get_or_put` result — `std::HashMap` cannot hand out
Expand Down Expand Up @@ -1835,23 +1852,6 @@ impl<V: Default, A: Allocator + HashbrownAllocator + Clone + Default> StringHash
},
)
}

pub fn get_or_put_value(&mut self, key: &[u8], value: V) -> Result<&mut V, AllocError> {
use hashbrown::hash_map::RawEntryMut;
let hash = self.hash_key(key);
Ok(
match self
.inner
.raw_entry_mut()
.from_key_hashed_nocheck(hash, key)
{
RawEntryMut::Occupied(e) => e.into_mut(),
RawEntryMut::Vacant(e) => {
e.insert_hashed_nocheck(hash, owned_key::<A>(key), value).1
}
},
)
}
}

// ──────────────────────────────────────────────────────────────────────────
Expand Down
47 changes: 10 additions & 37 deletions src/install/PackageManager/WorkspacePackageJSONCache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use crate::bun_json as json;
use crate::initialize_store;

pub struct MapEntry {
/// The nodes and the escape-decoded strings are in the process heap and are
/// never freed, like the `AstVec` buffers inside them: a mimalloc heap for
/// each entry costs 30 KB or more. A node spliced in later must live as long.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub root: Expr,
pub source: Source,
pub indentation: Indentation,
Expand All @@ -27,46 +30,21 @@ pub struct MapEntry {
/// so the source's path slices stay valid for the entry's lifetime.
/// `StringHashMap` boxes its own key, so keep the duped copy alive here.
_path_storage: bun_core::ZBox,
/// Owns the arena that backs decoded string bytes inside `root`.
/// `deepClone` does *not* dupe escape-decoded `E.String.data` slices.
/// The parser takes a `&Arena`, so the arena must outlive the
/// cached AST — hold it here so it drops with the entry.
///
/// Public so editors that splice new `Expr` nodes into `root`
/// (e.g. `update_interactive_command::update_package_json_files_from_updates`)
/// can allocate those nodes here instead of in the resettable `Store` —
/// the cached `root` outlives `initialize_store()` resets.
pub json_arena: bun_alloc::Arena,
/// Superseded `source.contents` buffers, pinned so cached `root` slices
/// stay valid; freed when the entry drops.
pub stale_contents: Vec<std::borrow::Cow<'static, [u8]>>,
}

impl Default for MapEntry {
fn default() -> Self {
Self {
root: Expr::default(),
source: Source::default(),
indentation: Indentation::default(),
indentation_guessed: false,
_path_storage: bun_core::ZBox::default(),
json_arena: bun_alloc::Arena::new(),
stale_contents: Vec::new(),
}
}
}

impl MapEntry {
/// Re-parse `self.source.contents` into `self.root`.
///
/// `updatePackageJSONAndInstall` edits a copy of `root`, prints it, and
/// writes the printed JSON back into `source.contents`. The caller then
/// invokes this to restore the invariant `root == parse(source)`.
pub(crate) fn reparse_root(&mut self, log: &mut Log) -> Result<(), Error> {
let json_bump = bun_alloc::Arena::new();
let parsed = parse_package_json(&self.source, log, &json_bump, false)?;
self.root = bun_core::handle_oom(parsed.root.deep_clone(&json_bump));
self.json_arena = json_bump;
let heap = bun_alloc::Arena::borrowing_default();
let parsed = parse_package_json(&self.source, log, &heap, false)?;
self.root = bun_core::handle_oom(parsed.root.deep_clone(&heap));
Ok(())
}
}
Expand Down Expand Up @@ -185,30 +163,25 @@ impl WorkspacePackageJSONCache {
initialize_store();
}

let json_bump = bun_alloc::Arena::new();
let parsed = match parse_package_json(&source, log, &json_bump, opts.guess_indentation) {
let heap = bun_alloc::Arena::borrowing_default();
let parsed = match parse_package_json(&source, log, &heap, opts.guess_indentation) {
Ok(p) => p,
Err(err) => {
return GetResult::ParseErr(err);
}
};

let value = MapEntry {
root: bun_core::handle_oom(parsed.root.deep_clone(&json_bump)),
root: bun_core::handle_oom(parsed.root.deep_clone(&heap)),
source,
indentation: parsed.indentation,
indentation_guessed: opts.guess_indentation,
// `source.path` borrows this allocation; the `Box<[u8]>` heap
// address is stable across the move into the map.
_path_storage: key,
json_arena: json_bump,
stale_contents: Vec::new(),
};

let entry = bun_core::handle_oom(self.map.get_or_put(path));
debug_assert!(!entry.found_existing);
*entry.value_ptr = value;

GetResult::Entry(entry.value_ptr)
GetResult::Entry(bun_core::handle_oom(self.map.get_or_put_value(path, value)))
}
}
77 changes: 29 additions & 48 deletions src/install/lockfile/Package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,23 @@ pub use workspace_map as WorkspaceMap;

bun_output::declare_scope!(Lockfile, hidden);

trait ExprStr {
fn as_utf8<'b>(&self, bump: &'b bun_alloc::Arena) -> Option<&'b [u8]>;
}
impl ExprStr for Expr {
#[inline]
fn as_utf8<'b>(&self, bump: &'b bun_alloc::Arena) -> Option<&'b [u8]> {
if let ExprData::EString(s) = &self.data {
return Some(s.string(bump).expect("OOM"));
}
None
}
}

enum JsonObjectStringRows<'a> {
Classic(
core::slice::Iter<'a, bun_ast::G::Property>,
&'a bun_alloc::Arena,
),
Classic(core::slice::Iter<'a, bun_ast::G::Property>),
Json(core::slice::Iter<'a, E::PropertyJSON>),
}

impl<'a> JsonObjectStringRows<'a> {
fn new(expr: &'a Expr, bump: &'a bun_alloc::Arena) -> Option<Self> {
fn new(expr: &'a Expr) -> Option<Self> {
match &expr.data {
ExprData::EObject(obj) => Some(Self::Classic(obj.properties.slice().iter(), bump)),
ExprData::EObject(obj) => Some(Self::Classic(obj.properties.slice().iter())),
ExprData::EObjectJSON(obj) => Some(Self::Json(obj.get().properties().iter())),
_ => None,
}
}

fn len(&self) -> usize {
match self {
Self::Classic(iter, _) => iter.len(),
Self::Classic(iter) => iter.len(),
Self::Json(iter) => iter.len(),
}
}
Expand All @@ -89,13 +73,12 @@ impl<'a> Iterator for JsonObjectStringRows<'a> {

fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Classic(iter, bump) => {
Self::Classic(iter) => {
let prop = iter.next()?;
let key = prop.key?;
let key_bytes = key.as_utf8(*bump)?;
let key = prop.key.as_ref()?;
Some((
key_bytes,
prop.value.as_ref().and_then(|v| v.as_utf8(*bump)),
key.as_utf8_string_literal()?,
prop.value.as_ref().and_then(|v| v.as_utf8_string_literal()),
key.loc,
))
}
Expand Down Expand Up @@ -2170,9 +2153,6 @@ impl Package<u64> {
) -> crate::Result<()> {
#[allow(non_snake_case)]
let FEATURES = features;
// Function-local arena for `asString` transcoding (transcoded strings
// are only borrowed until `string_builder.append` copies them).
let bump = bun_alloc::Arena::new();
// split-borrow `string_bytes`/`string_pool` so the dozens of
// disjoint `lockfile.{buffers.*, overrides, catalogs, workspace_*, …}`
// accesses below pass borrowck. Reads of `lockfile.buffers.string_bytes`
Expand All @@ -2191,7 +2171,7 @@ impl Package<u64> {
// -- Count the sizes
'name: {
if let Some(name_q) = json.as_property(b"name") {
if let Some(name) = name_q.expr.as_utf8(&bump) {
if let Some(name) = name_q.expr.as_utf8_string_literal() {
if !name.is_empty() {
string_builder.count(name);
break 'name;
Expand Down Expand Up @@ -2220,7 +2200,7 @@ impl Package<u64> {

if FEATURES.patched_dependencies {
if let Some(patched_deps) = json.as_property(b"patchedDependencies") {
if let Some(rows) = JsonObjectStringRows::new(&patched_deps.expr, &bump) {
if let Some(rows) = JsonObjectStringRows::new(&patched_deps.expr) {
for (_, value, _) in rows {
if let Some(value) = value {
string_builder.count(value);
Expand All @@ -2232,14 +2212,14 @@ impl Package<u64> {

if !FEATURES.is_main {
if let Some(version_q) = json.as_property(b"version") {
if let Some(version_str) = version_q.expr.as_utf8(&bump) {
if let Some(version_str) = version_q.expr.as_utf8_string_literal() {
string_builder.count(version_str);
}
}
}
'bin: {
if let Some(bin) = json.as_property(b"bin") {
if let Some(rows) = JsonObjectStringRows::new(&bin.expr, &bump) {
if let Some(rows) = JsonObjectStringRows::new(&bin.expr) {
for (k, v, _) in rows {
string_builder.count(k);
let Some(v) = v else {
Expand All @@ -2250,7 +2230,7 @@ impl Package<u64> {
break 'bin;
}
if bin.expr.is_string() {
if let Some(str_) = bin.expr.as_utf8(&bump) {
if let Some(str_) = bin.expr.as_utf8_string_literal() {
// The build pass reads `directories.bin` when `bin` is empty.
if !str_.is_empty() {
string_builder.count(str_);
Expand All @@ -2262,7 +2242,7 @@ impl Package<u64> {

if let Some(dirs) = json.as_property(b"directories") {
if let Some(bin_prop) = dirs.expr.as_property(b"bin") {
if let Some(str_) = bin_prop.expr.as_utf8(&bump) {
if let Some(str_) = bin_prop.expr.as_utf8_string_literal() {
string_builder.count(str_);
break 'bin;
}
Expand Down Expand Up @@ -2305,7 +2285,7 @@ impl Package<u64> {
// names that nothing in the build loop consumed.
let mut optional_peer_dependencies: ArrayHashMap<
PackageNameHash,
&[u8],
Box<[u8]>,
bun_collections::identity_context::U64,
> = ArrayHashMap::default();

Expand All @@ -2326,9 +2306,10 @@ impl Package<u64> {
return;
}

let key: &[u8] = bump.alloc_slice_copy(key);
optional_peer_dependencies
.put_assume_capacity(semver::string::Builder::string_hash(key), key);
optional_peer_dependencies.put_assume_capacity(
semver::string::Builder::string_hash(key),
Box::from(key),
);
string_builder.count(key);
string_builder.count(b"*");
total_dependencies_count += 1;
Expand Down Expand Up @@ -2373,7 +2354,7 @@ impl Package<u64> {
break 'brk;
}

if let Some(rows) = JsonObjectStringRows::new(&dependencies_q.expr, &bump) {
if let Some(rows) = JsonObjectStringRows::new(&dependencies_q.expr) {
if group.behavior.is_workspace() {
// yarn workspaces expects a "workspaces" property shaped like this:
//
Expand Down Expand Up @@ -2435,7 +2416,7 @@ impl Package<u64> {
let mut owned: Vec<Vec<u8>> = Vec::new();
if let Some(mut items) = q.expr.as_array() {
while let Some(item) = items.next() {
let Some(s) = item.as_string(&bump) else {
let Some(s) = item.as_utf8_string_literal() else {
let _ = log.add_error_fmt(
source,
item.loc,
Expand Down Expand Up @@ -2527,7 +2508,7 @@ impl Package<u64> {
trusted.ensure_unused_capacity(count)?;
if let Some(mut items) = q.expr.as_array() {
while let Some(item) = items.next() {
let Some(name) = item.as_string(&bump) else {
let Some(name) = item.as_utf8_string_literal() else {
return Err(invalid_trusted_dependencies(log, source, q.loc));
};
trusted.put_assume_capacity(
Expand Down Expand Up @@ -2606,7 +2587,7 @@ impl Package<u64> {
}

if let Some(name_q) = json.as_property(b"name") {
if let Some(name) = name_q.expr.as_utf8(&bump) {
if let Some(name) = name_q.expr.as_utf8_string_literal() {
if !name.is_empty() {
let external_string = string_builder.append::<ExternalString>(name);

Expand All @@ -2628,7 +2609,7 @@ impl Package<u64> {

if FEATURES.patched_dependencies {
if let Some(patched_deps) = json.as_property(b"patchedDependencies") {
if let Some(rows) = JsonObjectStringRows::new(&patched_deps.expr, &bump) {
if let Some(rows) = JsonObjectStringRows::new(&patched_deps.expr) {
lockfile
.patched_dependencies
.ensure_total_capacity(rows.len())
Expand Down Expand Up @@ -2656,7 +2637,7 @@ impl Package<u64> {

'bin: {
if let Some(bin) = json.as_property(b"bin") {
if let Some(mut rows) = JsonObjectStringRows::new(&bin.expr, &bump) {
if let Some(mut rows) = JsonObjectStringRows::new(&bin.expr) {
match rows.len() {
0 => {}
1 => {
Expand Down Expand Up @@ -2732,7 +2713,7 @@ impl Package<u64> {
// the files in an existing bin directory, use
// directories.bin.
if let Some(bin_prop) = dirs.expr.as_property(b"bin") {
if let Some(str_) = bin_prop.expr.as_utf8(&bump) {
if let Some(str_) = bin_prop.expr.as_utf8_string_literal() {
if !str_.is_empty() {
self.bin = Bin {
tag: bin::Tag::Dir,
Expand Down Expand Up @@ -2774,7 +2755,7 @@ impl Package<u64> {
_ => {
if let Some(mut items) = bundled_deps_expr.as_array() {
while let Some(item) = items.next() {
let Some(s) = item.as_string(&bump) else {
let Some(s) = item.as_utf8_string_literal() else {
continue;
};
bundled_deps.insert(s)?;
Expand Down Expand Up @@ -2977,7 +2958,7 @@ impl Package<u64> {
}
} else {
if let Some(dependencies_q) = json.as_property(group.prop) {
let rows = JsonObjectStringRows::new(&dependencies_q.expr, &bump)
let rows = JsonObjectStringRows::new(&dependencies_q.expr)
.expect("validated above: a dependency group is an object");
for (key, version, key_loc) in rows {
let external_name = string_builder.append::<ExternalString>(key);
Expand Down Expand Up @@ -3032,7 +3013,7 @@ impl Package<u64> {
// `peerDependencies`.
let meta_only = optional_peer_dependencies.iterator();
for entry in meta_only {
let external_name = string_builder.append::<ExternalString>(*entry.value_ptr);
let external_name = string_builder.append::<ExternalString>(entry.value_ptr);
if let Some(dep_) = Self::parse_dependency(
&mut lockfile.workspace_paths,
&mut lockfile.workspace_versions,
Expand Down
Loading
Loading