From 28549f676e040c96bfae575535dea7e0ecdf0f41 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 11 Feb 2023 22:15:41 -0700 Subject: Change how constants are declared to play nice with godoc Switch to NAME Type = val instead of NAME = Type(val) --- lib/btrfs/btrfsitem/item_fileextent.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/btrfs/btrfsitem/item_fileextent.go') diff --git a/lib/btrfs/btrfsitem/item_fileextent.go b/lib/btrfs/btrfsitem/item_fileextent.go index 30a14ef..ce6c45c 100644 --- a/lib/btrfs/btrfsitem/item_fileextent.go +++ b/lib/btrfs/btrfsitem/item_fileextent.go @@ -101,7 +101,7 @@ func (o FileExtent) MarshalBinary() ([]byte, error) { type FileExtentType uint8 const ( - FILE_EXTENT_INLINE = FileExtentType(iota) + FILE_EXTENT_INLINE FileExtentType = iota FILE_EXTENT_REG FILE_EXTENT_PREALLOC ) @@ -133,7 +133,7 @@ func (fet FileExtentType) String() string { type CompressionType uint8 const ( - COMPRESS_NONE = CompressionType(iota) + COMPRESS_NONE CompressionType = iota COMPRESS_ZLIB COMPRESS_LZO COMPRESS_ZSTD -- cgit v1.2.3-2-g168b From acbbfafa07922b458506b91a58f3a082da453fd1 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 11 Feb 2023 22:33:44 -0700 Subject: Try to avoid unnecessary allocations in enum-types' String methods --- lib/btrfs/btrfsitem/item_fileextent.go | 36 ++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'lib/btrfs/btrfsitem/item_fileextent.go') 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) } -- cgit v1.2.3-2-g168b