atmos/os_lib/ecma/
gc_policy.rs1use alloc::collections::BTreeSet;
8use alloc::vec::Vec;
9
10pub const MIN_INTERVAL: usize = 50_000;
12
13pub fn calc_next_interval_pure(live: usize, used: usize, total: usize) -> usize {
19 if total == 0 {
20 return (MIN_INTERVAL + live / 4).clamp(MIN_INTERVAL, 100_000);
21 }
22 let ratio_percent = (used.saturating_mul(100)) / total;
23 if ratio_percent >= 80 {
24 10_000
26 } else if ratio_percent >= 70 {
27 25_000
29 } else if ratio_percent >= 55 {
30 50_000
32 } else {
33 (MIN_INTERVAL + live / 4).clamp(MIN_INTERVAL, 100_000)
35 }
36}
37
38#[allow(clippy::manual_checked_ops)] pub fn should_collect_pure(made: usize, next_at: usize, used: usize, total: usize) -> bool {
41 if made >= next_at {
42 return true;
43 }
44 if total > 0 {
47 let ratio_percent = (used.saturating_mul(100)) / total;
48 if ratio_percent >= 80 && made >= 2_000 {
49 return true;
50 }
51 if ratio_percent >= 75 && made >= 5_000 {
52 return true;
53 }
54 }
55 false
56}
57
58pub fn find_reachable_dom_nodes<FParent, FChildren, FRef>(
64 total_nodes: usize,
65 mut get_parent: FParent,
66 mut get_children: FChildren,
67 mut is_referenced: FRef,
68) -> BTreeSet<usize>
69where
70 FParent: FnMut(usize) -> Option<usize>,
71 FChildren: FnMut(usize) -> Vec<usize>,
72 FRef: FnMut(usize) -> bool,
73{
74 let mut reachable = BTreeSet::new();
75 if total_nodes == 0 {
76 return reachable;
77 }
78
79 let mut stack = Vec::new();
80
81 stack.push(0);
83 while let Some(i) = stack.pop() {
84 if !reachable.insert(i) {
85 continue;
86 }
87 for c in get_children(i) {
88 if c < total_nodes && !reachable.contains(&c) {
89 stack.push(c);
90 }
91 }
92 }
93
94 for i in 1..total_nodes {
96 if reachable.contains(&i) {
97 continue;
98 }
99 if is_referenced(i) {
100 let mut top = i;
102 let mut guard = 0;
103 while let Some(p) = get_parent(top) {
104 if p >= total_nodes || guard > 2048 {
105 break;
106 }
107 top = p;
108 guard += 1;
109 }
110 stack.clear();
112 stack.push(top);
113 while let Some(curr) = stack.pop() {
114 if !reachable.insert(curr) {
115 continue;
116 }
117 for c in get_children(curr) {
118 if c < total_nodes && !reachable.contains(&c) {
119 stack.push(c);
120 }
121 }
122 }
123 }
124 }
125
126 reachable
127}
128
129pub fn parse_content_frag(attr: Option<&str>) -> Option<usize> {
131 attr.and_then(|s| s.parse::<usize>().ok())
132}
133
134pub fn children_with_links(children: &[usize], shadow_root: Option<usize>, content_frag: Option<usize>) -> Vec<usize> {
140 let mut kids = Vec::with_capacity(children.len() + 2);
141 kids.extend_from_slice(children);
142 if let Some(r) = shadow_root {
143 kids.push(r);
144 }
145 if let Some(f) = content_frag {
146 kids.push(f);
147 }
148 kids
149}