Optimize Python performance: eliminate anti-patterns in loops, collections, string operations, data structures, and memory usage - #1182
Conversation
|
Looking AI. Can it improve the performance or not? |
…ist concatenation, and unnecessary list() calls Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
… khavee loop patterns Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
… fix middle_cut Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
|
@copilot Due to merge resolution from dev branch, some of the changes made previously by this PR is reverted. Please rework. Additionally, look at:
|
…ns, re.search over re.findall, and efficient list slicing Co-authored-by: bact <128572+bact@users.noreply.github.com>
…n for efficient iteration Co-authored-by: bact <128572+bact@users.noreply.github.com>
…mplify conditions Co-authored-by: bact <128572+bact@users.noreply.github.com>
I've reworked the PR after the dev merge and added additional optimizations: New optimizations added (commits 2f805d7, c036569, 0e7fc37):
All tests pass (17/17 tokenize tests OK). The optimizations maintain backward compatibility while improving performance through better data structures and algorithms. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot Look at new added comments, remove those verbose ones, ones that state the obvious. |
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Removed verbose comments in commit ffbfc4e. Cleaned up comments that stated the obvious from the code itself, including:
The code is now cleaner while maintaining all optimizations. |
|
@copilot Find opportunities for early returns that might speedup function call (also try to keep number of returns within the limit of linters). |
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Added early returns in commit 52a8f4b to avoid unnecessary computation: Functions optimized:
Additional optimization:
All functions maintain single return point or have minimal returns within linter limits. Tests pass (17/17 tokenize, 2/2 lm tests). |
|
@copilot There are changes from dev branch merged in. Please recheck the optimizations that have been made. Addtionally, run Ruff to identify more potential waste of resources and other issues. Fix them when possible. Particularly for these rules: C417 Unnecessary |
…prehensions, remove unused import, fix default parameter, and remove trailing whitespace Co-authored-by: bact <128572+bact@users.noreply.github.com>
Fixed all Ruff issues in commit 1b004ac: Ruff violations fixed:
Whitespace issues fixed:
Files optimized: 11 files updated with cleaner, more efficient code. All Ruff checks now pass. Tests pass (17/17 tokenize tests). Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
Test wsd modulefrom pythainlp.wsd.core import get_sense Test tokenize - already tested but let's confirmfrom pythainlp.tokenize.core import word_tokenize
|
|
@copilot Got this warning from the test workflow: E/home/runner/work/pythainlp/pythainlp/pythainlp/corpus/tnc.py:65: ResourceWarning: unclosed <ssl.SSLSocket [closed] fd=3, family=2, type=1, proto=6> Identify the issue and fix it |
…c.py Co-authored-by: bact <128572+bact@users.noreply.github.com>
Fixed ResourceWarning in commit 08092b2. Added try-except blocks around file operations in Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
from pythainlp.corpus import tnc
try:
|
|



Performance Improvements Summary (Final)
This PR implements comprehensive performance optimizations for PyThaiNLP.
Latest Update:
✅ Fixed ResourceWarning - Added exception handling to ensure proper file cleanup in corpus/tnc.py
Previous Updates:
✅ Fixed Ruff lint issues - Cleaned up code quality issues
✅ Added early returns - Functions return immediately for empty/invalid inputs
✅ List comprehensions and generators - Use comprehensions and generator expressions
✅ Optimized corpus reading - Iterate directly without loading all lines into memory
✅ Removed verbose comments - Cleaned up obvious comments
✅ Data structure efficiency - Use
deque.popleft(),itertools.chain()✅ Algorithm improvements - Compiled regex patterns, dict comprehensions
✅ Loop and collection optimizations -
enumerate(),extend(),set.update()Files Modified (20 total):
Performance Impact:
All changes maintain backward compatibility and pass existing tests.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.