Skip to content

Commit

Permalink
[mypyc] Faster dict true test (#10333)
Browse files Browse the repository at this point in the history
  • Loading branch information
97littleleaf11 authored Apr 22, 2021
1 parent 09bf250 commit bdcc562
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
is_list_rprimitive, is_tuple_rprimitive, is_dict_rprimitive, is_set_rprimitive, PySetObject,
none_rprimitive, RTuple, is_bool_rprimitive, is_str_rprimitive, c_int_rprimitive,
pointer_rprimitive, PyObject, PyListObject, bit_rprimitive, is_bit_rprimitive,
object_pointer_rprimitive, c_size_t_rprimitive
object_pointer_rprimitive, c_size_t_rprimitive, dict_rprimitive
)
from mypyc.ir.func_ir import FuncDecl, FuncSignature
from mypyc.ir.class_ir import ClassIR, all_concrete_classes
Expand Down Expand Up @@ -960,7 +960,8 @@ def add_bool_branch(self, value: Value, true: BasicBlock, false: BasicBlock) ->
return
elif is_same_type(value.type, str_rprimitive):
value = self.call_c(str_check_if_true, [value], value.line)
elif is_same_type(value.type, list_rprimitive):
elif (is_same_type(value.type, list_rprimitive)
or is_same_type(value.type, dict_rprimitive)):
length = self.builtin_len(value, value.line)
zero = Integer(0)
value = self.binary_op(length, zero, '!=', value.line)
Expand Down
23 changes: 23 additions & 0 deletions mypyc/test-data/run-dicts.test
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,26 @@ def test_dict_subclass_setdefault() -> None:
assert d.setdefault('a') == 1
assert d.setdefault('e') == None
assert d.setdefault('e', 100) == 110

[case testDictToBool]
from typing import Dict, List

def is_true(x: dict) -> bool:
if x:
return True
else:
return False

def is_false(x: dict) -> bool:
if not x:
return True
else:
return False

def test_dict_to_bool() -> None:
assert is_false({})
assert not is_true({})
tmp_list: List[Dict] = [{2: bool}, {'a': 'b'}]
for x in tmp_list:
assert is_true(x)
assert not is_false(x)

0 comments on commit bdcc562

Please sign in to comment.