summaryrefslogtreecommitdiff
path: root/lib/btrfs/btrfsitem/item_fileextent.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-11 22:33:44 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-12 16:17:27 -0700
commitacbbfafa07922b458506b91a58f3a082da453fd1 (patch)
tree020cb033adb3809bcc8ffed5f56ba8d167b32dd1 /lib/btrfs/btrfsitem/item_fileextent.go
parent1f3a1a474ac41f427e4c5b0d27d2c85a3cfaa65e (diff)
Try to avoid unnecessary allocations in enum-types' String methods
Diffstat (limited to 'lib/btrfs/btrfsitem/item_fileextent.go')
-rw-r--r--lib/btrfs/btrfsitem/item_fileextent.go36
1 files changed, 19 insertions, 17 deletions
diff --git a/lib/btrfs/btrfsitem/item_fileextent.go b/lib/btrfs/btrfsitem/item_fileextent.go
index ce6c45c..6b08897 100644
--- a/lib/btrfs/btrfsitem/item_fileextent.go
+++ b/lib/btrfs/btrfsitem/item_fileextent.go
@@ -106,6 +106,12 @@ const (
FILE_EXTENT_PREALLOC
)
+var fileExtentTypeNames = []string{
+ "inline",
+ "regular",
+ "prealloc",
+}
+
func (o FileExtent) Size() (int64, error) {
switch o.Type {
case FILE_EXTENT_INLINE:
@@ -118,14 +124,9 @@ func (o FileExtent) Size() (int64, error) {
}
func (fet FileExtentType) String() string {
- names := map[FileExtentType]string{
- FILE_EXTENT_INLINE: "inline",
- FILE_EXTENT_REG: "regular",
- FILE_EXTENT_PREALLOC: "prealloc",
- }
- name, ok := names[fet]
- if !ok {
- name = "unknown"
+ name := "unknown"
+ if int(fet) < len(fileExtentTypeNames) {
+ name = fileExtentTypeNames[fet]
}
return fmt.Sprintf("%d (%s)", fet, name)
}
@@ -139,16 +140,17 @@ const (
COMPRESS_ZSTD
)
+var compressionTypeNames = []string{
+ "none",
+ "zlib",
+ "lzo",
+ "zstd",
+}
+
func (ct CompressionType) String() string {
- names := map[CompressionType]string{
- COMPRESS_NONE: "none",
- COMPRESS_ZLIB: "zlib",
- COMPRESS_LZO: "lzo",
- COMPRESS_ZSTD: "zstd",
- }
- name, ok := names[ct]
- if !ok {
- name = "unknown"
+ name := "unknown"
+ if int(ct) < len(compressionTypeNames) {
+ name = compressionTypeNames[ct]
}
return fmt.Sprintf("%d (%s)", ct, name)
}