Preallocate space for variables in run_context #1364

Merged
mfenniak merged 1 commit from steveno/runner:preallocate_execs into main 2026-02-14 20:01:19 +00:00
Contributor

This should help with reducing allocations during runtime which,
in theory, could provide a performance boost. These changes were
recommended by the prealloc lint.

  • other
    • PR: Preallocate space for variables in run_context
This should help with reducing allocations during runtime which, in theory, could provide a performance boost. These changes were recommended by the prealloc lint. <!--start release-notes-assistant--> <!--URL:https://code.forgejo.org/forgejo/runner--> - other - [PR](https://code.forgejo.org/forgejo/runner/pulls/1364): <!--number 1364 --><!--line 0 --><!--description UHJlYWxsb2NhdGUgc3BhY2UgZm9yIHZhcmlhYmxlcyBpbiBydW5fY29udGV4dA==-->Preallocate space for variables in run_context<!--description--> <!--end release-notes-assistant-->
Preallocate space for variables in run_context
Some checks failed
issue-labels / release-notes (pull_request_target) Successful in 3s
checks / Build Forgejo Runner (pull_request) Has been cancelled
checks / Build unsupported platforms (pull_request) Has been cancelled
checks / runner exec tests (pull_request) Has been cancelled
checks / integration tests (docker-latest) (pull_request) Has been cancelled
checks / integration tests (docker-stable) (pull_request) Has been cancelled
checks / validate mocks (pull_request) Has been cancelled
checks / validate pre-commit-hooks file (pull_request) Has been cancelled
1543ad3148
viceice left a comment
Owner

can you please extend PR description what this improves?

can you please extend PR description what this improves?
Author
Contributor

I re-enabled a lot of the linters provided by golangci-lint and realized I could correct a lot of the issues it highlighted.

I re-enabled a lot of the linters provided by golangci-lint and realized I could correct a lot of the issues it highlighted.
Owner

@steveno wrote in #1364 (comment):

I re-enabled a lot of the linters provided by golangci-lint and realized I could correct a lot of the issues it highlighted.

Nice! Can you include enabling the related linter into this PR?

@steveno wrote in https://code.forgejo.org/forgejo/runner/pulls/1364#issuecomment-77769: > I re-enabled a lot of the linters provided by golangci-lint and realized I could correct a lot of the issues it highlighted. Nice! Can you include enabling the related linter into this PR?
steveno force-pushed preallocate_execs from 1543ad3148
Some checks failed
issue-labels / release-notes (pull_request_target) Successful in 3s
checks / Build Forgejo Runner (pull_request) Has been cancelled
checks / Build unsupported platforms (pull_request) Has been cancelled
checks / runner exec tests (pull_request) Has been cancelled
checks / integration tests (docker-latest) (pull_request) Has been cancelled
checks / integration tests (docker-stable) (pull_request) Has been cancelled
checks / validate mocks (pull_request) Has been cancelled
checks / validate pre-commit-hooks file (pull_request) Has been cancelled
to a02fe2b742
Some checks failed
cascade / debug (pull_request_target) Has been skipped
cascade / forgejo (pull_request_target) Has been skipped
cascade / end-to-end (pull_request_target) Has been skipped
issue-labels / release-notes (pull_request_target) Successful in 4s
checks / validate pre-commit-hooks file (pull_request) Successful in 40s
checks / Build Forgejo Runner (pull_request) Successful in 47s
checks / validate mocks (pull_request) Successful in 53s
checks / runner exec tests (pull_request) Failing after 15s
checks / Build unsupported platforms (pull_request) Successful in 1m1s
checks / integration tests (docker-stable) (pull_request) Failing after 1m34s
checks / integration tests (docker-latest) (pull_request) Failing after 1m42s
Integration tests for the release process / release-simulation (pull_request) Successful in 4m6s
2026-02-12 05:25:32 +00:00
Compare
Author
Contributor

Since the group seems receptive to the changes, I've added more to make this MR worth merging.

Can you include enabling the related linter into this PR?

I'd rather not for a few reasons. First, this MR doesn't correct all the issues for the reenabled linter and I don't want all the pipelines to start failing by default. Second, at least on my machine, the linter is acting a little goofy. For example, I updated act/runner/run_context.go:809 from 0 to len(rc.ServiceContainers) and now its spitting this at me - which is obviously wrong.

act/runner/run_context.go:809:3: Consider preallocating execs with capacity len(rc.ServiceContainers) + len(rc.ServiceContainers) (prealloc)
                execs := make([]common.Executor, len(rc.ServiceContainers))

Finally, @viceice asked me to update the PR description. Do you mean the commit message or the blurb here on forgejo?

Since the group seems receptive to the changes, I've added more to make this MR worth merging. > Can you include enabling the related linter into this PR? I'd rather not for a few reasons. First, this MR doesn't correct _all_ the issues for the reenabled linter and I don't want all the pipelines to start failing by default. Second, at least on my machine, the linter is acting a little goofy. For example, I updated `act/runner/run_context.go:809` from `0` to `len(rc.ServiceContainers)` and now its spitting this at me - which is obviously wrong. ``` act/runner/run_context.go:809:3: Consider preallocating execs with capacity len(rc.ServiceContainers) + len(rc.ServiceContainers) (prealloc) execs := make([]common.Executor, len(rc.ServiceContainers)) ``` Finally, @viceice asked me to update the PR description. Do you mean the commit message or the blurb here on forgejo?
Owner

just a short description why this change is worth merging. it's not obvious from PR title/ commit message/ code change

just a short description why this change is worth merging. it's not obvious from PR title/ commit message/ code change
steveno force-pushed preallocate_execs from a02fe2b742
Some checks failed
cascade / debug (pull_request_target) Has been skipped
cascade / forgejo (pull_request_target) Has been skipped
cascade / end-to-end (pull_request_target) Has been skipped
issue-labels / release-notes (pull_request_target) Successful in 4s
checks / validate pre-commit-hooks file (pull_request) Successful in 40s
checks / Build Forgejo Runner (pull_request) Successful in 47s
checks / validate mocks (pull_request) Successful in 53s
checks / runner exec tests (pull_request) Failing after 15s
checks / Build unsupported platforms (pull_request) Successful in 1m1s
checks / integration tests (docker-stable) (pull_request) Failing after 1m34s
checks / integration tests (docker-latest) (pull_request) Failing after 1m42s
Integration tests for the release process / release-simulation (pull_request) Successful in 4m6s
to 07bba2df35
Some checks failed
cascade / forgejo (pull_request_target) Has been skipped
cascade / debug (pull_request_target) Has been skipped
cascade / end-to-end (pull_request_target) Has been skipped
issue-labels / release-notes (pull_request_target) Successful in 5s
Integration tests for the release process / release-simulation (pull_request) Has been cancelled
checks / Build Forgejo Runner (pull_request) Has been cancelled
checks / Build unsupported platforms (pull_request) Has been cancelled
checks / runner exec tests (pull_request) Has been cancelled
checks / integration tests (docker-latest) (pull_request) Has been cancelled
checks / integration tests (docker-stable) (pull_request) Has been cancelled
checks / validate mocks (pull_request) Has been cancelled
checks / validate pre-commit-hooks file (pull_request) Has been cancelled
2026-02-13 04:59:08 +00:00
Compare
Author
Contributor

@viceice I amended the git commit message and force pushed. Please let me know if further adjustments are needed.

@viceice I amended the git commit message and force pushed. Please let me know if further adjustments are needed.
Owner

@steveno wrote in #1364 (comment):

@viceice I amended the git commit message and force pushed. Please let me know if further adjustments are needed.

why didn't you added it to the PR description above as told? I've done it now. it makes review much easier because you can see the description much more prominent

@steveno wrote in https://code.forgejo.org/forgejo/runner/pulls/1364#issuecomment-77893: > @viceice I amended the git commit message and force pushed. Please let me know if further adjustments are needed. why didn't you added it to the PR description above as told? I've done it now. it makes review much easier because you can see the description much more prominent
Owner

All of these preallocations are implemented slightly incorrectly, resulting in significant logic errors.

		names := make([]string, len(s.Runs))
		for _, r := range s.Runs {
 				names = append(names, r.JobID)
		}

This will create an array of length len(s.Runs), full of zero values. And then it will append new values to the end, resulting in an array that is 2*len(s.Runs) long.

What is intended here is names := make([]string, 0, len(s.Runs)), which creates an array of length 0, but capacity len(s.Runs) so that the append statement doesn't have to allocate memory.

All of these preallocations are implemented slightly incorrectly, resulting in significant logic errors. ```go names := make([]string, len(s.Runs)) for _, r := range s.Runs { names = append(names, r.JobID) } ``` This will create an array of **length** `len(s.Runs)`, full of zero values. And then it will append new values to the end, resulting in an array that is `2*len(s.Runs)` long. What is intended here is `names := make([]string, 0, len(s.Runs))`, which creates an array of length 0, but capacity `len(s.Runs)` so that the `append` statement doesn't have to allocate memory.
Author
Contributor

@viceice wrote in #1364 (comment):

@steveno wrote in #1364 (comment):

@viceice I amended the git commit message and force pushed. Please let me know if further adjustments are needed.

why didn't you added it to the PR description above as told? I've done it now. it makes review much easier because you can see the description much more prominent

I'm sorry, I was a little confused. Next time I'll make sure both are filled out more fully.

@viceice wrote in https://code.forgejo.org/forgejo/runner/pulls/1364#issuecomment-77894: > @steveno wrote in #1364 (comment): > > > @viceice I amended the git commit message and force pushed. Please let me know if further adjustments are needed. > > why didn't you added it to the PR description above as told? I've done it now. it makes review much easier because you can see the description much more prominent I'm sorry, I was a little confused. Next time I'll make sure both are filled out more fully.
Author
Contributor

@mfenniak wrote in #1364 (comment):

All of these preallocations are implemented slightly incorrectly, resulting in significant logic errors.

This is a great catch! Did you catch this as part of the test suite, or did you just recognize I wrote them incorrectly?

@mfenniak wrote in https://code.forgejo.org/forgejo/runner/pulls/1364#issuecomment-77993: > All of these preallocations are implemented slightly incorrectly, resulting in significant logic errors. This is a great catch! Did you catch this as part of the test suite, or did you just recognize I wrote them incorrectly?
steveno force-pushed preallocate_execs from 07bba2df35
Some checks failed
cascade / forgejo (pull_request_target) Has been skipped
cascade / debug (pull_request_target) Has been skipped
cascade / end-to-end (pull_request_target) Has been skipped
issue-labels / release-notes (pull_request_target) Successful in 5s
Integration tests for the release process / release-simulation (pull_request) Has been cancelled
checks / Build Forgejo Runner (pull_request) Has been cancelled
checks / Build unsupported platforms (pull_request) Has been cancelled
checks / runner exec tests (pull_request) Has been cancelled
checks / integration tests (docker-latest) (pull_request) Has been cancelled
checks / integration tests (docker-stable) (pull_request) Has been cancelled
checks / validate mocks (pull_request) Has been cancelled
checks / validate pre-commit-hooks file (pull_request) Has been cancelled
to 220bcdb865
All checks were successful
issue-labels / release-notes (pull_request_target) Successful in 4s
checks / validate pre-commit-hooks file (pull_request) Successful in 37s
checks / Build Forgejo Runner (pull_request) Successful in 44s
checks / validate mocks (pull_request) Successful in 52s
checks / runner exec tests (pull_request) Successful in 34s
checks / Build unsupported platforms (pull_request) Successful in 55s
Integration tests for the release process / release-simulation (pull_request) Successful in 4m19s
checks / integration tests (docker-latest) (pull_request) Successful in 13m44s
checks / integration tests (docker-stable) (pull_request) Successful in 14m43s
cascade / debug (pull_request_target) Has been skipped
cascade / end-to-end (pull_request_target) Successful in 4s
cascade / forgejo (pull_request_target) Successful in 1m23s
2026-02-14 19:25:31 +00:00
Compare
mfenniak approved these changes 2026-02-14 20:01:03 +00:00
mfenniak left a comment
Owner

Looks good to me, thanks for the contribution!

@steveno wrote in #1364 (comment):

This is a great catch! Did you catch this as part of the test suite, or did you just recognize I wrote them incorrectly?

Just code review. I'm pretty sure that many tests would have been broken, but I didn't trigger CI to run once I noted the problem. 🙂

Looks good to me, thanks for the contribution! @steveno wrote in https://code.forgejo.org/forgejo/runner/pulls/1364#issuecomment-78042: > This is a great catch! Did you catch this as part of the test suite, or did you just recognize I wrote them incorrectly? Just code review. I'm pretty sure that many tests would have been broken, but I didn't trigger CI to run once I noted the problem. 🙂
steveno deleted branch preallocate_execs 2026-02-14 20:33:45 +00:00
Owner

@steveno wrote in #1364 (comment):

@viceice wrote in #1364 (comment):

@steveno wrote in #1364 (comment):

@viceice I amended the git commit message and force pushed. Please let me know if further adjustments are needed.

why didn't you added it to the PR description above as told? I've done it now. it makes review much easier because you can see the description much more prominent

I'm sorry, I was a little confused. Next time I'll make sure both are filled out more fully.

PR description is more important, because we mostly do squash merge, so only the PR description lands in default branch commit message. 🤗

@steveno wrote in https://code.forgejo.org/forgejo/runner/pulls/1364#issuecomment-78041: > @viceice wrote in #1364 (comment): > > > @steveno wrote in #1364 (comment): > > > @viceice I amended the git commit message and force pushed. Please let me know if further adjustments are needed. > > > > > > why didn't you added it to the PR description above as told? I've done it now. it makes review much easier because you can see the description much more prominent > > I'm sorry, I was a little confused. Next time I'll make sure both are filled out more fully. PR description is more important, because we mostly do squash merge, so only the PR description lands in default branch commit message. 🤗
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
forgejo/runner!1364
No description provided.