From 59056cf7056cdda4352da7f99ac4fddd345a54bf Mon Sep 17 00:00:00 2001 From: Aspen Smith Date: Sun, 28 Jul 2024 11:37:50 -0400 Subject: feat(tvix/eval): Leak strings (with flag to disable) Default to always leaking strings, and copying strings by copying pointers rather than cloning the underlying allocation. This (somewhat bafflingly) doesn't seem to affect any benchmarks, but paves the way for some tricks around string allocation that do. Unfortunately, we can't do this (yet?) for contextful strings, for reasons I don't currently understand but which I will address later, when I address contextful strings more holistically. I've left a flag in here to disable this, both to test the cloning logic for strings for when/if we decide to bring this back, and to allow people who care more about memory usage than perf to disable leaking. Change-Id: Iec44bcbfe9b3d20389d2450b9a551792a79b9b26 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12045 Autosubmit: aspen Tested-by: BuildkiteCI Reviewed-by: flokli --- tvix/eval/src/value/string.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/value/string.rs b/tvix/eval/src/value/string.rs index 163e140a19c4..3e991890ea5a 100644 --- a/tvix/eval/src/value/string.rs +++ b/tvix/eval/src/value/string.rs @@ -409,6 +409,10 @@ unsafe impl Sync for NixString {} impl Drop for NixString { fn drop(&mut self) { + if !cfg!(feature = "no_leak") { + return; + } + // SAFETY: There's no way to construct a NixString that doesn't leave the allocation correct // according to the rules of dealloc unsafe { @@ -419,9 +423,17 @@ impl Drop for NixString { impl Clone for NixString { fn clone(&self) -> Self { - // SAFETY: There's no way to construct a NixString that doesn't leave the allocation correct - // according to the rules of clone - unsafe { Self(NixStringInner::clone(self.0)) } + if cfg!(feature = "no_leak") || self.context().is_some() { + // SAFETY: There's no way to construct a NixString that doesn't leave the allocation correct + // according to the rules of clone + unsafe { Self(NixStringInner::clone(self.0)) } + } else { + // SAFETY: + // + // - NixStrings are never mutated + // - NixStrings are never freed + Self(self.0) + } } } -- cgit 1.4.1