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

sql: remove a bunch of allocations from name and type resolution paths #57201

Merged
merged 5 commits into from
Dec 1, 2020

Conversation

jordanlewis
Copy link
Member

See individual commits. This PR shaves 3% of the total number of allocations in the FlowSetup benchmark via a variety of methods.

name                                          old time/op    new time/op    delta
FlowSetup/vectorize=true/distribute=false-24     144µs ± 1%     142µs ± 2%  -1.01%  (p=0.023 n=10+10)

name                                          old alloc/op   new alloc/op   delta
FlowSetup/vectorize=true/distribute=false-24    28.3kB ± 1%    27.8kB ± 1%  -1.76%  (p=0.000 n=9+9)

name                                          old allocs/op  new allocs/op  delta
FlowSetup/vectorize=true/distribute=false-24       232 ± 0%       224 ± 0%  -3.45%  (p=0.000 n=8+8)

@jordanlewis jordanlewis requested review from a team and adityamaru and removed request for a team November 28, 2020 01:32
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@jordanlewis jordanlewis force-pushed the fewerTypeResolverAllocs branch 4 times, most recently from aec193b to a344a0b Compare November 29, 2020 05:09
@jordanlewis jordanlewis requested a review from a team November 29, 2020 17:12
@yuzefovich yuzefovich removed the request for review from adityamaru November 30, 2020 05:46
Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

nit: not sure where "accessors" in the fifth commit's title comes from.

Reviewed 1 of 1 files at r1, 4 of 4 files at r2, 1 of 1 files at r3, 1 of 1 files at r4, 1 of 1 files at r5.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @jordanlewis)


pkg/sql/execinfra/processorsbase.go, line 867 at r3 (raw file):

	pb.SemaCtx.TypeResolver = resolver

	return pb.Out.Init(post, coreOutputTypes, &pb.SemaCtx, pb.EvalCtx, output)

I don't understand the third commit - where is the allocation removed from?

Copy link
Member Author

@jordanlewis jordanlewis left a comment

Choose a reason for hiding this comment

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

Done.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @yuzefovich)


pkg/sql/execinfra/processorsbase.go, line 867 at r3 (raw file):

Previously, yuzefovich wrote…

I don't understand the third commit - where is the allocation removed from?

The compiler detects that the SemaCtx argument to Init must be heap-allocated. Before, we constructed a new SemaCtx - seemingly on the stack. But because Init needed a heap-allocated SemaCtx, the compiler promotes the SemaCtx to the heap: the object escapes.

Now, we add space on ProcessorBase for a SemaCtx. At this point, ProcessorBase is already on the heap. So, the compiler can directly use the address of ProcessorBase.SemaCtx safely - no allocations needed.

This commit changes the VectorizedFlow to store its type resolver as a
struct directly, rather than a pointer. This prevents a pointless
allocation per flow.

Release note: None
Previously, when initializing a processor, we'd need to allocate a fresh
SemaCtx for every processor. Instead of doing this, we add a SemaCtx
value inside of ProcessorBase to share the ProcessorBase-allocated
memory.

Release note: None
Previously, resolving a table would always need to heap allocate the
TableName that is used to look up the names. This was unnecessary.

Release note: None
@jordanlewis jordanlewis force-pushed the fewerTypeResolverAllocs branch from a344a0b to 0bd8a5d Compare December 1, 2020 04:55
Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 5 of 5 files at r6, 4 of 4 files at r7, 1 of 1 files at r8, 1 of 1 files at r9, 1 of 1 files at r10.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @jordanlewis)


pkg/sql/execinfra/processorsbase.go, line 867 at r3 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

The compiler detects that the SemaCtx argument to Init must be heap-allocated. Before, we constructed a new SemaCtx - seemingly on the stack. But because Init needed a heap-allocated SemaCtx, the compiler promotes the SemaCtx to the heap: the object escapes.

Now, we add space on ProcessorBase for a SemaCtx. At this point, ProcessorBase is already on the heap. So, the compiler can directly use the address of ProcessorBase.SemaCtx safely - no allocations needed.

Got it, thanks.

Copy link
Member Author

@jordanlewis jordanlewis left a comment

Choose a reason for hiding this comment

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

bors r+

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @jordanlewis)

@craig
Copy link
Contributor

craig bot commented Dec 1, 2020

Build succeeded:

@craig craig bot merged commit e6f9184 into cockroachdb:master Dec 1, 2020
@jordanlewis jordanlewis deleted the fewerTypeResolverAllocs branch December 1, 2020 19:21
@RaduBerinde
Copy link
Member

@jordanlewis @lucy-zhang I have a comment related to the "remove pointless log allocs on the hot path" commit. The very long log line changed by that commit adds the full descriptor information to recorded traces, which makes SHOW TRACE FOR SESSION pretty much unusable (see below). Could we backport this commit to 20.2.x and 20.1.x (if the log is there)?

image

@jordanlewis
Copy link
Member Author

Hmm... I'm thinking that we actually didn't mean to remove that from the trace. I think I should have used ExpensiveLogEnabled(ctx,2) instead of V(2)...

@yuzefovich
Copy link
Member

Hm, I made a couple of changes like that too, didn't realize if log.V(n) would start hiding the log message from the trace.

@RaduBerinde
Copy link
Member

Well, as I said, I think we should reconsider dumping that long info in each trace. Or at least split it into multiple logs.

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