Skip to content
Open
Changes from all commits
Commits
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
8 changes: 5 additions & 3 deletions bin/idstack-learnings-delete
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ if ! command -v python3 &>/dev/null; then
exit 1
fi

export LEARNINGS
python3 -c "
import json, sys
import json, sys, os

key = sys.argv[1]
learnings_file = '$LEARNINGS'
learnings_file = os.environ.get('LEARNINGS')

lines = open(learnings_file).readlines()
# Find the last line with this key and remove it
found_idx = -1
for i, line in enumerate(lines):
for i, line in reversed(list(enumerate(lines))):
try:
d = json.loads(line)
if d.get('key') == key:
found_idx = i
break
Comment on lines +29 to +34

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using reversed(list(enumerate(lines))) creates a new list of tuples in memory for all lines, which is inefficient for very large files. Since the goal of this PR is optimization, we can avoid this memory overhead by iterating over the indices in reverse using reversed(range(len(lines))) and accessing the lines directly by index.

for i in reversed(range(len(lines))):
    try:
        d = json.loads(lines[i])
        if d.get('key') == key:
            found_idx = i
            break

except:
pass

Expand Down