DEV Community

Cover image for AI Serialization Skill for Go - High Performance, Zero Boilerplate
Yuri Zinchenko
Yuri Zinchenko

Posted on

AI Serialization Skill for Go - High Performance, Zero Boilerplate

I wanted to share a quick story about a weekend experiment.

There is a library called mok that lacks a code generator, so every time you want to mock an interface, it requires you to write all the boilerplate yourself. It's simple but tedious work. I usually just offload it to an AI agent, and with a small example, it completes this task exceptionally well.

This got me thinking - could AI handle something more complex, like writing serialization code? If it could, we'd basically get the best of both worlds: the raw speed of generated code, with the same simplicity you get from reflection-based libraries.

But is it safe to trust AI with something as sensitive as serialization logic? With all its non-determinism and unpredictable behavior, the answer is more likely a hard "No", unless we enforce two strict requirements:

  1. Provide a solid foundation — give the AI a robust library to work with.
  2. Test everything — all AI-generated code must be rigorously tested.

With those guidelines in place, I decided to give it a shot. After a few late nights, and honestly, way more fun than expected, I ended up building mus-skill: an AI skill for generating MUS serialization code.

At its core, the skill is just a set of rules for combining the serialization primitives provided by the mus and mus-stream libraries. In addition to the code itself, it also generates the accompanying tests so you can be sure everything works correctly.

Because the code looks exactly like the output of a traditional code generator you can expect from it the same high-performance.

|     NAME     |  NS/OP   | B/SIZE |  B/OP   | ALLOCS/OP |
|--------------|----------|--------|---------|-----------|
| mus          |   102.90 |  58.00 |   48.00 |         1 |
| protobuf     |   531.70 |  69.00 |  271.00 |         4 |
| json         |  2779.00 | 150.00 |  600.00 |         9 |

Full benchmarks https://github.com/ymz-ncnk/go-serialization-benchmarks
Enter fullscreen mode Exit fullscreen mode

This constrained approach even works flawlessly on deeply nested, complex types (where AI usually hallucinates), such as:

// mus:vl = ValidateLen
// mus:elemVl = ValidateComplexMapValue
type ComplexMap map[string]map[*[]int][][]float32
Enter fullscreen mode Exit fullscreen mode

And supports user hints, so you can easily customize the generated code to fit your exact needs (like specifying custom validators).

Getting started is simple. Just clone this repository into your project's agent directory (e.g., .agents/skills). Or, even easier, install it using the skills CLI tool:

npx skills add github.com/mus-format/mus-skill-go
Enter fullscreen mode Exit fullscreen mode

Once installed, simply give your AI agent a prompt like this:

Generate MUS serializers for the types found in the <file_name>.go file.
Enter fullscreen mode Exit fullscreen mode

The result? Two newly generated files: mus.ai.gen.go and mus.ai.gen_test.go. As always, just be sure to double-check that the generated tests actually cover all your edge cases.

Have you ever tried a similar approach? I'd love to hear how it went for you!

Top comments (1)

Collapse
 
__df499cfa profile image
Максим Галиченко

interesting