/container-growth-ratio.py Secret
Created
October 26, 2023 20:25
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# container implementation details: we assume that the growth is exponential | |
# and that short string buffer handles strings of smaller size | |
ratio = 1.5 | |
block = 16 | |
# allocator implementation details: we assume that we're using first fit allocator | |
# that magically stores block metadata externally and has no alignment restrictions | |
# for simplicity we just keep track of a single live allocation | |
allocoffset = 0 | |
allocsize = 0 | |
for i in range(20): | |
block = int(block * ratio) | |
if allocoffset >= block: | |
print(f"round {i}: allocated range [{allocoffset}..{allocoffset+allocsize}), allocating {block} from the beginning") | |
allocoffset = 0 | |
allocsize = block | |
else: | |
print(f"round {i}: allocated range [{allocoffset}..{allocoffset+allocsize}), allocating {block} by advancing range") | |
allocoffset += allocsize | |
allocsize = block |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment