Skip to content

Commit

Permalink
Drop Boltons dependency for small custom LRU dict
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Sep 26, 2023
1 parent 9996af1 commit 9985de7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
url="https://github.com/google-research/torchsde",
packages=setuptools.find_packages(exclude=['benchmarks', 'diagnostics', 'examples', 'tests']),
install_requires=[
"boltons>=20.2.1",
"numpy>=1.19",
"scipy>=1.5",
"torch>=1.6.0",
Expand Down
18 changes: 16 additions & 2 deletions torchsde/_brownian/brownian_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import trampoline
import warnings

import boltons.cacheutils
import numpy as np
import torch

Expand Down Expand Up @@ -112,6 +111,21 @@ def __getitem__(self, item):
raise KeyError


class _LRUDict(dict):
def __init__(self, max_size):
super().__init__()
self._max_size = max_size
self._keys = []

def __setitem__(self, key, value):
if key in self:
self._keys.remove(key)
elif len(self) >= self._max_size:
del self[self._keys.pop(0)]
super().__setitem__(key, value)
self._keys.append(key)


class _Interval:
# Intervals correspond to some subinterval of the overall interval [t0, t1].
# They are arranged as a binary tree: each node corresponds to an interval. If a node has children, they are left
Expand Down Expand Up @@ -505,7 +519,7 @@ def __init__(self,
elif cache_size == 0:
self._increment_and_space_time_levy_area_cache = _EmptyDict()
else:
self._increment_and_space_time_levy_area_cache = boltons.cacheutils.LRU(max_size=cache_size)
self._increment_and_space_time_levy_area_cache = _LRUDict(max_size=cache_size)

# We keep track of the most recently queried interval, and start searching for the next interval from that
# element of the binary tree. This is because subsequent queries are likely to be near the most recent query.
Expand Down

0 comments on commit 9985de7

Please sign in to comment.