github.com/matrixorigin/matrixone@v1.2.0/pkg/common/reuse/types.go (about)

     1  // Copyright 2023 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package reuse
    16  
    17  import (
    18  	"os"
    19  	"strings"
    20  )
    21  
    22  // Pool is a pool of temporary objects, designed to reduce the creation of
    23  // temporary objects to reduce GC pressure.
    24  //
    25  // There are many places throughout the system where temporary objects need to
    26  // be created, and if these places are in our hot path, we need to consider using
    27  // a pool to reduce the number of temporary objects.
    28  type Pool[T ReusableObject] interface {
    29  	Alloc() *T
    30  	Free(*T)
    31  }
    32  
    33  // Options options to create object pool
    34  type Options[T ReusableObject] struct {
    35  	release       func(*T)
    36  	enableChecker bool
    37  	memCapacity   int64
    38  
    39  	// for testing
    40  	gcRecover func()
    41  }
    42  
    43  // ReusableObject all reusable objects must implements this interface
    44  type ReusableObject interface {
    45  	// TypeName returns the name of the object type. We cannot use reflect.TypeOf to get
    46  	// the name of the object type, to avoid mem allocate.
    47  	// Outdated, may delete later.
    48  	TypeName() string
    49  }
    50  
    51  func init() {
    52  	spi, ok := os.LookupEnv("mo_reuse_spi")
    53  	if ok {
    54  		switch strings.ToLower(spi) {
    55  		case "sync-pool":
    56  			use(SyncBased)
    57  		case "mpool":
    58  			use(MpoolBased)
    59  		}
    60  	}
    61  
    62  	enable, ok := os.LookupEnv("mo_reuse_enable_checker")
    63  	if ok {
    64  		switch strings.ToLower(enable) {
    65  		case "true":
    66  			enableChecker.Store(true)
    67  		}
    68  	}
    69  
    70  	enable, ok = os.LookupEnv("mo_reuse_enable_checker_verbose")
    71  	if ok {
    72  		switch strings.ToLower(enable) {
    73  		case "true":
    74  			enableChecker.Store(true)
    75  			enableVerbose.Store(true)
    76  		}
    77  	}
    78  }