Skip to content

Commit

Permalink
adding config setting to disable singleton cache
Browse files Browse the repository at this point in the history
  • Loading branch information
brimoor committed Dec 22, 2023
1 parent 2ed7f19 commit 9ad6b78
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/source/user_guide/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ FiftyOne supports the configuration options described below:
| | | | operations such reading/writing large datasets or activiating FiftyOne |
| | | | Brain methods on datasets. |
+-------------------------------+-------------------------------------+-------------------------------+----------------------------------------------------------------------------------------+
| `singleton_cache` | `FIFTYONE_SINGLETON_CACHE` | `True` | Whether to treat :class:`Dataset <fiftyone.core.dataset.Dataset>`, |
| | | | :class:`Sample <fiftyone.core.sample.Sample>`, and |
| | | | :class:`Frame <fiftyone.core.frame.Frame>` instances as singletons. |
+-------------------------------+-------------------------------------+-------------------------------+----------------------------------------------------------------------------------------+
| `timezone` | `FIFTYONE_TIMEZONE` | `None` | An optional timzone string. If provided, all datetimes read from FiftyOne datasets |
| | | | will be expressed in this timezone. See :ref:`this section <configuring-timezone>` for |
| | | | more information. |
Expand Down Expand Up @@ -191,6 +195,7 @@ and the CLI:
"plugins_dir": null,
"requirement_error_level": 0,
"show_progress_bars": true,
"singleton_cache": true,
"timezone": null
}
Expand Down Expand Up @@ -242,6 +247,7 @@ and the CLI:
"plugins_dir": null,
"requirement_error_level": 0,
"show_progress_bars": true,
"singleton_cache": true,
"timezone": null
}
Expand Down
6 changes: 6 additions & 0 deletions fiftyone/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ def __init__(self, d=None):
env_var="FIFTYONE_MAX_PROCESS_POOL_WORKERS",
default=None,
)
self.singleton_cache = self.parse_bool(
d,
"singleton_cache",
env_var="FIFTYONE_SINGLETON_CACHE",
default=True,
)

self._init()

Expand Down
15 changes: 10 additions & 5 deletions fiftyone/core/singletons.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from collections import defaultdict
import weakref

import fiftyone as fo


class DatasetSingleton(type):
"""Singleton metaclass for :class:`fiftyone.core.dataset.Dataset`.
Expand Down Expand Up @@ -44,7 +46,8 @@ def __call__(cls, name=None, _create=True, *args, **kwargs):
name=name, _create=_create, *args, **kwargs
)

cls._instances[name] = instance
if fo.config.singleton_cache:
cls._instances[name] = instance

return instance

Expand Down Expand Up @@ -107,7 +110,8 @@ def __new__(metacls, *args, **kwargs):
return cls

def _register_instance(cls, obj):
cls._instances[obj._doc.collection_name][obj.id] = obj
if fo.config.singleton_cache:
cls._instances[obj._doc.collection_name][obj.id] = obj

def _get_instance(cls, doc):
try:
Expand Down Expand Up @@ -248,9 +252,10 @@ def __new__(metacls, *args, **kwargs):
return cls

def _register_instance(cls, obj):
cls._instances[obj._doc.collection_name][obj.sample_id][
obj.frame_number
] = obj
if fo.config.singleton_cache:
cls._instances[obj._doc.collection_name][obj.sample_id][
obj.frame_number
] = obj

def _get_instance(cls, doc):
try:
Expand Down

0 comments on commit 9ad6b78

Please sign in to comment.