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

Replace usages of list.insert(0, ...) #17

Merged
merged 1 commit into from
Sep 11, 2024
Merged

Conversation

bibajz
Copy link

@bibajz bibajz commented Aug 10, 2024

Hello!

In my pursuit of deepening knowledge about FreeBSD jails, a lot of people and books point to iocage as the way to go, so I checked the project. While doing so, I noticed usages of list.insert(0, ...), which is a Python footgun, because despite the name, Python lists are more like arrays with optimized appends, not prepends, so such call incurs an O(n) runtime penalty.

I decided to grep the codebase for .insert( method calls and fix them in this pull request.


Remaining usages:

$ rg '\.insert\(' --glob='*.py'
doc/source/conf.py
21:sys.path.insert(0, os.path.abspath('.'))

tests/conftest.py
262:        cmd.insert(0, 'iocage')

iocage_lib/ioc_start.py
1753:                    rules.insert(1, f'{final_line}{rdrs}')
1763:                rules.insert(1, nat_rule)

were inspected and do not pose any trouble.


Make sure to follow and check these boxes before submitting a PR! Thank you.

In Python, inserting an item at the beginning of a list incurs an
O(n) penalty, because Python lists behave more like arrays -
https://docs.python.org/3/glossary.html#term-list - and are opti-
mized for appends, which are O(1).

Remaining usages:

```
$ rg '\.insert\(' --glob='*.py'
doc/source/conf.py
21:sys.path.insert(0, os.path.abspath('.'))

tests/conftest.py
262:        cmd.insert(0, 'iocage')

iocage_lib/ioc_start.py
1753:                    rules.insert(1, f'{final_line}{rdrs}')
1763:                rules.insert(1, nat_rule)
```

were inspected and do not pose any trouble.
@coveralls
Copy link

coveralls commented Aug 13, 2024

Pull Request Test Coverage Report for Build 10333795135

Details

  • 0 of 19 (0.0%) changed or added relevant lines in 6 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.002%) to 7.797%

Changes Missing Coverage Covered Lines Changed/Added Lines %
iocage_cli/df.py 0 2 0.0%
iocage_cli/snaplist.py 0 2 0.0%
iocage_lib/ioc_fstab.py 0 2 0.0%
iocage_lib/ioc_plugin.py 0 2 0.0%
iocage_lib/ioc_common.py 0 3 0.0%
iocage_lib/ioc_list.py 0 8 0.0%
Files with Coverage Reduction New Missed Lines %
iocage_lib/ioc_common.py 1 16.97%
Totals Coverage Status
Change from base Build 9926936252: 0.002%
Covered Lines: 798
Relevant Lines: 7752

💛 - Coveralls

@dgeo
Copy link
Contributor

dgeo commented Sep 9, 2024

Thank you.
I still have to test this, but it seems ok

Copy link
Contributor

@dgeo dgeo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested and approved

@dgeo dgeo merged commit c6e10a0 into freebsd:master Sep 11, 2024
2 checks passed
@dgeo dgeo added this to the 1.8 milestone Sep 11, 2024
@dgeo dgeo added the enhancement New feature or request label Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants