Skip to content

Truly independent web browser

LibWeb: Let Blobs and Files count their bytes as external memory

Andreas Kling8b03aa4e4b9d
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
index 2c51216c27d..6b230d34799 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
@@ -51,6 +51,12 @@ pub(crate) trait Wrappable {
/// implementation object.
const OWNS_EXTERNAL_MEMORY: bool = false;
+ /// Whether the memory outside the heap that the object owns was made with it, so that allocating the object counts
+ /// it towards the next collection. Not so for an object that reports memory another object holds as well, as a CSS
+ /// rule does the rule its style sheet holds: counting it each time a new one is made would start collections that
+ /// free none of it.
+ const COUNTS_EXTERNAL_MEMORY_WHEN_ALLOCATED: bool = false;
+
/// How much memory outside the heap the object owns, which LibGC asks for if OWNS_EXTERNAL_MEMORY is true.
fn external_memory_size(&self) -> usize {
0
@@ -173,10 +179,19 @@ impl<T: Wrappable + Trace + Sized + 'static> Interface for T {
/// A new object of the interface, built as Heap::allocate() builds a cell.
pub(crate) fn allocate<T: Interface>(vm: &Vm, build: impl FnOnce(&DeferGc<'_>) -> T) -> Ref<T> {
- T::CELL_CLASS.allocate(vm, |defer_gc| Wrapped {
+ let object = T::CELL_CLASS.allocate(vm, |defer_gc| Wrapped {
main_world_wrapper: Cell::new(None),
implementation: build(defer_gc),
- })
+ });
+ // NB: The memory the object owns outside the heap counts towards the next collection too, or a page could make
+ // many objects that each own a lot of it, and keep the dead ones until its other allocations started one.
+ if T::OWNS_EXTERNAL_MEMORY && T::COUNTS_EXTERNAL_MEMORY_WHEN_ALLOCATED {
+ let size = object.external_memory_size();
+ if size != 0 {
+ vm.heap().did_allocate_external_memory(size);
+ }
+ }
+ object
}
/// What the constructor steps or the transfer-receiving steps of an interface create: the implementation of the new
diff --git a/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs b/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs
index a8d93f99449..94008156e68 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs
@@ -155,7 +155,17 @@ pub(crate) struct Blob {
r#type: Utf16String,
}
-impl Wrappable for Blob {}
+impl Wrappable for Blob {
+ const OWNS_EXTERNAL_MEMORY: bool = true;
+ const COUNTS_EXTERNAL_MEMORY_WHEN_ALLOCATED: bool = true;
+
+ fn external_memory_size(&self) -> usize {
+ match &*self.byte_sequence.borrow() {
+ ByteSequence::Owned(bytes) => bytes.capacity(),
+ ByteSequence::Foreign(bytes) => bytes.as_slice().len(),
+ }
+ }
+}
fn append_utf8(bytes: &mut Vec<u8>, string: &UsvString) {
let length = string.utf8_length();
diff --git a/Libraries/LibWeb/Bindings/Rust/src/file_api/file.rs b/Libraries/LibWeb/Bindings/Rust/src/file_api/file.rs
index dd2fcb161ed..22b075a791e 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/file_api/file.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/file_api/file.rs
@@ -35,7 +35,14 @@ pub(crate) struct File {
last_modified: i64,
}
-impl Wrappable for File {}
+impl Wrappable for File {
+ const OWNS_EXTERNAL_MEMORY: bool = true;
+ const COUNTS_EXTERNAL_MEMORY_WHEN_ALLOCATED: bool = true;
+
+ fn external_memory_size(&self) -> usize {
+ self.blob.external_memory_size()
+ }
+}
impl Inherits<Blob> for File {
fn parent(&self) -> &Blob {