pingcap / pingcap/tidb

create NewSessionVars by cloning cached vars

Open
#36,200 5 comments 1 reaction 1 assignee Claimed by @CbcWestwolf View on GitHub
type/enhancement
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Enhancement

Currently SessionVars() are created in two steps:

1. Calling [NewSessionVars()](https://github.com/pingcap/tidb/blob/24eb419ca2da36cf689fe9923380a2cea8a32227/sessionctx/variable/session.go#L1319-L1465) which populates the vars with defaults
2. Calling [loadCommonGlobalVariablesIfNeeded()](https://github.com/pingcap/tidb/blob/24eb419ca2da36cf689fe9923380a2cea8a32227/session/session.go#L2978-L3006) to copy the cached variables from the sysvar cache, and call init functions.

I suggest the following changes:

1. Remove the loadCommonGlobalVariablesIfNeeded() step.
1. In NewSessionVars(), deep copy a pre-build SessionVars() that was built using similar code in loadCommonGlobalVariablesIfNeeded + calling the init functions.

(Or something logically similar to this. There may be some ordering or cyclic dependency problems which make it complex).

The rationale for this change is:

1. It reduces duplicate code. Setting the defaults in NewSessionVars() is currently a redundant step.
2. It improves session create performance. Calling an int func (SetSession) for each variable on creating a session likely isn't great.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.