You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some set constants can be replaced by frozensets, e.g. in cases like x in {1,2,3}. This would allow them to become global constants, created once at module init time. There is already a framework for merging tuple and string constants, see TupleNode. The ConstantFolding transformation could detect these cases and set a flag attribute on the SetNode instances that would trigger constant frozenset behaviour.
Other potential candidates:
>>>frozenset((1,2,3)) | {2,3,4} # imagine one side being non-constantfrozenset([1, 2, 3, 4])
>>> {3,4,5} |frozenset((1,2,3))
set([1, 2, 3, 4, 5])
if self.is_literal or self.is_partly_literal:
# The "mult_factor" is part of the deduplication if it is also constant, i.e. when
# we deduplicate the multiplied result. Otherwise, only deduplicate the constant part.
dedup_key = make_dedup_key(self.type, [self.mult_factor if self.is_literal else None] + self.args)
tuple_target = code.get_py_const(py_object_type, 'tuple', cleanup_level=2, dedup_key=dedup_key)
const_code = code.get_cached_constants_writer(tuple_target)
if const_code is not None:
# constant is not yet initialised
const_code.mark_pos(self.pos)
self.generate_sequence_packing_code(const_code, tuple_target, plain=not self.is_literal)
const_code.put_giveref(tuple_target, py_object_type)
Is here the place for deduping tuple. and I should go deeper for code at here?
Activity
Endle commentedon Apr 4, 2021
Hi scoder. I'm new to cython and I'm interested in this issue
As you mentioned, we're already deduping tuple constants.
For example. in this case
We're referring to the same object
I'm reading the code for
TupleNodeand I foundIs here the place for deduping tuple. and I should go deeper for code at here?
Thank you!
da-woods commentedon Apr 4, 2021
@Endle That definitely looks right to me. I think the harder part of this issue will be identifying which sets to freeze
Endle commentedon Apr 5, 2021
I found an interesting case after my first glance (I'm not sure it if's related to this issue)
See this code
We know
a, b, care identical. However, cython result isIt's making an extra function call. The
tuplekeyword will be considered as<Cython.Compiler.ExprNodes.SimpleCallNode object at 0x7f15633615e0>For
frozenset, I think the logic lies in_handle_simple_function_frozensetand I would have a deeper look at itThank you
Edit: I mean
OptimizeBuiltinCalls._handle_simple_function_frozensetscoder commentedon Apr 5, 2021
Gives me a
TypeErrorat runtime, too many arguments. Same in Cython.Endle commentedon Apr 5, 2021
Sorry, my bad.
If I create a tuple correctly, cython will re-use the same tuple object as expected.
Endle commentedon Apr 6, 2021
I created a draft at Endle#1
With some ugly hacks, I could let cython share the same frozenset object
Then
Endle commentedon Apr 6, 2021
I need some time to understand
refand other code generation components to clean up my PRDeduplicate constant frozensets (cythonGH-7838)
Integrate the aHPy Universal backend into main (#2)