tinycodinglm / code /bad007.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
2.2 kB
deepseek-harness trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

What are common performance anti-patterns?

Assistant

Bad Practices:

  1. N+1 Query Problem:
python
# Bad: queries inside loop
for user in users:
    orders = db.query(f"SELECT * FROM orders WHERE user_id={user.id}")

# Good: single query with JOIN
orders = db.query("SELECT * FROM orders WHERE user_id IN ?", [user_ids])
  1. String Concatenation in Loop:
python
# Bad: O(n^2)
result = ""
for item in items:
    result += str(item)

# Good: O(n)
result = "".join(str(item) for item in items)
  1. Unnecessary Computation:
python
# Bad: recomputing in loop
for i in range(len(data)):
    processed = expensive_function(data[i])  # same result each time

# Good: compute once
cached = expensive_function(data[0])
for i in range(len(data)):
    use(cached)
  1. Loading Entire File Into Memory:
python
# Bad: memory issues with large files
data = open("huge_file.txt").read()

# Good: process line by line
with open("huge_file.txt") as f:
    for line in f:
        process(line)
  1. Premature Optimization:
python
# Bad: optimizing rarely-used code
@lru_cache(maxsize=10000)
def rarely_called(x):
    return x * 2

# Good: profile first, optimize bottlenecks
  1. Blocking I/O in Async Code:
python
# Bad
async def fetch():
    data = requests.get(url)  # blocks entire event loop

# Good
async def fetch():
    async with aiohttp.ClientSession() as session:
        data = await session.get(url)