dissolve-python 0.3.0

A tool to dissolve deprecated calls in Python codebases
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Copyright (C) 2024 Jelmer Vernooij <jelmer@samba.org>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Performance optimizations including caching and string interning

use rustpython_ast as ast;
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::{Arc, Mutex, RwLock};

/// String interning system for reducing memory usage
pub struct StringInterner {
    strings: RwLock<HashMap<String, Arc<str>>>,
}

impl StringInterner {
    pub fn new() -> Self {
        Self {
            strings: RwLock::new(HashMap::new()),
        }
    }

    /// Intern a string, returning an Arc to the shared copy
    pub fn intern(&self, s: &str) -> Arc<str> {
        // First try to read and see if we already have it
        if let Ok(map) = self.strings.read() {
            if let Some(interned) = map.get(s) {
                return Arc::clone(interned);
            }
        }

        // Need to insert it - get write lock
        let mut map = self.strings.write().unwrap();
        if let Some(interned) = map.get(s) {
            // Someone else might have inserted it while we were waiting for write lock
            Arc::clone(interned)
        } else {
            let arc: Arc<str> = Arc::from(s);
            map.insert(s.to_string(), Arc::clone(&arc));
            arc
        }
    }

    /// Get statistics about the interner
    pub fn stats(&self) -> InternerStats {
        let map = self.strings.read().unwrap();
        InternerStats {
            unique_strings: map.len(),
            total_bytes_saved: map.iter().map(|(k, _)| k.len()).sum::<usize>()
                * (std::mem::size_of::<String>() - std::mem::size_of::<Arc<str>>()),
        }
    }
}

impl Default for StringInterner {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug, Clone)]
pub struct InternerStats {
    pub unique_strings: usize,
    pub total_bytes_saved: usize,
}

/// Cache for parsed ASTs to avoid re-parsing the same files
pub struct AstCache {
    cache: RwLock<HashMap<String, CacheEntry>>,
    max_entries: usize,
}

#[derive(Clone)]
struct CacheEntry {
    ast: ast::Mod,
    last_modified: std::time::SystemTime,
    hit_count: usize,
}

impl AstCache {
    pub fn new(max_entries: usize) -> Self {
        Self {
            cache: RwLock::new(HashMap::new()),
            max_entries,
        }
    }

    /// Get an AST from cache if it exists and is up to date
    pub fn get(&self, file_path: &str, file_modified: std::time::SystemTime) -> Option<ast::Mod> {
        let mut cache = self.cache.write().ok()?;

        if let Some(entry) = cache.get_mut(file_path) {
            if entry.last_modified >= file_modified {
                entry.hit_count += 1;
                return Some(entry.ast.clone());
            } else {
                // File was modified, remove stale entry
                cache.remove(file_path);
            }
        }
        None
    }

    /// Store an AST in the cache
    pub fn insert(&self, file_path: String, ast: ast::Mod, file_modified: std::time::SystemTime) {
        let mut cache = self.cache.write().unwrap();

        // If we're at capacity, remove the least recently used entry
        if cache.len() >= self.max_entries {
            if let Some(lru_key) = cache
                .iter()
                .min_by_key(|(_, entry)| entry.hit_count)
                .map(|(k, _)| k.clone())
            {
                cache.remove(&lru_key);
            }
        }

        cache.insert(
            file_path,
            CacheEntry {
                ast,
                last_modified: file_modified,
                hit_count: 0,
            },
        );
    }

    /// Get cache statistics
    pub fn stats(&self) -> CacheStats {
        let cache = self.cache.read().unwrap();
        let total_hits: usize = cache.values().map(|e| e.hit_count).sum();

        CacheStats {
            entries: cache.len(),
            total_hits,
            hit_ratio: if cache.len() > 0 {
                total_hits as f64 / cache.len() as f64
            } else {
                0.0
            },
        }
    }

    /// Clear the cache
    pub fn clear(&self) {
        self.cache.write().unwrap().clear();
    }
}

#[derive(Debug, Clone)]
pub struct CacheStats {
    pub entries: usize,
    pub total_hits: usize,
    pub hit_ratio: f64,
}

/// Cache for type information to reduce LSP queries
pub struct TypeCache {
    cache: RwLock<HashMap<TypeQuery, TypeInfo>>,
    max_entries: usize,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct TypeQuery {
    pub file_path: String,
    pub line: usize,
    pub column: usize,
    pub expression: String,
}

#[derive(Debug, Clone)]
pub struct TypeInfo {
    pub type_name: String,
    pub is_callable: bool,
    pub cached_at: std::time::Instant,
    pub ttl: std::time::Duration, // Time to live
}

impl TypeCache {
    pub fn new(max_entries: usize) -> Self {
        Self {
            cache: RwLock::new(HashMap::new()),
            max_entries,
        }
    }

    pub fn get(&self, query: &TypeQuery) -> Option<TypeInfo> {
        let mut cache = self.cache.write().ok()?;

        if let Some(info) = cache.get(query) {
            // Check if the entry has expired
            if info.cached_at.elapsed() < info.ttl {
                return Some(info.clone());
            } else {
                // Entry expired, remove it
                cache.remove(query);
            }
        }
        None
    }

    pub fn insert(&self, query: TypeQuery, info: TypeInfo) {
        let mut cache = self.cache.write().unwrap();

        // If we're at capacity, remove some expired entries first
        if cache.len() >= self.max_entries {
            let now = std::time::Instant::now();
            cache.retain(|_, info| now.duration_since(info.cached_at) < info.ttl);

            // If still at capacity, remove oldest entries
            if cache.len() >= self.max_entries {
                let oldest_keys: Vec<_> = cache
                    .iter()
                    .map(|(k, v)| (k.clone(), v.cached_at))
                    .collect::<Vec<_>>()
                    .into_iter()
                    .sorted_by_key(|(_, cached_at)| *cached_at)
                    .take(cache.len() - self.max_entries + 1)
                    .map(|(k, _)| k)
                    .collect();

                for key in oldest_keys {
                    cache.remove(&key);
                }
            }
        }

        cache.insert(query, info);
    }

    pub fn stats(&self) -> CacheStats {
        let cache = self.cache.read().unwrap();
        CacheStats {
            entries: cache.len(),
            total_hits: 0, // We don't track hits for type cache yet
            hit_ratio: 0.0,
        }
    }
}

/// Performance monitoring and statistics
pub struct PerformanceMonitor {
    pub string_interner: Arc<StringInterner>,
    pub ast_cache: Arc<AstCache>,
    pub type_cache: Arc<TypeCache>,
    start_time: std::time::Instant,
    files_processed: Arc<Mutex<usize>>,
    total_parse_time: Arc<Mutex<std::time::Duration>>,
    total_migration_time: Arc<Mutex<std::time::Duration>>,
}

impl PerformanceMonitor {
    pub fn new(max_ast_entries: usize, max_type_entries: usize) -> Self {
        Self {
            string_interner: Arc::new(StringInterner::new()),
            ast_cache: Arc::new(AstCache::new(max_ast_entries)),
            type_cache: Arc::new(TypeCache::new(max_type_entries)),
            start_time: std::time::Instant::now(),
            files_processed: Arc::new(Mutex::new(0)),
            total_parse_time: Arc::new(Mutex::new(std::time::Duration::ZERO)),
            total_migration_time: Arc::new(Mutex::new(std::time::Duration::ZERO)),
        }
    }

    pub fn record_file_processed(&self) {
        *self.files_processed.lock().unwrap() += 1;
    }

    pub fn record_parse_time(&self, duration: std::time::Duration) {
        *self.total_parse_time.lock().unwrap() += duration;
    }

    pub fn record_migration_time(&self, duration: std::time::Duration) {
        *self.total_migration_time.lock().unwrap() += duration;
    }

    pub fn summary(&self) -> PerformanceSummary {
        let files_processed = *self.files_processed.lock().unwrap();
        let total_parse_time = *self.total_parse_time.lock().unwrap();
        let total_migration_time = *self.total_migration_time.lock().unwrap();
        let total_time = self.start_time.elapsed();

        PerformanceSummary {
            total_time,
            files_processed,
            files_per_second: if total_time.as_secs() > 0 {
                files_processed as f64 / total_time.as_secs_f64()
            } else {
                0.0
            },
            total_parse_time,
            total_migration_time,
            average_parse_time: if files_processed > 0 {
                total_parse_time / files_processed as u32
            } else {
                std::time::Duration::ZERO
            },
            average_migration_time: if files_processed > 0 {
                total_migration_time / files_processed as u32
            } else {
                std::time::Duration::ZERO
            },
            interner_stats: self.string_interner.stats(),
            ast_cache_stats: self.ast_cache.stats(),
            type_cache_stats: self.type_cache.stats(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct PerformanceSummary {
    pub total_time: std::time::Duration,
    pub files_processed: usize,
    pub files_per_second: f64,
    pub total_parse_time: std::time::Duration,
    pub total_migration_time: std::time::Duration,
    pub average_parse_time: std::time::Duration,
    pub average_migration_time: std::time::Duration,
    pub interner_stats: InternerStats,
    pub ast_cache_stats: CacheStats,
    pub type_cache_stats: CacheStats,
}

impl std::fmt::Display for PerformanceSummary {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Performance Summary:")?;
        writeln!(f, "  Total time: {:.2?}", self.total_time)?;
        writeln!(f, "  Files processed: {}", self.files_processed)?;
        writeln!(f, "  Files per second: {:.2}", self.files_per_second)?;
        writeln!(f, "  Total parse time: {:.2?}", self.total_parse_time)?;
        writeln!(
            f,
            "  Total migration time: {:.2?}",
            self.total_migration_time
        )?;
        writeln!(f, "  Average parse time: {:.2?}", self.average_parse_time)?;
        writeln!(
            f,
            "  Average migration time: {:.2?}",
            self.average_migration_time
        )?;
        writeln!(
            f,
            "  String interner: {} unique strings",
            self.interner_stats.unique_strings
        )?;
        writeln!(
            f,
            "  AST cache: {} entries, {:.1}% hit rate",
            self.ast_cache_stats.entries,
            self.ast_cache_stats.hit_ratio * 100.0
        )?;
        writeln!(f, "  Type cache: {} entries", self.type_cache_stats.entries)?;
        Ok(())
    }
}

/// Helper trait for sorting - we need this because we can't use itertools
trait SortedIterator: Iterator {
    fn sorted_by_key<K, F>(self, f: F) -> std::vec::IntoIter<Self::Item>
    where
        Self: Sized,
        F: FnMut(&Self::Item) -> K,
        K: Ord,
    {
        let mut v: Vec<Self::Item> = self.collect();
        v.sort_by_key(f);
        v.into_iter()
    }
}

impl<I: Iterator> SortedIterator for I {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_string_interner() {
        let interner = StringInterner::new();

        let s1 = interner.intern("hello");
        let s2 = interner.intern("hello");
        let s3 = interner.intern("world");

        // Same string should return same Arc
        assert!(Arc::ptr_eq(&s1, &s2));
        assert!(!Arc::ptr_eq(&s1, &s3));

        let stats = interner.stats();
        assert_eq!(stats.unique_strings, 2);
    }

    #[test]
    fn test_ast_cache() {
        let cache = AstCache::new(2);
        let now = std::time::SystemTime::now();

        // Create a dummy AST
        let module = ast::Mod::Module(ast::ModModule {
            body: vec![],
            type_ignores: vec![],
            range: Default::default(),
        });

        // Insert and retrieve
        cache.insert("test.py".to_string(), module.clone(), now);
        let retrieved = cache.get("test.py", now);
        assert!(retrieved.is_some());

        // Should miss for modified file
        let later = now + std::time::Duration::from_secs(1);
        let missed = cache.get("test.py", later);
        assert!(missed.is_none());
    }

    #[test]
    fn test_type_cache() {
        let cache = TypeCache::new(10);

        let query = TypeQuery {
            file_path: "test.py".to_string(),
            line: 10,
            column: 5,
            expression: "func()".to_string(),
        };

        let info = TypeInfo {
            type_name: "str".to_string(),
            is_callable: false,
            cached_at: std::time::Instant::now(),
            ttl: std::time::Duration::from_secs(60),
        };

        cache.insert(query.clone(), info);
        let retrieved = cache.get(&query);
        assert!(retrieved.is_some());
    }
}