github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/physicalplan/specs.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package physicalplan
    12  
    13  import (
    14  	"sync"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
    18  )
    19  
    20  var flowSpecPool = sync.Pool{
    21  	New: func() interface{} {
    22  		return &execinfrapb.FlowSpec{}
    23  	},
    24  }
    25  
    26  // NewFlowSpec returns a new FlowSpec, which may have non-zero capacity in its
    27  // slice fields.
    28  func NewFlowSpec(flowID execinfrapb.FlowID, gateway roachpb.NodeID) *execinfrapb.FlowSpec {
    29  	spec := flowSpecPool.Get().(*execinfrapb.FlowSpec)
    30  	spec.FlowID = flowID
    31  	spec.Gateway = gateway
    32  	return spec
    33  }
    34  
    35  // ReleaseFlowSpec returns this FlowSpec back to the pool of FlowSpecs. It may
    36  // not be used again after this call.
    37  func ReleaseFlowSpec(spec *execinfrapb.FlowSpec) {
    38  	*spec = execinfrapb.FlowSpec{
    39  		Processors: spec.Processors[:0],
    40  	}
    41  	flowSpecPool.Put(spec)
    42  }
    43  
    44  var trSpecPool = sync.Pool{
    45  	New: func() interface{} {
    46  		return &execinfrapb.TableReaderSpec{}
    47  	},
    48  }
    49  
    50  // NewTableReaderSpec returns a new TableReaderSpec.
    51  func NewTableReaderSpec() *execinfrapb.TableReaderSpec {
    52  	return trSpecPool.Get().(*execinfrapb.TableReaderSpec)
    53  }
    54  
    55  // ReleaseTableReaderSpec puts this TableReaderSpec back into its sync pool. It
    56  // may not be used again after Release returns.
    57  func ReleaseTableReaderSpec(s *execinfrapb.TableReaderSpec) {
    58  	s.Reset()
    59  	trSpecPool.Put(s)
    60  }
    61  
    62  // ReleaseSetupFlowRequest releases the resources of this SetupFlowRequest,
    63  // putting them back into their respective object pools.
    64  func ReleaseSetupFlowRequest(s *execinfrapb.SetupFlowRequest) {
    65  	if s == nil {
    66  		return
    67  	}
    68  	for i := range s.Flow.Processors {
    69  		if tr := s.Flow.Processors[i].Core.TableReader; tr != nil {
    70  			ReleaseTableReaderSpec(tr)
    71  		}
    72  	}
    73  	ReleaseFlowSpec(&s.Flow)
    74  }