|
| 1 | +# Development Guide |
| 2 | + |
| 3 | +## Setup |
| 4 | + |
| 5 | +1. Install dependencies: |
| 6 | +```bash |
| 7 | +npm install |
| 8 | +``` |
| 9 | + |
| 10 | +2. Compile TypeScript: |
| 11 | +```bash |
| 12 | +npm run compile |
| 13 | +``` |
| 14 | + |
| 15 | +3. Run tests: |
| 16 | +```bash |
| 17 | +node out/test/memoryCalculator.test.js |
| 18 | +node out/test/goParser.test.js |
| 19 | +node out/test/optimizer.test.js |
| 20 | +``` |
| 21 | + |
| 22 | +## Running the Extension |
| 23 | + |
| 24 | +### Option 1: Debug in VS Code |
| 25 | +1. Open this folder in VS Code |
| 26 | +2. Press F5 to launch Extension Development Host |
| 27 | +3. Open a Go file (try `examples/structs.go`) |
| 28 | +4. See inline memory annotations above struct fields |
| 29 | +5. Hover over fields for detailed info |
| 30 | +6. Click CodeLens "Optimize Layout" button above structs |
| 31 | + |
| 32 | +### Option 2: Package and Install |
| 33 | +```bash |
| 34 | +npm install -g @vscode/vsce |
| 35 | +vsce package |
| 36 | +code --install-extension go-memory-visualizer-0.1.0.vsix |
| 37 | +``` |
| 38 | + |
| 39 | +## Features to Test |
| 40 | + |
| 41 | +### 1. Inline Annotations |
| 42 | +- Open `examples/structs.go` |
| 43 | +- You should see annotations like `[0-7] 8B` above each field |
| 44 | +- Fields with padding show `+7B padding [!]` |
| 45 | + |
| 46 | +### 2. Hover Information |
| 47 | +- Hover over any struct field |
| 48 | +- See detailed breakdown: |
| 49 | + - Type, offset, size, alignment |
| 50 | + - Padding warnings |
| 51 | + |
| 52 | +### 3. CodeLens Optimization |
| 53 | +- Look for `Optimize Layout (save XB)` above poorly ordered structs |
| 54 | +- Click to automatically reorder fields |
| 55 | +- See confirmation message with bytes saved |
| 56 | + |
| 57 | +### 4. Commands |
| 58 | +- `Ctrl+Shift+P` → "Go: Show Memory Layout" |
| 59 | + - Opens side panel with full memory breakdown table |
| 60 | +- `Ctrl+Shift+P` → "Go: Toggle Architecture" |
| 61 | + - Switch between amd64, arm64, 386 |
| 62 | + - Recalculates layouts for selected architecture |
| 63 | +- `Ctrl+Shift+P` → "Go: Optimize Struct Memory Layout" |
| 64 | + - Optimizes struct under cursor |
| 65 | + |
| 66 | +### 5. Architecture Support |
| 67 | +- Test with different architectures |
| 68 | +- Note how pointer/int sizes change between 386 (32-bit) and amd64/arm64 (64-bit) |
| 69 | + |
| 70 | +## Configuration |
| 71 | + |
| 72 | +Settings in `.vscode/settings.json`: |
| 73 | +```json |
| 74 | +{ |
| 75 | + "goMemoryVisualizer.defaultArchitecture": "amd64", |
| 76 | + "goMemoryVisualizer.showInlineAnnotations": true, |
| 77 | + "goMemoryVisualizer.highlightPadding": true, |
| 78 | + "goMemoryVisualizer.paddingWarningThreshold": 8, |
| 79 | + "goMemoryVisualizer.showCacheLineWarnings": true |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Architecture |
| 84 | + |
| 85 | +``` |
| 86 | +src/ |
| 87 | +├── types.ts - TypeScript interfaces |
| 88 | +├── memoryCalculator.ts - Core memory layout logic |
| 89 | +├── goParser.ts - Go AST parsing |
| 90 | +├── optimizer.ts - Struct field reordering |
| 91 | +├── extension.ts - VS Code integration |
| 92 | +└── test/ |
| 93 | + ├── memoryCalculator.test.ts |
| 94 | + ├── goParser.test.ts |
| 95 | + └── optimizer.test.ts |
| 96 | +``` |
| 97 | + |
| 98 | +## Memory Layout Rules |
| 99 | + |
| 100 | +### Go Type Sizes (amd64) |
| 101 | +- `bool`, `int8`, `uint8`, `byte`: 1 byte |
| 102 | +- `int16`, `uint16`: 2 bytes |
| 103 | +- `int32`, `uint32`, `float32`, `rune`: 4 bytes |
| 104 | +- `int64`, `uint64`, `float64`: 8 bytes |
| 105 | +- `int`, `uint`, `uintptr`, `*T`: 8 bytes (pointer size) |
| 106 | +- `string`: 16 bytes (pointer + length) |
| 107 | +- `[]T`: 8 bytes (slice header pointer) |
| 108 | +- `interface{}`: 16 bytes (type + data pointers) |
| 109 | + |
| 110 | +### Alignment Rules |
| 111 | +- Fields are aligned to their natural alignment |
| 112 | +- Structs are aligned to their largest field alignment |
| 113 | +- Padding is added to maintain alignment |
| 114 | + |
| 115 | +### Optimization Strategy |
| 116 | +1. Sort fields by alignment (descending) |
| 117 | +2. Within same alignment, sort by size (descending) |
| 118 | +3. This minimizes padding between fields |
| 119 | + |
| 120 | +## Troubleshooting |
| 121 | + |
| 122 | +### No decorations showing |
| 123 | +- Check `goMemoryVisualizer.showInlineAnnotations` is `true` |
| 124 | +- Verify file language is `Go` |
| 125 | +- Try reloading VS Code window |
| 126 | + |
| 127 | +### Wrong sizes displayed |
| 128 | +- Check selected architecture matches your target |
| 129 | +- Use `Go: Toggle Architecture` command |
| 130 | + |
| 131 | +### Tests failing |
| 132 | +- Run `npm run compile` first |
| 133 | +- Check Node.js version (requires 20.x) |
| 134 | + |
| 135 | +## Publishing |
| 136 | + |
| 137 | +1. Update version in `package.json` |
| 138 | +2. Update `README.md` and `CHANGELOG.md` |
| 139 | +3. Run tests: `npm test` |
| 140 | +4. Package: `vsce package` |
| 141 | +5. Publish: `vsce publish` |
| 142 | + |
| 143 | +## Future Enhancements |
| 144 | + |
| 145 | +- [ ] Cache line boundary visualization |
| 146 | +- [ ] Support for embedded structs |
| 147 | +- [ ] Comparison view (before/after optimization) |
| 148 | +- [ ] Export memory layout reports |
| 149 | +- [ ] Integration with Go's `unsafe.Sizeof` |
| 150 | +- [ ] Benchmark impact visualization |
0 commit comments