Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linker: do not copy instructions when there is no reference in flattenInstructions #1647

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,19 @@ func flattenPrograms(progs map[string]*ProgramSpec, names []string) {
// dependencies of each program.
func flattenInstructions(name string, progs map[string]*ProgramSpec, refs map[*ProgramSpec][]string) asm.Instructions {
prog := progs[name]
progRefs := refs[prog]

if len(progRefs) == 0 {
// No references, nothing to do.
return prog.Instructions
}

insns := make(asm.Instructions, len(prog.Instructions))
copy(insns, prog.Instructions)

// Add all direct references of prog to the list of to be linked programs.
pending := make([]string, len(refs[prog]))
copy(pending, refs[prog])
pending := make([]string, len(progRefs))
copy(pending, progRefs)

// All references for which we've appended instructions.
linked := make(map[string]bool)
Expand Down
21 changes: 21 additions & 0 deletions linker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,24 @@ func TestSplitSymbols(t *testing.T) {
qt.Assert(t, qt.HasLen(m["sym3"], 3))
qt.Assert(t, qt.HasLen(m["sym4"], 4))
}

func TestFlattenInstructionsAllocations(t *testing.T) {
name := "entrypoint"
instructions := asm.Instructions{
asm.LoadImm(asm.R0, 0, asm.DWord),
asm.Return(),
}
prog := &ProgramSpec{
Name: name,
Instructions: instructions,
}
progs := map[string]*ProgramSpec{name: prog}
refs := make(map[*ProgramSpec][]string)

// ensure that flattenInstructions does not allocate memory
// if there is no reference for the given program.
allocs := testing.AllocsPerRun(5, func() {
_ = flattenInstructions(name, progs, refs)
})
qt.Assert(t, qt.Equals(allocs, float64(0)))
}
Loading