From ad24f807d4723e13912b8b73f452b7d1beb7f3c8 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Fri, 18 Nov 2022 12:08:50 +0100 Subject: [PATCH] fix: Write SourceMapCache to disk in cabi Instead of holding the SourceMapCache buffer in memory when it is being used via the cabi/python bindings, write it to a tempfile which is automatically cleaned up when the SourceMapCache is dropped. This should reduce memory pressure and allow the OS to page that out. --- symbolic-cabi/Cargo.toml | 1 + symbolic-cabi/src/sourcemapcache.rs | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/symbolic-cabi/Cargo.toml b/symbolic-cabi/Cargo.toml index d4104d90d..2428aaeef 100644 --- a/symbolic-cabi/Cargo.toml +++ b/symbolic-cabi/Cargo.toml @@ -23,3 +23,4 @@ crate-type = ["cdylib"] proguard = { version = "5.0.0", features = ["uuid"] } sourcemap = "6.0.2" symbolic = { version = "10.1.2", path = "../symbolic", features = ["cfi", "debuginfo", "demangle", "sourcemapcache", "symcache"] } +tempfile = "3.1.0" diff --git a/symbolic-cabi/src/sourcemapcache.rs b/symbolic-cabi/src/sourcemapcache.rs index b3d25ac43..680372156 100644 --- a/symbolic-cabi/src/sourcemapcache.rs +++ b/symbolic-cabi/src/sourcemapcache.rs @@ -1,3 +1,4 @@ +use std::io::BufWriter; use std::ops::Deref; use std::os::raw::c_char; use std::ptr; @@ -93,10 +94,13 @@ ffi_fn! { String::from_utf8_lossy(source_slice).deref(), String::from_utf8_lossy(sourcemap_slice).deref() )?; - let mut buffer = Vec::new(); - writer.serialize(&mut buffer)?; - let byteview = ByteView::from_vec(buffer); + let file = tempfile::tempfile()?; + let mut buf_writer = BufWriter::new(file); + writer.serialize(&mut buf_writer)?; + let file = buf_writer.into_inner()?; + + let byteview = ByteView::map_file(file)?; let inner = SelfCell::try_new::(byteview, |data| { let cache = SourceMapCache::parse(&*data)?; Ok(Inner { cache })