github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/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  }