Skip to content

Commit

Permalink
dev(hansbug): use _c_undelay_data when checkout datas
Browse files Browse the repository at this point in the history
  • Loading branch information
HansBug committed Jan 2, 2022
1 parent 8e35899 commit 17c2ac4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 122 deletions.
3 changes: 3 additions & 0 deletions treevalue/tree/common/storage.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ cdef class TreeStorage:
cpdef public void deepcopyx_from(self, TreeStorage ts, copy_func, bool allow_delayed)

cpdef public object create_storage(dict value)
cdef object _c_undelay_data(dict data, object k, object v)
cdef object _c_undelay_not_none_data(dict data, object k, object v)
cdef object _c_undelay_check_data(dict data, object k, object v)
65 changes: 26 additions & 39 deletions treevalue/tree/common/storage.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,14 @@ cdef class TreeStorage:
cdef object v, nv
try:
v = self.map[key]
nv = undelay(v)
if nv is not v:
self.map[key] = nv
return nv
else:
return v
return _c_undelay_data(self.map, key, v)
except KeyError:
raise KeyError(f"Key {repr(key)} not found in this tree.")

cpdef public object get_or_default(self, str key, object default):
cdef object v, nv
v = self.map.get(key, default)
nv = undelay(v)
if nv is not v:
v = nv
if key in self.map:
self.map[key] = v

return v
return _c_undelay_check_data(self.map, key, v)

cpdef public void del_(self, str key) except *:
try:
Expand Down Expand Up @@ -90,10 +79,7 @@ cdef class TreeStorage:
cdef object v, obj, nv
for k, v in self.map.items():
if not allow_delayed:
nv = undelay(v)
if nv is not v:
v = nv
self.map[k] = v
v = _c_undelay_data(self.map, k, v)

if isinstance(v, TreeStorage):
result[k] = v.jsondumpx(copy_func, need_raw, allow_delayed)
Expand Down Expand Up @@ -134,10 +120,7 @@ cdef class TreeStorage:
if k in detached:
v = detached[k]
if not allow_delayed:
nv = undelay(v)
if nv is not v:
v = nv
detached[k] = v
v = _c_undelay_data(detached, k, v)

if isinstance(v, TreeStorage):
if k in self.map and isinstance(self.map[k], TreeStorage):
Expand Down Expand Up @@ -181,16 +164,10 @@ cdef class TreeStorage:
if self_keys == other_keys:
for key in self_keys:
self_v = self.map[key]
self_nv = undelay(self_v)
if self_nv is not self_v:
self_v = self_nv
self.map[key] = self_v
self_v = _c_undelay_data(self.map, key, self_v)

other_v = other_map[key]
other_nv = undelay(other_v)
if other_nv is not other_v:
other_v = other_nv
other_map[key] = other_v
other_v = _c_undelay_data(other_map, key, other_v)

if self_v != other_v:
return False
Expand All @@ -214,21 +191,13 @@ cdef class TreeStorage:
cdef str k
cdef object v, nv
for k, v in self.map.items():
nv = undelay(v)
if nv is not v:
v = nv
self.map[k] = v

yield v
yield _c_undelay_data(self.map, k, v)

def items(self):
cdef str k
cdef object v, nv
for k, v in self.map.items():
nv = undelay(v)
if nv is not v:
v = nv
self.map[k] = v
v = _c_undelay_data(self.map, k, v)

yield k, v

Expand All @@ -244,3 +213,21 @@ cpdef object create_storage(dict value):
_map[k] = unraw(v)

return TreeStorage(_map)

cdef inline object _c_undelay_data(dict data, object k, object v):
cdef object nv = undelay(v)
if nv is not v:
data[k] = nv
return nv

cdef inline object _c_undelay_not_none_data(dict data, object k, object v):
cdef object nv = undelay(v)
if nv is not v and k is not None:
data[k] = nv
return nv

cdef inline object _c_undelay_check_data(dict data, object k, object v):
cdef object nv = undelay(v)
if nv is not v and k in data:
data[k] = nv
return nv
28 changes: 7 additions & 21 deletions treevalue/tree/func/cfunc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,26 @@ from libcpp cimport bool
from .modes cimport _e_tree_mode, _c_keyset, _c_load_mode, _c_check
from ..common.delay import delayed_partial
from ..common.delay cimport undelay
from ..common.storage cimport TreeStorage
from ..common.storage cimport TreeStorage, _c_undelay_not_none_data, _c_undelay_data
from ..tree.structural cimport _c_subside, _c_rise
from ..tree.tree cimport TreeValue

_VALUE_IS_MISSING = SingletonMark('value_is_missing')
MISSING_NOT_ALLOW = SingletonMark("missing_not_allow")



cdef inline object _c_wrap_func_treelize_run(object func, list args, dict kwargs, _e_tree_mode mode, bool inherit,
bool allow_missing, object missing_func, bool delayed):
cdef list _l_args = []
cdef dict _d_kwargs = {}
cdef str k, ak
cdef object av, v, nv
for av, k, v in args:
nv = undelay(v)
if nv is not v and k is not None:
av[k] = nv

_l_args.append(nv)
_l_args.append(_c_undelay_not_none_data(av, k, v))

for ak, (av, k, v) in kwargs.items():
nv = undelay(v)
if nv is not v and k is not None:
av[k] = nv

_d_kwargs[ak] = nv
_d_kwargs[ak] = _c_undelay_not_none_data(av, k, v)

return _c_func_treelize_run(func, _l_args, _d_kwargs,
mode, inherit, allow_missing, missing_func, delayed)
Expand Down Expand Up @@ -96,11 +90,7 @@ cdef object _c_func_treelize_run(object func, list args, dict kwargs, _e_tree_mo
if delayed:
_l_args.append((av, k, v))
else:
nv = undelay(v)
if nv is not v:
v = nv
av[k] = v

v = _c_undelay_data(av, k, v)
_l_args.append(v)
except KeyError:
if allow_missing:
Expand Down Expand Up @@ -131,11 +121,7 @@ cdef object _c_func_treelize_run(object func, list args, dict kwargs, _e_tree_mo
if delayed:
_d_kwargs[ak] = (av, k, v)
else:
nv = undelay(v)
if nv is not v:
v = nv
av[k] = v

v = _c_undelay_data(av, k, v)
_d_kwargs[ak] = v
except KeyError:
if allow_missing:
Expand Down
21 changes: 4 additions & 17 deletions treevalue/tree/tree/flatten.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import cython

from .tree cimport TreeValue
from ..common.delay cimport undelay
from ..common.storage cimport TreeStorage
from ..common.storage cimport TreeStorage, _c_undelay_data

cdef void _c_flatten(TreeStorage st, tuple path, list res) except *:
cdef dict data = st.detach()
Expand All @@ -16,11 +15,7 @@ cdef void _c_flatten(TreeStorage st, tuple path, list res) except *:
cdef str k
cdef object v, nv
for k, v in data.items():
nv = undelay(v)
if nv is not v:
v = nv
data[k] = v

v = _c_undelay_data(data, k, v)
curpath = path + (k,)
if isinstance(v, TreeStorage):
_c_flatten(v, curpath, res)
Expand Down Expand Up @@ -55,11 +50,7 @@ cdef void _c_flatten_values(TreeStorage st, list res) except *:
cdef str k
cdef object v, nv
for k, v in data.items():
nv = undelay(v)
if nv is not v:
v = nv
data[k] = v

v = _c_undelay_data(data, k, v)
if isinstance(v, TreeStorage):
_c_flatten_values(v, res)
else:
Expand Down Expand Up @@ -88,11 +79,7 @@ cdef void _c_flatten_keys(TreeStorage st, tuple path, list res) except *:
cdef str k
cdef object v, nv
for k, v in data.items():
nv = undelay(v)
if nv is not v:
v = nv
data[k] = v

v = _c_undelay_data(data, k, v)
curpath = path + (k,)
if isinstance(v, TreeStorage):
_c_flatten_keys(v, curpath, res)
Expand Down
32 changes: 7 additions & 25 deletions treevalue/tree/tree/functional.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ from libcpp cimport bool
from .tree cimport TreeValue
from ..common.delay cimport undelay
from ..common.delay import delayed_partial
from ..common.storage cimport TreeStorage
from ..common.storage cimport TreeStorage, _c_undelay_data

cdef inline object _c_no_arg(object func, object v, object p):
return func()
Expand Down Expand Up @@ -54,10 +54,7 @@ cdef TreeStorage _c_mapping(TreeStorage st, object func, tuple path, bool delaye
cdef tuple curpath
for k, v in _d_st.items():
if not delayed:
nv = undelay(v)
if nv is not v:
v = nv
_d_st[k] = v
v = _c_undelay_data(_d_st, k, v)

curpath = path + (k,)
if isinstance(v, TreeStorage):
Expand Down Expand Up @@ -112,11 +109,7 @@ cdef TreeStorage _c_filter_(TreeStorage st, object func, tuple path, bool remove
cdef tuple curpath
cdef TreeStorage curst
for k, v in _d_st.items():
nv = undelay(v)
if nv is not v:
v = nv
_d_st[k] = v

v = _c_undelay_data(_d_st, k, v)
curpath = path + (k,)
if isinstance(v, TreeStorage):
curst = _c_filter_(v, func, curpath, remove_empty)
Expand Down Expand Up @@ -170,24 +163,17 @@ cdef object _c_mask(TreeStorage st, object sm, tuple path, bool remove_empty):
cdef dict _d_res = {}

cdef str k
cdef object v, mv, nv, nmv
cdef object v, mv
cdef tuple curpath
cdef object curres
for k, v in _d_st.items():
nv = undelay(v)
if nv is not v:
v = nv
_d_st[k] = v

v = _c_undelay_data(_d_st, k, v)
curpath = path + (k,)
if _b_tree_mask:
mv = _d_sm[k]
mv = _c_undelay_data(_d_sm, k, mv)
else:
mv = sm
nmv = undelay(mv)
if nmv is not mv:
mv = nmv
_d_sm[k] = mv

if isinstance(v, TreeStorage):
curres = _c_mask(v, mv, curpath, remove_empty)
Expand Down Expand Up @@ -233,11 +219,7 @@ cdef object _c_reduce(TreeStorage st, object func, tuple path, object return_typ
cdef tuple curpath
cdef object curst
for k, v in _d_st.items():
nv = undelay(v)
if nv is not v:
v = nv
_d_st[k] = v

v = _c_undelay_data(_d_st, k, v)
curpath = path + (k,)
if isinstance(v, TreeStorage):
curst = _c_reduce(v, func, curpath, return_type)
Expand Down
9 changes: 2 additions & 7 deletions treevalue/tree/tree/service.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import cython
from libcpp cimport bool

from .tree cimport TreeValue
from ..common.delay cimport undelay
from ..common.storage cimport TreeStorage
from ..common.storage cimport TreeStorage, _c_undelay_data

cdef object _keep_object(object obj):
return obj
Expand Down Expand Up @@ -99,11 +98,7 @@ def _p_walk(TreeStorage tree, object type_, tuple path, bool include_nodes):
cdef object v, nv
cdef tuple curpath
for k, v in data.items():
nv = undelay(v)
if nv is not v:
v = nv
data[k] = v

v = _c_undelay_data(data, k, v)
curpath = path + (k,)
if isinstance(v, TreeStorage):
yield from _p_walk(v, type_, curpath, include_nodes)
Expand Down
9 changes: 2 additions & 7 deletions treevalue/tree/tree/structural.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ from hbutils.design import SingletonMark
from libcpp cimport bool

from .tree cimport TreeValue
from ..common.delay cimport undelay
from ..common.storage cimport TreeStorage
from ..common.storage cimport TreeStorage, _c_undelay_data
from ..func.cfunc cimport _c_func_treelize_run, _c_missing_process
from ..func.modes cimport _c_load_mode

Expand Down Expand Up @@ -228,11 +227,7 @@ cdef tuple _c_rise_tree_process(object t):
_l_items = []
_l_values = []
for k, v in detached.items():
nv = undelay(v)
if nv is not v:
v = nv
detached[k] = v

v = _c_undelay_data(detached, k, v)
_i_item, _i_value = _c_rise_tree_process(v)
_l_items.append((k, _i_item))
_l_values.append(_i_value)
Expand Down
8 changes: 2 additions & 6 deletions treevalue/tree/tree/tree.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import cython
from hbutils.design import SingletonMark

from ..common.delay cimport undelay, _c_delayed_partial, DelayedProxy
from ..common.storage cimport TreeStorage, create_storage
from ..common.storage cimport TreeStorage, create_storage, _c_undelay_data
from ...utils import format_tree

_GET_NO_DEFAULT = SingletonMark('get_no_default')
Expand Down Expand Up @@ -396,11 +396,7 @@ cdef object _build_tree(TreeStorage st, object type_, str prefix, dict id_pool,
id_pool[nid] = path
data = st.detach()
for k, v in sorted(data.items()):
nv = undelay(v)
if nv is not v:
v = nv
data[k] = v

v = _c_undelay_data(data, k, v)
curpath = path + (k,)
_t_prefix = f'{k} --> '
if isinstance(v, TreeStorage):
Expand Down

0 comments on commit 17c2ac4

Please sign in to comment.