Skip to content

Commit

Permalink
perf: only check if field is set (#519)
Browse files Browse the repository at this point in the history
* perf: only check if field is set

* fix: correct _is_not_empty implementation
  • Loading branch information
alaeddine-13 authored Sep 5, 2022
1 parent fa05ec3 commit d615233
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
4 changes: 3 additions & 1 deletion docarray/array/queryset/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"""
from typing import TYPE_CHECKING

from docarray.document.data import _is_not_empty

if TYPE_CHECKING:
from docarray import Document
import re
Expand Down Expand Up @@ -121,7 +123,7 @@ def lookup(key, val, doc: 'Document') -> bool:

return is_empty != val
else:
return (get_key in doc.non_empty_fields) == val
return (_is_not_empty(get_key, value)) == val
else:
# return value == val
raise ValueError(
Expand Down
40 changes: 23 additions & 17 deletions docarray/document/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@
_all_mime_types = set(mimetypes.types_map.values())


def _is_not_empty(attribute, value):
if value is not None:
if attribute not in default_values:
return True
else:
dv = default_values[attribute]
if dv in (
'ChunkArray',
'MatchArray',
'DocumentArray',
list,
dict,
'Dict[str, NamedScore]',
):
if value:
return True
elif value != dv:
return True
return False


@dataclass(unsafe_hash=True, eq=False)
class DocumentData:
_reference_doc: 'Document' = field(hash=False, compare=False)
Expand Down Expand Up @@ -67,23 +88,8 @@ def _non_empty_fields(self) -> Tuple[str]:
f_name = f.name
if not f_name.startswith('_') or f_name == '_metadata':
v = getattr(self, f_name)
if v is not None:
if f_name not in default_values:
r.append(f_name)
else:
dv = default_values[f_name]
if dv in (
'ChunkArray',
'MatchArray',
'DocumentArray',
list,
dict,
'Dict[str, NamedScore]',
):
if v:
r.append(f_name)
elif v != dv:
r.append(f_name)
if _is_not_empty(f_name, v):
r.append(f_name)

return tuple(r)

Expand Down

0 comments on commit d615233

Please sign in to comment.