use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use bytes::Bytes;
use crate::budget::DecodeBuf;
use crate::error::LimitKind;
use crate::limits::{ChargeError, Meter, MeteredVec};
use crate::raw::RawStr;
use crate::typetree::NodeRow;
pub const UNPIN_MIN: u64 = 1 << 20;
#[must_use]
pub const fn worth_copying(pinned: u64, s: u64) -> bool {
let four = s.saturating_mul(4);
let floor = if four > UNPIN_MIN { four } else { UNPIN_MIN };
s > 0 && pinned.saturating_sub(s) >= floor
}
pub(crate) enum Slice<'a> {
Str(&'a mut RawStr),
Bytes(&'a mut Bytes),
}
impl Slice<'_> {
fn bytes(&self) -> &[u8] {
match self {
Self::Str(s) => s.as_bytes(),
Self::Bytes(b) => b,
}
}
fn set(&mut self, to: Bytes) {
match self {
Self::Str(s) => **s = RawStr::from_shared(to),
Self::Bytes(b) => **b = to,
}
}
}
pub(crate) trait Slices {
fn each(&mut self, f: &mut dyn FnMut(Slice<'_>));
}
impl Slices for MeteredVec<NodeRow> {
fn each(&mut self, f: &mut dyn FnMut(Slice<'_>)) {
for row in self.as_mut_slice() {
f(Slice::Str(&mut row.ty));
f(Slice::Str(&mut row.name));
}
}
}
fn len64(n: usize) -> u64 {
u64::try_from(n).unwrap_or(u64::MAX)
}
fn place(within: &[u8], b: &[u8]) -> Option<(usize, usize)> {
let at = b.as_ptr().addr().checked_sub(within.as_ptr().addr())?;
let end = at.checked_add(b.len())?;
(!b.is_empty() && end <= within.len()).then_some((at, end))
}
fn merge(spans: &mut [(usize, usize)]) -> usize {
spans.sort_unstable();
let mut kept = 0usize;
for i in 0..spans.len() {
let Some(&(from, to)) = spans.get(i) else {
break;
};
if let Some(last) = kept.checked_sub(1).and_then(|k| spans.get_mut(k))
&& from <= last.1
{
last.1 = last.1.max(to);
continue;
}
if let Some(slot) = spans.get_mut(kept) {
*slot = (from, to);
}
kept += 1;
}
kept
}
pub(crate) fn copy_out(
target: &mut dyn Slices,
within: &[u8],
pinned: u64,
meter: &Arc<Meter>,
) -> u64 {
try_copy_out(target, within, pinned, meter, false).unwrap_or(0)
}
pub(crate) fn try_copy_out(
target: &mut dyn Slices,
within: &[u8],
pinned: u64,
meter: &Arc<Meter>,
count_work: bool,
) -> Result<u64, ChargeError> {
if pinned <= UNPIN_MIN {
return Ok(0);
}
let mut n = 0usize;
target.each(&mut |sl| n += usize::from(place(within, sl.bytes()).is_some()));
if n == 0 {
return Ok(0);
}
let mut table = MeteredVec::with_capacity(meter, n)?;
let mut listed = Ok(());
target.each(&mut |sl| {
if listed.is_ok()
&& let Some(span) = place(within, sl.bytes())
{
listed = table.push(span);
}
});
listed?;
if count_work {
meter.count_decode(len64(n).saturating_mul(u64::from(n.ilog2() + 1)))?;
}
let kept = merge(table.as_mut_slice());
let Some(spans) = table.as_mut_slice().get_mut(..kept) else {
return Ok(0);
};
let s = spans
.iter()
.fold(0u64, |s, (from, to)| s.saturating_add(len64(to - from)));
if !worth_copying(pinned, s) {
return Ok(0);
}
if count_work {
meter.count_decode(s)?;
}
let mut buf = DecodeBuf::new(meter, s, s, LimitKind::InputMemory)?;
for span in spans.iter_mut() {
let Some(bytes) = within.get(span.0..span.1) else {
return Ok(0);
};
buf.extend_from_slice(bytes)?;
span.1 = buf.len() - (span.1 - span.0);
}
let copy = buf.into_bytes()?;
target.each(&mut |mut sl| {
if let Some((from, to)) = place(within, sl.bytes()) {
let i = spans.partition_point(|&(start, _)| start <= from);
if let Some(&(start, at)) = i.checked_sub(1).and_then(|i| spans.get(i)) {
let at = at + (from - start);
sl.set(copy.slice(at..at + (to - from)));
}
}
});
Ok(s)
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Copies {
pub values: u64,
pub value_bytes: u64,
pub trees: u64,
pub tree_bytes: u64,
}
const _: () = crate::send_sync::<Copies>();
#[derive(Debug, Default)]
pub(crate) struct CopyCounts {
values: AtomicU64,
value_bytes: AtomicU64,
trees: u64,
tree_bytes: u64,
}
impl CopyCounts {
pub(crate) fn new(trees: u64, bytes: u64) -> Self {
let on = cfg!(not(unityrs_no_counters));
Self {
trees: if on { trees } else { 0 },
tree_bytes: if on { bytes } else { 0 },
..Self::default()
}
}
pub(crate) fn value(&self, bytes: u64) {
if cfg!(not(unityrs_no_counters)) && bytes > 0 {
self.values.fetch_add(1, Ordering::Relaxed);
self.value_bytes.fetch_add(bytes, Ordering::Relaxed);
}
}
pub(crate) fn get(&self) -> Copies {
Copies {
values: self.values.load(Ordering::Relaxed),
value_bytes: self.value_bytes.load(Ordering::Relaxed),
trees: self.trees,
tree_bytes: self.tree_bytes,
}
}
}
#[cfg(test)]
mod tests;