Skip to content

Conversation

@timvaillancourt
Copy link
Contributor

@timvaillancourt timvaillancourt commented Nov 17, 2025

Description

This PR introduces the prealloc linter to our golangci-lint config, in order to reduce allocations in the runtime

prealloc is a Go static analysis tool to find slice declarations that could potentially be preallocated.

Included are manual fixes to 116 files 😱 that should be reviewed carefully. Here is the method I followed:

  1. If the slice is appended-to using an int range-loop, I set the cap == the int
  2. If the slice is appended-to 1:1 by a range-loop, I set the cap == len(whatWeRangedOn)
  3. If the slice is appended-to by a range-loop AND individual appends, I set the cap == len(whatWeRangedOn) + #, where # is the number of individual appends
    • This should be manually reviewed carefully incase my math was wrong 👀
    • If we aren't confident in this part of the change, we could just use a cap == 0 at some minor cost
  4. If it's not clear how many appends a slice needs, I just used a cap of 0

While I was cleaning up, I removed the empty .gitmodules file in the repo root, and mentions of Godeps and php in .dockerfile + .gitignore (both are long gone) 🧹

Related Issue(s)

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

AI Disclosure

Signed-off-by: Tim Vaillancourt <[email protected]>
Signed-off-by: Tim Vaillancourt <[email protected]>
Signed-off-by: Tim Vaillancourt <[email protected]>
Signed-off-by: Tim Vaillancourt <[email protected]>
Signed-off-by: Tim Vaillancourt <[email protected]>
@vitess-bot
Copy link
Contributor

vitess-bot bot commented Nov 17, 2025

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Nov 17, 2025
@github-actions github-actions bot added this to the v24.0.0 milestone Nov 17, 2025
@timvaillancourt timvaillancourt removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Nov 17, 2025
@timvaillancourt timvaillancourt self-assigned this Nov 17, 2025
Signed-off-by: Tim Vaillancourt <[email protected]>
Comment on lines +148 to +150
exportedStructFields = make([]reflect.StructField, 0, len(visibleFields))
fields = make([]*querypb.Field, 0, len(visibleFields))
rows = make([]Row, 0, val.Len())
Copy link
Member

Choose a reason for hiding this comment

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

Claude pointed out here that the len(visibleFields) will most likely over-allocate, because not all fields might be exported. I don't think that's an issue, just potentially wasteful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is expected because we sometimes continue. But an upper limit should be more efficient than a capacity of zero

@timvaillancourt
Copy link
Contributor Author

This PR is also causing this unit test failure because of a change to nil-ness:

2025-11-17T16:59:18.2318158Z === RUN   TestPlanTestSuite/TestPlan/misc_cases.json/execute_no_param_statement
2025-11-17T16:59:18.2318412Z     plan_test.go:704: misc_cases.json
2025-11-17T16:59:18.2318509Z         Diff:
2025-11-17T16:59:18.2318593Z         {
2025-11-17T16:59:18.2318748Z             "Instructions": {
2025-11-17T16:59:18.2318906Z                 "Inputs": [
2025-11-17T16:59:18.2319063Z                     {
2025-11-17T16:59:18.2319517Z                         "FieldQuery": "select 1 from `user` where 1 != 1",
2025-11-17T16:59:18.2319746Z                         "Keyspace": {
2025-11-17T16:59:18.2320009Z                             "Name": "user",
2025-11-17T16:59:18.2320276Z                             "Sharded": true
2025-11-17T16:59:18.2320437Z                         },
2025-11-17T16:59:18.2320725Z                         "OperatorType": "Route",
2025-11-17T16:59:18.2321061Z                         "Query": "select 1 from `user`",
2025-11-17T16:59:18.2321316Z                         "Variant": "Scatter"
2025-11-17T16:59:18.2321444Z                     }
2025-11-17T16:59:18.2321560Z                 ],
2025-11-17T16:59:18.2321775Z                 "OperatorType": "EXECUTE",
2025-11-17T16:59:18.2322220Z                 "Parameters": �[0;33mnull => []�[0m
2025-11-17T16:59:18.2322315Z             },
2025-11-17T16:59:18.2322531Z             "Original": "execute prep_no_param",
2025-11-17T16:59:18.2322703Z             "QueryType": "EXECUTE",
2025-11-17T16:59:18.2322841Z             "TablesUsed": [
2025-11-17T16:59:18.2322988Z                 "user.user"
2025-11-17T16:59:18.2323087Z             ],
2025-11-17T16:59:18.2323231Z             "Type": "Complex"
2025-11-17T16:59:18.2323309Z         }
2025-11-17T16:59:18.2323391Z         [{
2025-11-17T16:59:18.2323552Z               "Type": "Complex",
2025-11-17T16:59:18.2323742Z               "QueryType": "EXECUTE",
2025-11-17T16:59:18.2323974Z               "Original": "execute prep_no_param",
2025-11-17T16:59:18.2324132Z               "Instructions": {
2025-11-17T16:59:18.2324422Z                 "OperatorType": "EXECUTE",
2025-11-17T16:59:18.2324611Z                 "Parameters": null,
2025-11-17T16:59:18.2324755Z                 "Inputs": [
2025-11-17T16:59:18.2324872Z                   {
2025-11-17T16:59:18.2325116Z                     "OperatorType": "Route",
2025-11-17T16:59:18.2325336Z                     "Variant": "Scatter",
2025-11-17T16:59:18.2325523Z                     "Keyspace": {
2025-11-17T16:59:18.2325734Z                       "Name": "user",
2025-11-17T16:59:18.2325950Z                       "Sharded": true
2025-11-17T16:59:18.2326139Z                     },
2025-11-17T16:59:18.2326528Z                     "FieldQuery": "select 1 from `user` where 1 != 1",
2025-11-17T16:59:18.2326808Z                     "Query": "select 1 from `user`"
2025-11-17T16:59:18.2326918Z                   }
2025-11-17T16:59:18.2327019Z                 ]
2025-11-17T16:59:18.2327124Z               },
2025-11-17T16:59:18.2327271Z               "TablesUsed": [
2025-11-17T16:59:18.2327416Z                 "user.user"
2025-11-17T16:59:18.2327522Z               ]
2025-11-17T16:59:18.2327615Z             }] 
2025-11-17T16:59:18.2327731Z         [{
2025-11-17T16:59:18.2327974Z           "Type": "Complex",
2025-11-17T16:59:18.2328120Z           "QueryType": "EXECUTE",
2025-11-17T16:59:18.2328318Z           "Original": "execute prep_no_param",
2025-11-17T16:59:18.2328447Z           "Instructions": {
2025-11-17T16:59:18.2328622Z             "OperatorType": "EXECUTE",
2025-11-17T16:59:18.2328839Z             "Parameters": [],
2025-11-17T16:59:18.2328967Z             "Inputs": [
2025-11-17T16:59:18.2329062Z               {
2025-11-17T16:59:18.2329275Z                 "OperatorType": "Route",
2025-11-17T16:59:18.2329459Z                 "Variant": "Scatter",
2025-11-17T16:59:18.2329617Z                 "Keyspace": {
2025-11-17T16:59:18.2329791Z                   "Name": "user",
2025-11-17T16:59:18.2329963Z                   "Sharded": true
2025-11-17T16:59:18.2330075Z                 },
2025-11-17T16:59:18.2330405Z                 "FieldQuery": "select 1 from `user` where 1 != 1",
2025-11-17T16:59:18.2330721Z                 "Query": "select 1 from `user`"
2025-11-17T16:59:18.2330823Z               }
2025-11-17T16:59:18.2330911Z             ]
2025-11-17T16:59:18.2331003Z           },
2025-11-17T16:59:18.2331129Z           "TablesUsed": [
2025-11-17T16:59:18.2331250Z             "user.user"
2025-11-17T16:59:18.2331339Z           ]
2025-11-17T16:59:18.2331415Z         }
2025-11-17T16:59:18.2331494Z         ]

Signed-off-by: Tim Vaillancourt <[email protected]>

func generateOwnedVindexQuery(del *sqlparser.Delete, table TargetTable, ksidCols []sqlparser.IdentifierCI) *sqlparser.Select {
var selExprs []sqlparser.SelectExpr
selExprs := make([]sqlparser.SelectExpr, 0)
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem right? No len(ksidCols)?

Copy link
Contributor Author

@timvaillancourt timvaillancourt Nov 18, 2025

Choose a reason for hiding this comment

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

@dbussink I kept capacity zero here because of this sub-loop that I don't really know the capacity length of up-front:

for _, col := range cv.Columns {
colName := makeColName(col, table, sqlparser.MultiTable(del.TableExprs))
selExprs = append(selExprs, aeWrap(colName))
}

}

var newOption []*VindexOption
newOption := make([]*VindexOption, 0, len(v.Options))
Copy link
Member

Choose a reason for hiding this comment

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

I don't know about this specific case, but in general, is it really correct to allocate it all everywhere? Do we not have cases where it's more the exception an element is added and not the common case? And we very much over-allocate?

That's a general issue with this linter, it's not very smart and complains about every case, while not every case makes really sense to convert.

Copy link
Contributor Author

@timvaillancourt timvaillancourt Nov 18, 2025

Choose a reason for hiding this comment

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

I don't know about this specific case, but in general, is it really correct to allocate it all everywhere?

@dbussink I haven't validated it myself, but I've consistently read (and just confirmed with ChatGPG) that allocating a slice with a fixed capacity is "usually" more efficient, because a zero-capacity slice has to be grown every append, causing new allocations for a new backing array each append

To my knowledge the risk to using a capacity that may/may not be 100% used is memory usage that may just be filled with empty values. But there must be cases where this isn't always true, perhaps really huge slices(?). I wondered if you had any scenarios in mind?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants