Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use analytical solver for BitLengthSet instead of numerical computation #66

Merged
merged 21 commits into from
Apr 7, 2021
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
44d11bd
Refactor the bit length set into a package
pavel-kirienko Mar 28, 2021
8fd3af1
Intermediate result; seems like a dead-end
pavel-kirienko Apr 2, 2021
894c325
Define three operators so far: Nullary, Padding, Concatenation
pavel-kirienko Apr 4, 2021
59fc04e
Implement all operators
pavel-kirienko Apr 4, 2021
9e23e6f
Update the bit length set class to use the new analytical solver
pavel-kirienko Apr 5, 2021
3bd9736
Update the bit length set usage
pavel-kirienko Apr 5, 2021
8bb4236
Log slow operations to help with migrating Nunavut to the new solver API
pavel-kirienko Apr 5, 2021
fab989a
Support Python before 3.9
pavel-kirienko Apr 5, 2021
f6aca93
MyPy fixes
pavel-kirienko Apr 5, 2021
6ec9ef2
PyLint fixes
pavel-kirienko Apr 5, 2021
50095e5
Bump up test coverage
pavel-kirienko Apr 5, 2021
0ad83b1
Read env var PYDSDL_POISON_SLOW_EXPANSION to help performance optimiz…
pavel-kirienko Apr 6, 2021
a62ffb0
Remove accidental reliance on numerical expansion through __len__ (di…
pavel-kirienko Apr 6, 2021
01d44bb
Override BitLengthSet.__bool__
pavel-kirienko Apr 6, 2021
be14126
Refactor the symbolic API slightly and apply modulo reduction in the …
pavel-kirienko Apr 7, 2021
c2f0092
Log the stack trace if slow expansion is found
pavel-kirienko Apr 7, 2021
efc7997
Make __eq__ and __hash__ constant-time, too, since type comparison de…
pavel-kirienko Apr 7, 2021
2a0d5a5
Do not cover the diagnostic
pavel-kirienko Apr 7, 2021
2ff2f64
Reduce divisor used in __eq__ to reduce complexity
pavel-kirienko Apr 7, 2021
27d0b80
Review
pavel-kirienko Apr 7, 2021
a1be9d9
Fix deprecation warning syntax
pavel-kirienko Apr 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reduce divisor used in __eq__ to reduce complexity
  • Loading branch information
pavel-kirienko committed Apr 7, 2021
commit 2ff2f64600e2c558f6992115c91403410cd088a0
22 changes: 18 additions & 4 deletions pydsdl/_bit_length_set/_bit_length_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ class BitLengthSet:
so production systems should not rely on them.

Instances are guaranteed to be immutable.

>>> b = 16 + BitLengthSet(8).repeat_range(256)
>>> b
BitLengthSet(concat({16},repeat(<=256,{8})))
>>> b = 32 + b.repeat_range(65536)
>>> b
BitLengthSet(concat({32},repeat(<=65536,concat({16},repeat(<=256,{8})))))
>>> b.min, b.max
(32, 135266336)
>>> sorted(b % 16)
[0, 8]
>>> sorted(b % 32)
[0, 8, 16, 24]
"""

def __init__(self, value: typing.Union[typing.Iterable[int], int, Operator, "BitLengthSet"]):
Expand Down Expand Up @@ -296,16 +309,17 @@ def __eq__(self, other: typing.Any) -> bool:
other = BitLengthSet(other)
except TypeError:
return NotImplemented
return self.min == other.min and self.max == other.max and set(self % 64) == set(other % 64)
divisor = 32
return self.min == other.min and self.max == other.max and set(self % divisor) == set(other % divisor)

def __hash__(self) -> int:
"""
Hash is computed in (nearly) constant time (numerical expansion is not performed).
Hash is computed in constant time (numerical expansion is not performed).

>>> hash(BitLengthSet({1, 2, 3})) != hash(BitLengthSet({1, 3}))
>>> hash(BitLengthSet({1, 4})) != hash(BitLengthSet({1, 3}))
True
"""
return hash((self.min, self.max, frozenset(self % 64)))
return hash((self.min, self.max))

def __bool__(self) -> bool:
"""
Expand Down