Skip to content

Comments

mlkem: reduce allocations#121

Merged
gdams merged 2 commits intomainfrom
dev/qmuntal/mlkemopt
Nov 20, 2025
Merged

mlkem: reduce allocations#121
gdams merged 2 commits intomainfrom
dev/qmuntal/mlkemopt

Conversation

@qmuntal
Copy link
Member

@qmuntal qmuntal commented Nov 20, 2025

Most of MLKEM allocations can be avoided by using big-enough buffer sizes when encoding/decoding CNG blobs. We know those max sizes, so use.

goos: windows
goarch: arm64
pkg: github.com/microsoft/go-crypto-winnative/cng
                        │   old.txt    │              new.txt               │
                        │    sec/op    │   sec/op     vs base               │
MLKEMKeyGen-12            91.96µ ± 16%   81.21µ ± 7%  -11.69% (p=0.001 n=7)
MLKEMEncaps-12            32.45µ ± 16%   28.75µ ± 2%  -11.39% (p=0.001 n=7)
MLKEMDecaps-12            45.30µ ±  1%   44.02µ ± 2%   -2.84% (p=0.002 n=7)
MLKEMRoundTrip/Alice-12   132.7µ ±  3%   124.8µ ± 0%   -5.98% (p=0.001 n=7)
MLKEMRoundTrip/Bob-12     29.63µ ±  3%   28.70µ ± 0%   -3.14% (p=0.001 n=7)
geomean                   55.61µ         51.66µ        -7.09%

                        │   old.txt    │                 new.txt                  │
                        │     B/op     │     B/op      vs base                    │
MLKEMKeyGen-12            1.438Ki ± 0%   0.000Ki ± 0%  -100.00% (p=0.001 n=7)
MLKEMEncaps-12            3.656Ki ± 0%   2.406Ki ± 0%   -34.19% (p=0.001 n=7)
MLKEMDecaps-12             128.00 ± 0%     32.00 ± 0%   -75.00% (p=0.001 n=7)
MLKEMRoundTrip/Alice-12   1600.00 ± 0%     32.00 ± 0%   -98.00% (p=0.001 n=7)
MLKEMRoundTrip/Bob-12     3.656Ki ± 0%   2.406Ki ± 0%   -34.19% (p=0.001 n=7)
geomean                   1.303Ki                      ?                      ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean

                        │  old.txt   │                new.txt                 │
                        │ allocs/op  │ allocs/op   vs base                    │
MLKEMKeyGen-12            3.000 ± 0%   0.000 ± 0%  -100.00% (p=0.001 n=7)
MLKEMEncaps-12            4.000 ± 0%   3.000 ± 0%   -25.00% (p=0.001 n=7)
MLKEMDecaps-12            2.000 ± 0%   1.000 ± 0%   -50.00% (p=0.001 n=7)
MLKEMRoundTrip/Alice-12   5.000 ± 0%   1.000 ± 0%   -80.00% (p=0.001 n=7)
MLKEMRoundTrip/Bob-12     4.000 ± 0%   3.000 ± 0%   -25.00% (p=0.001 n=7)
geomean                   3.438                    ?                      ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes ML-KEM operations by reducing memory allocations through pre-allocated buffers of known maximum sizes. The main change is to allocate buffers upfront using the maximum size needed (ML-KEM 1024 sizes) instead of making two allocation calls per operation.

Key changes:

  • Pre-allocate buffers using ML-KEM 1024 sizes to avoid allocation overhead for both 768 and 1024 parameter sets
  • Simplify error handling by converting helper functions from returning errors to panicking (errors are still properly propagated from public APIs)
  • Remove unnecessary runtime.KeepAlive calls and optimize function signatures

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
cng/mlkem.go Core optimization changes: added buffer size constants, refactored blob creation functions to use pre-allocated buffers, simplified error handling in internal helpers
cng/mlkem_test.go Added t.Parallel() calls to test functions to enable parallel test execution

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@qmuntal qmuntal requested a review from gdams November 20, 2025 11:34
@gdams gdams merged commit 5f60d7c into main Nov 20, 2025
27 checks passed
@gdams gdams deleted the dev/qmuntal/mlkemopt branch November 20, 2025 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants