Change strutil.StringFromBytes to depend on Go 1.3.

Change-Id: I83fd1369a199dba7ef41227df4d0b95d9a16aabb
This commit is contained in:
Brad Fitzpatrick 2014-08-14 12:59:54 -07:00
parent 883c8b752d
commit 17ab1c238f
2 changed files with 33 additions and 32 deletions

View File

@ -41,12 +41,28 @@ import (
"unicode/utf8" "unicode/utf8"
"camlistore.org/pkg/blob" "camlistore.org/pkg/blob"
"camlistore.org/pkg/strutil"
"camlistore.org/pkg/types" "camlistore.org/pkg/types"
"camlistore.org/third_party/github.com/bradfitz/latlong" "camlistore.org/third_party/github.com/bradfitz/latlong"
"camlistore.org/third_party/github.com/camlistore/goexif/exif" "camlistore.org/third_party/github.com/camlistore/goexif/exif"
"camlistore.org/third_party/github.com/camlistore/goexif/tiff" "camlistore.org/third_party/github.com/camlistore/goexif/tiff"
) )
func init() {
// Intern common strings as used by schema blobs (camliType values), to reduce
// index memory usage, which uses strutil.StringFromBytes.
strutil.RegisterCommonString(
"bytes",
"claim",
"directory",
"file",
"permanode",
"share",
"static-set",
"symlink",
)
}
// MaxSchemaBlobSize represents the upper bound for how large // MaxSchemaBlobSize represents the upper bound for how large
// a schema blob may be. // a schema blob may be.
const MaxSchemaBlobSize = 1 << 20 const MaxSchemaBlobSize = 1 << 20

View File

@ -16,39 +16,24 @@ limitations under the License.
package strutil package strutil
// StringFromBytes returns string(v), minimizing copies for common values of v. var internStr = map[string]string{}
// RegisterCommonString adds common strings to the interned string
// table. This should be called during init from the main
// goroutine, not later at runtime.
func RegisterCommonString(s ...string) {
for _, v := range s {
internStr[v] = v
}
}
// StringFromBytes returns string(v), minimizing copies for common values of v
// as previously registered with RegisterCommonString.
func StringFromBytes(v []byte) string { func StringFromBytes(v []byte) string {
// From net/textproto's reader.go... // In Go 1.3, this string conversion in the map lookup does not allocate
if len(v) == 0 { // to make a new string. We depend on Go 1.3, so this is always free:
return "" if s, ok := internStr[string(v)]; ok {
} return s
lo, hi := 0, len(commonStrings)
for i, c := range v {
if lo < hi {
for lo < hi && (len(commonStrings[lo]) <= i || commonStrings[lo][i] < c) {
lo++
}
for hi > lo && commonStrings[hi-1][i] > c {
hi--
}
} else {
break
}
}
if lo < hi && len(commonStrings[lo]) == len(v) {
return commonStrings[lo]
} }
return string(v) return string(v)
} }
// NOTE: must be sorted
var commonStrings = []string{
"bytes",
"claim",
"directory",
"file",
"permanode",
"share",
"static-set",
"symlink",
}