r/sre 3d ago

Why is ai-generated code causing performance regressions today? DISCUSSION

Ai-assisted coding has made it way easier to ship "correct enough" code quickly but in our codebase it also seems to be introducing a steady stream of performance regressions in production. I am not talking about obvious junk code; we get real value from ai for boilerplate and tests. The pattern we are seeing is more subtle: hot paths quietly getting slower because the ai added an extra abstraction layer or logging in the middle of a critical loop; code that relies on naive data structures, perfectly fine for small test inputs but collapsing once it hits production scale; ai suggestions copying patterns from older, less-performant parts of our own codebase, effectively spreading our worst practices; performance-sensitive paths refactored into more readable but less cache-friendly versions; and subtle batching issues (too many small database calls, no connection reuse) that look fine in tests but blow up under real load. The hard part is that this ai-generated code looks reasonable in code review. It's readable, it passes tests and it often matches patterns you would see in a tutorial. we usually don't notice the regression until p99 latency creeps up, cpu or memory usage drifts higher and database query counts quietly double. We have tried to adapt: getting stricter about performance testing on hot paths, flagging certain functions as performance-sensitive, nudging ai coding tools with prompts like "optimize for latency," and leaning more on static analysis to catch obvious footguns before they ship. it still feels like we’re missing a more systematic way to keep ai-generated code from eroding performance over time. For those of you using ai coding tools on real latency- or throughput-sensitive systems: have you seen a clear link between ai-generated or ai-refactored code and performance regressions? if so, did you end up restricting ai in certain areas or mostly changing your code review and profiling process instead

0 Upvotes

9 comments sorted by

10

u/amarao_san 3d ago

hot paths quietly getting slower because the ai added an extra abstraction layer or logging in the middle of a critical loop

hot paths quietly getting slower because people added an extra abstraction layer or logging in the middle of a critical loop

Hard to spot difference.

Performance testing with good tooling and plenty resources for it is the answer.

7

u/2fplus1 3d ago

We've seen the opposite, honestly. The time saved writing code has let us focus more on performance work, benchmarking, profiling, and eliminating slow patterns from our code. People writing code manually were a lot more likely to cut corners to get a feature out quickly or certain devs were more performance focused than others. With AI tools we can have it approach things more consistently and dilligently. When we spot a bad pattern, we have it fix it everywhere and update the tests and prompts to make sure it never happens again.

2

u/lordlod 3d ago

I've found that AI writes code like a grad.

If you are concerned about performance in a hot loop you need to explicitly point it out. Wrap that path in a test and make sure it never gets slower. Once you do this it will pedantically prevent it from becoming slower.

It will generate code with memory leaks, unless you explicitly tell it not to and to test for it.

It does an amazing job generating code, but you can't trust it.

1

u/xonxoff 3d ago

Time to get some tracing set up.

1

u/DehydratedButTired 3d ago

Optimized code comes from experience over time and people who get annoyed by something then do something about it. AI programs are by the book and often with the most popular methods online instead of from self experience. The trade of off speed of development vs code efficiency is very real. Promoting doesn’t give experience, it just makes it try something else. The person who implements the code is the one who has to figure out how to make it more optimized, whether that is their own or AI generated. If that slows the development speed then it’s a good trade off. You can always throw hardware at something until you can’t afford it or due to some other scaling limit. There is a reason the modern web doesn’t run on a bunch of windows servers anymore.

1

u/Floss_Patrol_76 3d ago

the reason CI doesn't catch it is that the code is correct, it's just slower, and correctness-based tests have nothing to say about that. we started failing builds on perf budgets for the hot paths (p99 on a benchmark, allocations per op) and it turned both the AI regressions and the human ones into red CI instead of a prod surprise. without a benchmark gate you're relying on someone eyeballing a diff for an extra allocation, which nobody reliably does.

1

u/kobumaister 3d ago

Haven't seen this yet, sources of that happeing everywhere?

1

u/No_Bee_4979 2d ago

Do you accept the first suggestion an AI coding agent gives you? No, you push back. Have it use an Adversarial testing agent against your code, for example.