diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-11 22:33:44 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-12 16:17:27 -0700 |
commit | acbbfafa07922b458506b91a58f3a082da453fd1 (patch) | |
tree | 020cb033adb3809bcc8ffed5f56ba8d167b32dd1 /lib/btrfs/btrfsprim | |
parent | 1f3a1a474ac41f427e4c5b0d27d2c85a3cfaa65e (diff) |
Try to avoid unnecessary allocations in enum-types' String methods
Diffstat (limited to 'lib/btrfs/btrfsprim')
-rw-r--r-- | lib/btrfs/btrfsprim/itemtype.go | 104 | ||||
-rw-r--r-- | lib/btrfs/btrfsprim/objid.go | 85 |
2 files changed, 109 insertions, 80 deletions
diff --git a/lib/btrfs/btrfsprim/itemtype.go b/lib/btrfs/btrfsprim/itemtype.go index dba7ced..f33179a 100644 --- a/lib/btrfs/btrfsprim/itemtype.go +++ b/lib/btrfs/btrfsprim/itemtype.go @@ -42,42 +42,72 @@ const ( ) func (t ItemType) String() string { - names := map[ItemType]string{ - BLOCK_GROUP_ITEM_KEY: "BLOCK_GROUP_ITEM", - CHUNK_ITEM_KEY: "CHUNK_ITEM", - DEV_EXTENT_KEY: "DEV_EXTENT", - DEV_ITEM_KEY: "DEV_ITEM", - DIR_INDEX_KEY: "DIR_INDEX", - DIR_ITEM_KEY: "DIR_ITEM", - EXTENT_CSUM_KEY: "EXTENT_CSUM", - EXTENT_DATA_KEY: "EXTENT_DATA", - EXTENT_DATA_REF_KEY: "EXTENT_DATA_REF", - EXTENT_ITEM_KEY: "EXTENT_ITEM", - FREE_SPACE_BITMAP_KEY: "FREE_SPACE_BITMAP", - FREE_SPACE_EXTENT_KEY: "FREE_SPACE_EXTENT", - FREE_SPACE_INFO_KEY: "FREE_SPACE_INFO", - INODE_ITEM_KEY: "INODE_ITEM", - INODE_REF_KEY: "INODE_REF", - METADATA_ITEM_KEY: "METADATA_ITEM", - ORPHAN_ITEM_KEY: "ORPHAN_ITEM", - PERSISTENT_ITEM_KEY: "PERSISTENT_ITEM", - QGROUP_INFO_KEY: "QGROUP_INFO", - QGROUP_LIMIT_KEY: "QGROUP_LIMIT", - QGROUP_RELATION_KEY: "QGROUP_RELATION", - QGROUP_STATUS_KEY: "QGROUP_STATUS", - ROOT_BACKREF_KEY: "ROOT_BACKREF", - ROOT_ITEM_KEY: "ROOT_ITEM", - ROOT_REF_KEY: "ROOT_REF", - SHARED_BLOCK_REF_KEY: "SHARED_BLOCK_REF", - SHARED_DATA_REF_KEY: "SHARED_DATA_REF", - TREE_BLOCK_REF_KEY: "TREE_BLOCK_REF", - UNTYPED_KEY: "UNTYPED", - UUID_RECEIVED_SUBVOL_KEY: "UUID_KEY_RECEIVED_SUBVOL", - UUID_SUBVOL_KEY: "UUID_KEY_SUBVOL", - XATTR_ITEM_KEY: "XATTR_ITEM", + switch t { + case BLOCK_GROUP_ITEM_KEY: + return "BLOCK_GROUP_ITEM" + case CHUNK_ITEM_KEY: + return "CHUNK_ITEM" + case DEV_EXTENT_KEY: + return "DEV_EXTENT" + case DEV_ITEM_KEY: + return "DEV_ITEM" + case DIR_INDEX_KEY: + return "DIR_INDEX" + case DIR_ITEM_KEY: + return "DIR_ITEM" + case EXTENT_CSUM_KEY: + return "EXTENT_CSUM" + case EXTENT_DATA_KEY: + return "EXTENT_DATA" + case EXTENT_DATA_REF_KEY: + return "EXTENT_DATA_REF" + case EXTENT_ITEM_KEY: + return "EXTENT_ITEM" + case FREE_SPACE_BITMAP_KEY: + return "FREE_SPACE_BITMAP" + case FREE_SPACE_EXTENT_KEY: + return "FREE_SPACE_EXTENT" + case FREE_SPACE_INFO_KEY: + return "FREE_SPACE_INFO" + case INODE_ITEM_KEY: + return "INODE_ITEM" + case INODE_REF_KEY: + return "INODE_REF" + case METADATA_ITEM_KEY: + return "METADATA_ITEM" + case ORPHAN_ITEM_KEY: + return "ORPHAN_ITEM" + case PERSISTENT_ITEM_KEY: + return "PERSISTENT_ITEM" + case QGROUP_INFO_KEY: + return "QGROUP_INFO" + case QGROUP_LIMIT_KEY: + return "QGROUP_LIMIT" + case QGROUP_RELATION_KEY: + return "QGROUP_RELATION" + case QGROUP_STATUS_KEY: + return "QGROUP_STATUS" + case ROOT_BACKREF_KEY: + return "ROOT_BACKREF" + case ROOT_ITEM_KEY: + return "ROOT_ITEM" + case ROOT_REF_KEY: + return "ROOT_REF" + case SHARED_BLOCK_REF_KEY: + return "SHARED_BLOCK_REF" + case SHARED_DATA_REF_KEY: + return "SHARED_DATA_REF" + case TREE_BLOCK_REF_KEY: + return "TREE_BLOCK_REF" + case UNTYPED_KEY: + return "UNTYPED" + case UUID_RECEIVED_SUBVOL_KEY: + return "UUID_KEY_RECEIVED_SUBVOL" + case UUID_SUBVOL_KEY: + return "UUID_KEY_SUBVOL" + case XATTR_ITEM_KEY: + return "XATTR_ITEM" + default: + return fmt.Sprintf("%d", t) } - if name, ok := names[t]; ok { - return name - } - return fmt.Sprintf("%d", t) } diff --git a/lib/btrfs/btrfsprim/objid.go b/lib/btrfs/btrfsprim/objid.go index 8ca1fbb..1aea030 100644 --- a/lib/btrfs/btrfsprim/objid.go +++ b/lib/btrfs/btrfsprim/objid.go @@ -54,13 +54,45 @@ const ( EMPTY_SUBVOL_DIR_OBJECTID ObjID = 2 ) +var ( + objidCommonNames = map[ObjID]string{ + BALANCE_OBJECTID: "BALANCE", + ORPHAN_OBJECTID: "ORPHAN", + TREE_LOG_OBJECTID: "TREE_LOG", + TREE_LOG_FIXUP_OBJECTID: "TREE_LOG_FIXUP", + TREE_RELOC_OBJECTID: "TREE_RELOC", + DATA_RELOC_TREE_OBJECTID: "DATA_RELOC_TREE", + EXTENT_CSUM_OBJECTID: "EXTENT_CSUM", + FREE_SPACE_OBJECTID: "FREE_SPACE", + FREE_INO_OBJECTID: "FREE_INO", + MULTIPLE_OBJECTIDS: "MULTIPLE", + } + objidDevTreeNames = map[ObjID]string{ + DEV_STATS_OBJECTID: "DEV_STATS", + } + objidChunkTreeNames = map[ObjID]string{ + DEV_ITEMS_OBJECTID: "DEV_ITEMS", + FIRST_CHUNK_TREE_OBJECTID: "FIRST_CHUNK_TREE", + } + objidRootTreeNames = map[ObjID]string{ + ROOT_TREE_OBJECTID: "ROOT_TREE", + EXTENT_TREE_OBJECTID: "EXTENT_TREE", + CHUNK_TREE_OBJECTID: "CHUNK_TREE", + DEV_TREE_OBJECTID: "DEV_TREE", + FS_TREE_OBJECTID: "FS_TREE", + ROOT_TREE_DIR_OBJECTID: "ROOT_TREE_DIR", + CSUM_TREE_OBJECTID: "CSUM_TREE", + QUOTA_TREE_OBJECTID: "QUOTA_TREE", + UUID_TREE_OBJECTID: "UUID_TREE", + FREE_SPACE_TREE_OBJECTID: "FREE_SPACE_TREE", + BLOCK_GROUP_TREE_OBJECTID: "BLOCK_GROUP_TREE", + } +) + func (id ObjID) Format(tree ObjID) string { switch tree { case DEV_TREE_OBJECTID: - names := map[ObjID]string{ - DEV_STATS_OBJECTID: "DEV_STATS", - } - if name, ok := names[id]; ok { + if name, ok := objidDevTreeNames[id]; ok { return name } return fmt.Sprintf("%d", int64(id)) @@ -75,51 +107,18 @@ func (id ObjID) Format(tree ObjID) string { case UUID_TREE_OBJECTID: return fmt.Sprintf("%#016x", uint64(id)) case CHUNK_TREE_OBJECTID: - names := map[ObjID]string{ - BALANCE_OBJECTID: "BALANCE", - ORPHAN_OBJECTID: "ORPHAN", - TREE_LOG_OBJECTID: "TREE_LOG", - TREE_LOG_FIXUP_OBJECTID: "TREE_LOG_FIXUP", - TREE_RELOC_OBJECTID: "TREE_RELOC", - DATA_RELOC_TREE_OBJECTID: "DATA_RELOC_TREE", - EXTENT_CSUM_OBJECTID: "EXTENT_CSUM", - FREE_SPACE_OBJECTID: "FREE_SPACE", - FREE_INO_OBJECTID: "FREE_INO", - MULTIPLE_OBJECTIDS: "MULTIPLE", - - DEV_ITEMS_OBJECTID: "DEV_ITEMS", - FIRST_CHUNK_TREE_OBJECTID: "FIRST_CHUNK_TREE", + if name, ok := objidCommonNames[id]; ok { + return name } - if name, ok := names[id]; ok { + if name, ok := objidChunkTreeNames[id]; ok { return name } return fmt.Sprintf("%d", int64(id)) default: - names := map[ObjID]string{ - BALANCE_OBJECTID: "BALANCE", - ORPHAN_OBJECTID: "ORPHAN", - TREE_LOG_OBJECTID: "TREE_LOG", - TREE_LOG_FIXUP_OBJECTID: "TREE_LOG_FIXUP", - TREE_RELOC_OBJECTID: "TREE_RELOC", - DATA_RELOC_TREE_OBJECTID: "DATA_RELOC_TREE", - EXTENT_CSUM_OBJECTID: "EXTENT_CSUM", - FREE_SPACE_OBJECTID: "FREE_SPACE", - FREE_INO_OBJECTID: "FREE_INO", - MULTIPLE_OBJECTIDS: "MULTIPLE", - - ROOT_TREE_OBJECTID: "ROOT_TREE", - EXTENT_TREE_OBJECTID: "EXTENT_TREE", - CHUNK_TREE_OBJECTID: "CHUNK_TREE", - DEV_TREE_OBJECTID: "DEV_TREE", - FS_TREE_OBJECTID: "FS_TREE", - ROOT_TREE_DIR_OBJECTID: "ROOT_TREE_DIR", - CSUM_TREE_OBJECTID: "CSUM_TREE", - QUOTA_TREE_OBJECTID: "QUOTA_TREE", - UUID_TREE_OBJECTID: "UUID_TREE", - FREE_SPACE_TREE_OBJECTID: "FREE_SPACE_TREE", - BLOCK_GROUP_TREE_OBJECTID: "BLOCK_GROUP_TREE", + if name, ok := objidCommonNames[id]; ok { + return name } - if name, ok := names[id]; ok { + if name, ok := objidRootTreeNames[id]; ok { return name } return fmt.Sprintf("%d", int64(id)) |