github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/internal/intern/intern.go (about)

     1  // Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package intern
     6  
     7  import "sync"
     8  
     9  var pool = sync.Pool{
    10  	New: func() interface{} {
    11  		return make(map[string]string)
    12  	},
    13  }
    14  
    15  // Bytes returns b converted to a string, interned.
    16  func Bytes(b []byte) string {
    17  	m := pool.Get().(map[string]string)
    18  	c, ok := m[string(b)]
    19  	if ok {
    20  		pool.Put(m)
    21  		return c
    22  	}
    23  	s := string(b)
    24  	m[s] = s
    25  	pool.Put(m)
    26  	return s
    27  }