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_dir.go | 20 ++++++++++---------- lib/btrfs/btrfsitem/item_extent.go | 2 +- lib/btrfs/btrfsitem/item_fileextent.go | 4 ++-- lib/btrfs/btrfsitem/item_freespaceinfo.go | 2 +- lib/btrfs/btrfsitem/item_inode.go | 2 +- lib/btrfs/btrfsitem/item_root.go | 2 +- 6 files changed, 16 insertions(+), 16 deletions(-) (limited to 'lib/btrfs/btrfsitem') diff --git a/lib/btrfs/btrfsitem/item_dir.go b/lib/btrfs/btrfsitem/item_dir.go index 0049072..0f263d5 100644 --- a/lib/btrfs/btrfsitem/item_dir.go +++ b/lib/btrfs/btrfsitem/item_dir.go @@ -84,17 +84,17 @@ func (o DirEntry) MarshalBinary() ([]byte, error) { type FileType uint8 const ( - FT_UNKNOWN = FileType(0) - FT_REG_FILE = FileType(1) - FT_DIR = FileType(2) - FT_CHRDEV = FileType(3) - FT_BLKDEV = FileType(4) - FT_FIFO = FileType(5) - FT_SOCK = FileType(6) - FT_SYMLINK = FileType(7) - FT_XATTR = FileType(8) + FT_UNKNOWN FileType = 0 + FT_REG_FILE FileType = 1 + FT_DIR FileType = 2 + FT_CHRDEV FileType = 3 + FT_BLKDEV FileType = 4 + FT_FIFO FileType = 5 + FT_SOCK FileType = 6 + FT_SYMLINK FileType = 7 + FT_XATTR FileType = 8 - FT_MAX = FileType(9) + FT_MAX FileType = 9 ) func (ft FileType) String() string { diff --git a/lib/btrfs/btrfsitem/item_extent.go b/lib/btrfs/btrfsitem/item_extent.go index 3789cfe..871ed90 100644 --- a/lib/btrfs/btrfsitem/item_extent.go +++ b/lib/btrfs/btrfsitem/item_extent.go @@ -111,7 +111,7 @@ type TreeBlockInfo struct { type ExtentFlags uint64 const ( - EXTENT_FLAG_DATA = ExtentFlags(1 << iota) + EXTENT_FLAG_DATA ExtentFlags = 1 << iota EXTENT_FLAG_TREE_BLOCK ) 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 diff --git a/lib/btrfs/btrfsitem/item_freespaceinfo.go b/lib/btrfs/btrfsitem/item_freespaceinfo.go index 0699367..9b886af 100644 --- a/lib/btrfs/btrfsitem/item_freespaceinfo.go +++ b/lib/btrfs/btrfsitem/item_freespaceinfo.go @@ -20,7 +20,7 @@ type FreeSpaceInfo struct { // trivial FREE_SPACE_INFO=198 type FreeSpaceFlags uint32 const ( - FREE_SPACE_USING_BITMAPS = FreeSpaceFlags(1 << iota) + FREE_SPACE_USING_BITMAPS FreeSpaceFlags = 1 << iota ) var freeSpaceFlagNames = []string{ diff --git a/lib/btrfs/btrfsitem/item_inode.go b/lib/btrfs/btrfsitem/item_inode.go index 69f8445..6951c76 100644 --- a/lib/btrfs/btrfsitem/item_inode.go +++ b/lib/btrfs/btrfsitem/item_inode.go @@ -36,7 +36,7 @@ type Inode struct { // trivial INODE_ITEM=1 type InodeFlags uint64 const ( - INODE_NODATASUM = InodeFlags(1 << iota) + INODE_NODATASUM InodeFlags = 1 << iota INODE_NODATACOW INODE_READONLY INODE_NOCOMPRESS diff --git a/lib/btrfs/btrfsitem/item_root.go b/lib/btrfs/btrfsitem/item_root.go index c0db900..d39bd70 100644 --- a/lib/btrfs/btrfsitem/item_root.go +++ b/lib/btrfs/btrfsitem/item_root.go @@ -48,7 +48,7 @@ type Root struct { // trivial ROOT_ITEM=132 type RootFlags uint64 const ( - ROOT_SUBVOL_RDONLY = RootFlags(1 << iota) + ROOT_SUBVOL_RDONLY RootFlags = 1 << iota ) var rootFlagNames = []string{ -- 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_dir.go | 47 +++++++++++++++++----------------- lib/btrfs/btrfsitem/item_fileextent.go | 36 ++++++++++++++------------ 2 files changed, 43 insertions(+), 40 deletions(-) (limited to 'lib/btrfs/btrfsitem') diff --git a/lib/btrfs/btrfsitem/item_dir.go b/lib/btrfs/btrfsitem/item_dir.go index 0f263d5..c1b3c09 100644 --- a/lib/btrfs/btrfsitem/item_dir.go +++ b/lib/btrfs/btrfsitem/item_dir.go @@ -84,33 +84,34 @@ func (o DirEntry) MarshalBinary() ([]byte, error) { type FileType uint8 const ( - FT_UNKNOWN FileType = 0 - FT_REG_FILE FileType = 1 - FT_DIR FileType = 2 - FT_CHRDEV FileType = 3 - FT_BLKDEV FileType = 4 - FT_FIFO FileType = 5 - FT_SOCK FileType = 6 - FT_SYMLINK FileType = 7 - FT_XATTR FileType = 8 + FT_UNKNOWN FileType = iota + FT_REG_FILE + FT_DIR + FT_CHRDEV + FT_BLKDEV + FT_FIFO + FT_SOCK + FT_SYMLINK + FT_XATTR - FT_MAX FileType = 9 + FT_MAX ) +var fileTypeNames = []string{ + "UNKNOWN", + "FILE", // NB: Just "FILE", despite corresponding to "REG_FILE" + "DIR", + "CHRDEV", + "BLKDEV", + "FIFO", + "SOCK", + "SYMLINK", + "XATTR", +} + func (ft FileType) String() string { - names := map[FileType]string{ - FT_UNKNOWN: "UNKNOWN", - FT_REG_FILE: "FILE", // XXX - FT_DIR: "DIR", - FT_CHRDEV: "CHRDEV", - FT_BLKDEV: "BLKDEV", - FT_FIFO: "FIFO", - FT_SOCK: "SOCK", - FT_SYMLINK: "SYMLINK", - FT_XATTR: "XATTR", - } - if name, ok := names[ft]; ok { - return name + if ft < FT_MAX { + return fileTypeNames[ft] } return fmt.Sprintf("DIR_ITEM.%d", uint8(ft)) } 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