Skip to content

Commit f205e6b

Browse files
committed
Optimize all items/keys/values methods
1 parent 201ba14 commit f205e6b

9 files changed

Lines changed: 67 additions & 72 deletions

File tree

cachebox/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88
from ._cachebox import VTTLCache as VTTLCache
99
from ._core import __version__ as __version__
1010
from ._core import _small_offset_feature as _small_offset_feature
11+
12+
# utils
13+
from .utils import Frozen as Frozen

cachebox/_core.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class BaseCacheImpl(typing.Generic[KT, VT]):
6767

6868
@property
6969
def maxsize(self) -> int:
70-
"""int: The configured ``maxsize``."""
70+
"""The configured ``maxsize``."""
7171
...
7272

7373
@property

src/pyclasses/cache.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ impl PyCache {
489489
.map(|x| !x)
490490
}
491491

492-
fn items(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyCacheItems>> {
492+
fn items(&self) -> pyo3::PyResult<pyo3::Py<PyCacheItems>> {
493493
let inner = self.0.get();
494494
let gv = inner.shared().generation_version().clone();
495495
let initial_gv = gv.get();
@@ -500,10 +500,11 @@ impl PyCache {
500500
gv,
501501
initial_gv,
502502
};
503-
pyo3::Py::new(py, result)
503+
504+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
504505
}
505506

506-
fn values(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyCacheValues>> {
507+
fn values(&self) -> pyo3::PyResult<pyo3::Py<PyCacheValues>> {
507508
let inner = self.0.get();
508509
let gv = inner.shared().generation_version().clone();
509510
let initial_gv = gv.get();
@@ -514,10 +515,10 @@ impl PyCache {
514515
gv,
515516
initial_gv,
516517
};
517-
pyo3::Py::new(py, result)
518+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
518519
}
519520

520-
fn keys(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyCacheKeys>> {
521+
fn keys(&self) -> pyo3::PyResult<pyo3::Py<PyCacheKeys>> {
521522
let inner = self.0.get();
522523
let gv = inner.shared().generation_version().clone();
523524
let initial_gv = gv.get();
@@ -528,12 +529,12 @@ impl PyCache {
528529
gv,
529530
initial_gv,
530531
};
531-
pyo3::Py::new(py, result)
532+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
532533
}
533534

534535
#[inline]
535-
fn __iter__(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyCacheKeys>> {
536-
self.keys(py)
536+
fn __iter__(&self) -> pyo3::PyResult<pyo3::Py<PyCacheKeys>> {
537+
self.keys()
537538
}
538539

539540
fn copy(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<Self>> {

src/pyclasses/fifocache.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ impl PyFIFOCache {
494494
.map(|x| !x)
495495
}
496496

497-
fn items(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheItems>> {
497+
fn items(&self) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheItems>> {
498498
let inner = self.0.get();
499499
let gv = inner.shared().generation_version().clone();
500500
let initial_gv = gv.get();
@@ -505,10 +505,10 @@ impl PyFIFOCache {
505505
gv,
506506
initial_gv,
507507
};
508-
pyo3::Py::new(py, result)
508+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
509509
}
510510

511-
fn values(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheValues>> {
511+
fn values(&self) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheValues>> {
512512
let inner = self.0.get();
513513
let gv = inner.shared().generation_version().clone();
514514
let initial_gv = gv.get();
@@ -519,10 +519,10 @@ impl PyFIFOCache {
519519
gv,
520520
initial_gv,
521521
};
522-
pyo3::Py::new(py, result)
522+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
523523
}
524524

525-
fn keys(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheKeys>> {
525+
fn keys(&self) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheKeys>> {
526526
let inner = self.0.get();
527527
let gv = inner.shared().generation_version().clone();
528528
let initial_gv = gv.get();
@@ -533,12 +533,12 @@ impl PyFIFOCache {
533533
gv,
534534
initial_gv,
535535
};
536-
pyo3::Py::new(py, result)
536+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
537537
}
538538

539539
#[inline]
540-
fn __iter__(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheKeys>> {
541-
self.keys(py)
540+
fn __iter__(&self) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheKeys>> {
541+
self.keys()
542542
}
543543

544544
fn copy(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<Self>> {

src/pyclasses/lfucache.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl PyLFUCache {
512512
.map(|x| !x)
513513
}
514514

515-
fn items(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyLFUCacheItems>> {
515+
fn items(&self) -> pyo3::PyResult<pyo3::Py<PyLFUCacheItems>> {
516516
let inner = self.0.get();
517517
let mut policy = inner.policy();
518518

@@ -524,10 +524,10 @@ impl PyLFUCache {
524524
gv: gv.clone(),
525525
initial_gv: gv.get(),
526526
};
527-
pyo3::Py::new(py, result)
527+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
528528
}
529529

530-
fn values(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyLFUCacheValues>> {
530+
fn values(&self) -> pyo3::PyResult<pyo3::Py<PyLFUCacheValues>> {
531531
let inner = self.0.get();
532532
let mut policy = inner.policy();
533533

@@ -539,10 +539,10 @@ impl PyLFUCache {
539539
gv: gv.clone(),
540540
initial_gv: gv.get(),
541541
};
542-
pyo3::Py::new(py, result)
542+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
543543
}
544544

545-
fn keys(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyLFUCacheKeys>> {
545+
fn keys(&self) -> pyo3::PyResult<pyo3::Py<PyLFUCacheKeys>> {
546546
let inner = self.0.get();
547547
let mut policy = inner.policy();
548548

@@ -554,18 +554,15 @@ impl PyLFUCache {
554554
gv: gv.clone(),
555555
initial_gv: gv.get(),
556556
};
557-
pyo3::Py::new(py, result)
557+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
558558
}
559559

560560
#[inline]
561-
fn __iter__(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyLFUCacheKeys>> {
562-
self.keys(py)
561+
fn __iter__(&self) -> pyo3::PyResult<pyo3::Py<PyLFUCacheKeys>> {
562+
self.keys()
563563
}
564564

565-
fn items_with_frequency(
566-
&self,
567-
py: pyo3::Python,
568-
) -> pyo3::PyResult<pyo3::Py<PyLFUCacheItemsWithFrequency>> {
565+
fn items_with_frequency(&self) -> pyo3::PyResult<pyo3::Py<PyLFUCacheItemsWithFrequency>> {
569566
let inner = self.0.get();
570567
let mut policy = inner.policy();
571568

@@ -577,7 +574,7 @@ impl PyLFUCache {
577574
gv: gv.clone(),
578575
initial_gv: gv.get(),
579576
};
580-
pyo3::Py::new(py, result)
577+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
581578
}
582579

583580
fn copy(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<Self>> {

src/pyclasses/lrucache.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ impl PyLRUCache {
521521
.map(|x| !x)
522522
}
523523

524-
fn items(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyLRUCacheItems>> {
524+
fn items(&self) -> pyo3::PyResult<pyo3::Py<PyLRUCacheItems>> {
525525
let inner = self.0.get();
526526
let gv = inner.shared().generation_version().clone();
527527
let initial_gv = gv.get();
@@ -532,10 +532,10 @@ impl PyLRUCache {
532532
gv,
533533
initial_gv,
534534
};
535-
pyo3::Py::new(py, result)
535+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
536536
}
537537

538-
fn values(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyLRUCacheValues>> {
538+
fn values(&self) -> pyo3::PyResult<pyo3::Py<PyLRUCacheValues>> {
539539
let inner = self.0.get();
540540
let gv = inner.shared().generation_version().clone();
541541
let initial_gv = gv.get();
@@ -546,10 +546,10 @@ impl PyLRUCache {
546546
gv,
547547
initial_gv,
548548
};
549-
pyo3::Py::new(py, result)
549+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
550550
}
551551

552-
fn keys(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyLRUCacheKeys>> {
552+
fn keys(&self) -> pyo3::PyResult<pyo3::Py<PyLRUCacheKeys>> {
553553
let inner = self.0.get();
554554
let gv = inner.shared().generation_version().clone();
555555
let initial_gv = gv.get();
@@ -560,12 +560,12 @@ impl PyLRUCache {
560560
gv,
561561
initial_gv,
562562
};
563-
pyo3::Py::new(py, result)
563+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
564564
}
565565

566566
#[inline]
567-
fn __iter__(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyLRUCacheKeys>> {
568-
self.keys(py)
567+
fn __iter__(&self) -> pyo3::PyResult<pyo3::Py<PyLRUCacheKeys>> {
568+
self.keys()
569569
}
570570

571571
fn copy(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<Self>> {

src/pyclasses/rrcache.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ impl PyRRCache {
494494
.map(|x| !x)
495495
}
496496

497-
fn items(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyRRCacheItems>> {
497+
fn items(&self) -> pyo3::PyResult<pyo3::Py<PyRRCacheItems>> {
498498
let inner = self.0.get();
499499
let gv = inner.shared().generation_version().clone();
500500
let initial_gv = gv.get();
@@ -505,10 +505,10 @@ impl PyRRCache {
505505
gv,
506506
initial_gv,
507507
};
508-
pyo3::Py::new(py, result)
508+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
509509
}
510510

511-
fn values(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyRRCacheValues>> {
511+
fn values(&self) -> pyo3::PyResult<pyo3::Py<PyRRCacheValues>> {
512512
let inner = self.0.get();
513513
let gv = inner.shared().generation_version().clone();
514514
let initial_gv = gv.get();
@@ -519,10 +519,10 @@ impl PyRRCache {
519519
gv,
520520
initial_gv,
521521
};
522-
pyo3::Py::new(py, result)
522+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
523523
}
524524

525-
fn keys(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyRRCacheKeys>> {
525+
fn keys(&self) -> pyo3::PyResult<pyo3::Py<PyRRCacheKeys>> {
526526
let inner = self.0.get();
527527
let gv = inner.shared().generation_version().clone();
528528
let initial_gv = gv.get();
@@ -533,12 +533,12 @@ impl PyRRCache {
533533
gv,
534534
initial_gv,
535535
};
536-
pyo3::Py::new(py, result)
536+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
537537
}
538538

539539
#[inline]
540-
fn __iter__(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyRRCacheKeys>> {
541-
self.keys(py)
540+
fn __iter__(&self) -> pyo3::PyResult<pyo3::Py<PyRRCacheKeys>> {
541+
self.keys()
542542
}
543543

544544
fn copy(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<Self>> {

src/pyclasses/ttlcache.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ impl PyTTLCache {
499499
.map(|x| !x)
500500
}
501501

502-
fn items(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyTTLCacheItems>> {
502+
fn items(&self) -> pyo3::PyResult<pyo3::Py<PyTTLCacheItems>> {
503503
let inner = self.0.get();
504504

505505
let iter = inner.policy().iter(inner.shared());
@@ -513,10 +513,10 @@ impl PyTTLCache {
513513
gv,
514514
initial_gv,
515515
};
516-
pyo3::Py::new(py, result)
516+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
517517
}
518518

519-
fn values(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyTTLCacheValues>> {
519+
fn values(&self) -> pyo3::PyResult<pyo3::Py<PyTTLCacheValues>> {
520520
let inner = self.0.get();
521521

522522
let iter = inner.policy().iter(inner.shared());
@@ -530,10 +530,10 @@ impl PyTTLCache {
530530
gv,
531531
initial_gv,
532532
};
533-
pyo3::Py::new(py, result)
533+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
534534
}
535535

536-
fn keys(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyTTLCacheKeys>> {
536+
fn keys(&self) -> pyo3::PyResult<pyo3::Py<PyTTLCacheKeys>> {
537537
let inner = self.0.get();
538538

539539
let iter = inner.policy().iter(inner.shared());
@@ -547,12 +547,12 @@ impl PyTTLCache {
547547
gv,
548548
initial_gv,
549549
};
550-
pyo3::Py::new(py, result)
550+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
551551
}
552552

553553
#[inline]
554-
fn __iter__(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<PyTTLCacheKeys>> {
555-
self.keys(py)
554+
fn __iter__(&self) -> pyo3::PyResult<pyo3::Py<PyTTLCacheKeys>> {
555+
self.keys()
556556
}
557557

558558
fn copy(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<Self>> {
@@ -735,10 +735,7 @@ impl PyTTLCache {
735735
Ok((key.into(), val, dur.as_secs_f64()))
736736
}
737737

738-
fn items_with_expire(
739-
&self,
740-
py: pyo3::Python,
741-
) -> pyo3::PyResult<pyo3::Py<PyTTLCacheItemsWithExpire>> {
738+
fn items_with_expire(&self) -> pyo3::PyResult<pyo3::Py<PyTTLCacheItemsWithExpire>> {
742739
let inner = self.0.get();
743740

744741
let iter = inner.policy().iter(inner.shared());
@@ -752,7 +749,7 @@ impl PyTTLCache {
752749
gv,
753750
initial_gv,
754751
};
755-
pyo3::Py::new(py, result)
752+
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
756753
}
757754

758755
fn __traverse__(&self, visit: pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> {

0 commit comments

Comments
 (0)