The perfect pair programmer?
What is Blogvent? (tl;dr I am going to write one post a day for all of December as a way to practice writing.)
ChatGPT from OpenAI is all the rage these days. A chat bot that is surprisingly good at holding a conversation with you. It will remember previous references, you can ask it to roleplay with you. I even got it to pretend to be King Harald from Norway.
Then we went further
People started to give it code examples, someone even asked to to pretend to be a virtual machine and start pinging other websites. I thought, since it’s acting like a human and you could give it code examples, then the perfect fit would be a programming partner.
So I started off easy, showing it an example of bubble sort in Rust and asking the bot what this is?
Pretty good, I tried a few more examples with varying levels of success. The bot would usually just describe every line and not talk about the bigger picture.
Until I gave it a bit of code that was dividing by a result of some unknown function, then the bot said this.
Wait a minute, the bot can tell me if the bit of code I’m giving it has possible errors?
The bot can review my code? This definitely has some potential, but how good is it?
I gave it a bunch of different code examples to try out. Something simple or something that I knew had problems. Here is the bot on the 3rd point regarding a command line parser I had laying around in some Rust project.
Is it better? I would say yes. The flow through the function is much simpler.
Earlier in the answer it wanted to
io::Result<Args> instead of
io::Result<&'static Args>. It even started on point 4 but there is a limit of how much text it can display in one answer.
Let’s do a bit of code I know has problems.
Almost 10 years ago I wrote a very simple genetic algorithm implementation of the game 2048. Looking at it now, it’s not great but I like it since it shows me who I was as a programmer all those years ago.
So let’s take a bit of code from that repo and give it to the AI. A bit of code that I know has problems, it even has a todo.
There is plenty wrong here and it’s not even fully finished, but the general idea is to take 2 vectors of “parents” and create 2 “children” from a random mix of the parent’s actions.
Here it’s a 1 point crossover so we split the parents in 1 location and swap them to make 2 children. These are then fed back into the population of possible moves in the game and then we try to figure out if those children are better at playing the game of 2048.
Without describing much more, this is what the AI gave me, and I want you to notice a few things here.
Ok, step 1, use
std::deque instead of
std::vector. So I tried that. I dusted off the project and built it, once with vector and once with deque and ran multiple performance tests and took the averages. With
std::deque the average runtime using 10000 iterations was 4.72 seconds. With
std::vector the average runtime was 4.43 seconds. Not huge but definitely a noticeable difference. Is there a benefit of using deque, yeah possibly, but vector still wins, as it often does.
Step 2, use
std::swap. Well, in the example the bot gives me, there is no
std::swap, so we’ll call that an extra optimization. Maybe?
Step 3, use
std::copy. Here is where I was really happy. The AI spotted that the two loops were in fact doing a mixed copy of elements from parent_a, parent_b into child_a, child_b. It turned the two loops into 4 copies (1 for each child/parent combo). There are some issues here since we are fetching parent min/max sizes in the lines above and then the bot fetches them again using
std::minmax, not great but I applaud the effort.
And step 4 I agree with, this should use
I tried running this, it compiles and runs, but as soon as I enable
-O3 or something similar it crashes. There’s an issue with correctly giving each child vector the right size but I’m willing to overlook that issue for now.
I haven’t spent a lot of time with this but from what I’m seeing, there is potential here.
Now I don’t think you can blindly copy these suggestions and I don’t recommend you do. But having a bot like this as an extra rubber duck on your desk that you can have a chat with and get suggestions? Color me impressed.