Skip to content

Legion-ke/go-memory-visualizer.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Memory Visualizer for Neovim

Real-time Go struct memory layout visualization and optimization.

Installation

Using lazy.nvim

{
  'Legion-ke/go-memory-visualizer.nvim',
  ft = 'go',
  config = function()
    require('go-memory-visualizer').setup({
      -- Optional: customize settings
      default_architecture = 'amd64',
      show_inline_annotations = true,
      highlight_padding = true,
    })
  end
}
use {
  'Legion-ke/go-memory-visualizer.nvim',
  ft = 'go',
  config = function()
    require('go-memory-visualizer').setup()
  end
}

Using vim-plug

Plug 'Legion-ke/go-memory-visualizer.nvim'

Then add to your init.lua:

require('go-memory-visualizer').setup()

Quick Start

  1. Install the plugin using one of the methods above
  2. Open any .go file with struct definitions
  3. See inline annotations automatically appear
  4. Use :GoMemoryOptimize to optimize the struct at cursor

Commands

Command Description
:GoMemoryAnalyze Analyze current buffer
:GoMemoryOptimize Optimize struct at cursor
:GoMemoryToggleArch Cycle through amd64/arm64/386
:GoMemoryReport Show detailed report window
:GoMemoryToggle Toggle plugin on/off

Configuration

require('go-memory-visualizer').setup({
  -- Architecture for calculations (amd64, arm64, 386)
  default_architecture = 'amd64',

  -- Show memory info above each field
  show_inline_annotations = true,

  -- Highlight fields with padding waste
  highlight_padding = true,

  -- Minimum padding bytes to show warning
  padding_warning_threshold = 8,

  -- Warn about cache line crossings
  show_cache_line_warnings = true,

  -- Auto-update on buffer changes
  auto_update = true,
})

Keybindings (Example)

Add to your config:

vim.keymap.set('n', '<leader>ga', ':GoMemoryAnalyze<CR>', { desc = 'Analyze Go struct memory' })
vim.keymap.set('n', '<leader>go', ':GoMemoryOptimize<CR>', { desc = 'Optimize Go struct' })
vim.keymap.set('n', '<leader>gr', ':GoMemoryReport<CR>', { desc = 'Show memory report' })
vim.keymap.set('n', '<leader>gt', ':GoMemoryToggleArch<CR>', { desc = 'Toggle architecture' })

Example

Before optimization:

type User struct {
    Active    bool    // 1 byte + 7 padding
    ID        uint64  // 8 bytes
    Name      string  // 16 bytes
    Age       uint8   // 1 byte + 7 padding
    Balance   float64 // 8 bytes
}
// Total: 48 bytes | Padding: 14 bytes (29% waste)

After running :GoMemoryOptimize:

type User struct {
    ID        uint64  // 8 bytes
    Balance   float64 // 8 bytes
    Name      string  // 16 bytes
    Active    bool    // 1 byte
    Age       uint8   // 1 byte + 6 final padding
}
// Total: 40 bytes | Padding: 6 bytes (15% waste)
// Saved 8 bytes (16.7% reduction)

How It Works

The plugin follows Go's memory alignment rules:

  1. Type Alignment: Each type has a natural alignment requirement
  2. Field Placement: Fields are placed at offsets aligned to their requirements
  3. Padding Insertion: Go adds padding bytes to satisfy alignment
  4. Struct Size: Rounded up to the largest field alignment

The optimizer reorders fields by:

  1. Sorting by alignment (descending)
  2. Then by size (descending)
  3. Recalculating layout with new ordering

Requirements

  • Neovim >= 0.8.0
  • Go files (.go extension)

License

MIT License - see LICENSE file

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published