Skip to content

Commit

Permalink
Expanded test and added comment for clarification
Browse files Browse the repository at this point in the history
  • Loading branch information
algoidurovic committed Oct 15, 2021
1 parent e0795fc commit bb1eb20
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
2 changes: 2 additions & 0 deletions pyteal/compiler/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def createConstantBlocks(ops: List[TealComponent]) -> List[TealComponent]:
sortedInts = sorted(intFreqs, key=lambda x: intFreqs[x], reverse=True)
sortedBytes = sorted(byteFreqs, key=lambda x: byteFreqs[x], reverse=True)

# Use Op.pushint if the constant does not occur in the top 4 most frequent and is smaller than
# 2 ** 7 to improve performance and save block space.
intBlock = [
val
for i, val in enumerate(sortedInts)
Expand Down
59 changes: 30 additions & 29 deletions pyteal/compiler/constants_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,32 +589,33 @@ def test_createConstantBlocks_small_constant():
and it can be stored in one varuint it byte then Op.pushint is used.
"""

ops = [
TealOp(None, Op.int, 0),
TealOp(None, Op.int, 0),
TealOp(None, Op.int, 1),
TealOp(None, Op.int, 1),
TealOp(None, Op.int, 2),
TealOp(None, Op.int, 2),
TealOp(None, Op.int, 3),
TealOp(None, Op.int, 3),
TealOp(None, Op.int, 4),
TealOp(None, Op.int, 4),
]

expected = [
TealOp(None, Op.intcblock, 0, 1, 2, 3),
TealOp(None, Op.intc_0, "//", 0),
TealOp(None, Op.intc_0, "//", 0),
TealOp(None, Op.intc_1, "//", 1),
TealOp(None, Op.intc_1, "//", 1),
TealOp(None, Op.intc_2, "//", 2),
TealOp(None, Op.intc_2, "//", 2),
TealOp(None, Op.intc_3, "//", 3),
TealOp(None, Op.intc_3, "//", 3),
TealOp(None, Op.pushint, 4, "//", 4),
TealOp(None, Op.pushint, 4, "//", 4),
]

actual = createConstantBlocks(ops)
assert actual == expected
for cur in range(4, 2 ** 7):
ops = [
TealOp(None, Op.int, 0),
TealOp(None, Op.int, 0),
TealOp(None, Op.int, 1),
TealOp(None, Op.int, 1),
TealOp(None, Op.int, 2),
TealOp(None, Op.int, 2),
TealOp(None, Op.int, 3),
TealOp(None, Op.int, 3),
TealOp(None, Op.int, cur),
TealOp(None, Op.int, cur),
]

expected = [
TealOp(None, Op.intcblock, 0, 1, 2, 3),
TealOp(None, Op.intc_0, "//", 0),
TealOp(None, Op.intc_0, "//", 0),
TealOp(None, Op.intc_1, "//", 1),
TealOp(None, Op.intc_1, "//", 1),
TealOp(None, Op.intc_2, "//", 2),
TealOp(None, Op.intc_2, "//", 2),
TealOp(None, Op.intc_3, "//", 3),
TealOp(None, Op.intc_3, "//", 3),
TealOp(None, Op.pushint, cur, "//", cur),
TealOp(None, Op.pushint, cur, "//", cur),
]

actual = createConstantBlocks(ops)
assert actual == expected

0 comments on commit bb1eb20

Please sign in to comment.