Skip to content

runtime: add ClearSpan experiment to clear spans on first allocation - #162

Open
lxq015 wants to merge 1 commit into
go1.26.5-zte-devfrom
clearspan
Open

lxq015 wants to merge 1 commit into
go1.26.5-zte-devfrom
clearspan

Conversation

@lxq015

@lxq015 lxq015 commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

Port of https://gitcode.com/openeuler/golang/pull/61 to the ZTE RISC-V toolchain.

goos: linux
goarch: riscv64
pkg: runtime
cpu: Spacemit(R) X60

                  │ bench_malloc_clearspan_before.txt │ bench_malloc_clearspan_after.txt │
                  │                      sec/op                      │           sec/op             vs base               │
Malloc8                                                  101.4n ± 6%                   103.6n ± 4%        ~ (p=0.290 n=6)
Malloc16                                                 180.8n ± 1%                   145.2n ± 3%  -19.64% (p=0.002 n=6)
Malloc32                                                 218.4n ± 0%                   172.8n ± 0%  -20.90% (p=0.002 n=6)
MallocTypeInfo8                                          151.6n ± 2%                   128.7n ± 2%  -15.11% (p=0.002 n=6)
MallocTypeInfo16                                         205.8n ± 8%                   183.3n ± 1%  -10.95% (p=0.002 n=6)
MallocTypeInfo32                                         244.6n ± 1%                   210.9n ± 1%  -13.78% (p=0.002 n=6)
MallocLargeStruct                                        1.432µ ± 1%                   1.385µ ± 1%   -3.25% (p=0.002 n=6)
geomean                                                  238.4n                        209.8n       -11.98%

This change introduces the ClearSpan GOEXPERIMENT, which clears an entire span when the first object is allocated from a span that still requires zeroing.

The ClearSpan path moves zeroing work from each individual small allocation to the first allocation from the span. Once the span is cleared, needzero is reset so later allocations from the same span can avoid repeated per-object clearing. Tiny allocation zeroing is also adjusted to avoid redundant clearing when the span has already been zeroed.

To enable this experiment, build the Go compiler with GOEXPERIMENT=tinysize.

Tests are added to validate that ClearSpan resets needzero and clears the backing memory only when the experiment is enabled.

Authored-by: cyf_123123

@wangpc-pp wangpc-pp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

+2

@ctk-1998 ctk-1998 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

+1

@wenchangping wenchangping left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

+1

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.

4 participants