|
| 1 | +# Fast NAR Reader (fastnar) |
| 2 | + |
| 3 | +This package provides a high-performance NAR (Nix Archive) reader implementation, thanks to the genius brain of @edef. It improves upon the original `pkg/nar` package with a synchronous, state-machine based approach. |
| 4 | + |
| 5 | +### Performance Improvements |
| 6 | + |
| 7 | +1. **Synchronous Processing**: No goroutines or channels overhead |
| 8 | +2. **Buffered Reading**: Uses `bufio.Reader` with peek/discard operations |
| 9 | +3. **Pre-computed Tokens**: Binary token matching for faster parsing |
| 10 | +4. **Lower Memory Allocation**: Minimal object creation during parsing |
| 11 | +5. **State Machine**: Direct state transitions without intermediate objects |
| 12 | + |
| 13 | +## Usage Examples |
| 14 | + |
| 15 | +### Basic Usage |
| 16 | + |
| 17 | +```go |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "os" |
| 24 | + |
| 25 | + "github.com/nix-community/go-nix/pkg/fastnar" |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + file, err := os.Open("archive.nar") |
| 30 | + if err != nil { |
| 31 | + panic(err) |
| 32 | + } |
| 33 | + defer file.Close() |
| 34 | + |
| 35 | + reader := nar.NewReader(file) |
| 36 | + |
| 37 | + for { |
| 38 | + tag, err := reader.Next() |
| 39 | + if err == io.EOF { |
| 40 | + break |
| 41 | + } |
| 42 | + if err != nil { |
| 43 | + panic(err) |
| 44 | + } |
| 45 | + |
| 46 | + switch tag { |
| 47 | + case nar.TagDir: |
| 48 | + fmt.Printf("Directory: %s\n", reader.Path()) |
| 49 | + case nar.TagReg: |
| 50 | + fmt.Printf("File: %s (%d bytes)\n", reader.Path(), reader.Size()) |
| 51 | + case nar.TagExe: |
| 52 | + fmt.Printf("Executable: %s (%d bytes)\n", reader.Path(), reader.Size()) |
| 53 | + case nar.TagSym: |
| 54 | + fmt.Printf("Symlink: %s -> %s\n", reader.Path(), reader.Target()) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Copying NAR Archives |
| 61 | + |
| 62 | +```go |
| 63 | +// High-performance NAR copying |
| 64 | +func copyNAR(dst io.Writer, src io.Reader) error { |
| 65 | + reader := nar.NewReader(src) |
| 66 | + writer := nar.NewWriter(dst) |
| 67 | + return nar.Copy(writer, reader) |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### Reading File Contents |
| 72 | + |
| 73 | +```go |
| 74 | +for { |
| 75 | + tag, err := reader.Next() |
| 76 | + if err == io.EOF { |
| 77 | + break |
| 78 | + } |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + if tag == nar.TagReg || tag == nar.TagExe { |
| 84 | + // Read file content |
| 85 | + content := make([]byte, reader.Size()) |
| 86 | + _, err := io.ReadFull(reader, content) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + fmt.Printf("File %s: %s\n", reader.Path(), string(content)) |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Migration Guide |
| 97 | + |
| 98 | +### From pkg/nar to pkg/fastnar |
| 99 | + |
| 100 | +**Before:** |
| 101 | +```go |
| 102 | +import "github.com/nix-community/go-nix/pkg/nar" |
| 103 | + |
| 104 | +reader, err := nar.NewReader(file) |
| 105 | +if err != nil { |
| 106 | + return err |
| 107 | +} |
| 108 | +defer reader.Close() |
| 109 | + |
| 110 | +for { |
| 111 | + header, err := reader.Next() |
| 112 | + if err == io.EOF { |
| 113 | + break |
| 114 | + } |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + switch header.Type { |
| 120 | + case nar.TypeDirectory: |
| 121 | + fmt.Printf("Dir: %s\n", header.Path) |
| 122 | + case nar.TypeRegular: |
| 123 | + fmt.Printf("File: %s\n", header.Path) |
| 124 | + if header.Executable { |
| 125 | + fmt.Printf(" (executable)\n") |
| 126 | + } |
| 127 | + case nar.TypeSymlink: |
| 128 | + fmt.Printf("Link: %s -> %s\n", header.Path, header.LinkTarget) |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +**After:** |
| 134 | +```go |
| 135 | +import "github.com/nix-community/go-nix/pkg/fastnar" |
| 136 | + |
| 137 | +reader := nar.NewReader(file) |
| 138 | + |
| 139 | +for { |
| 140 | + tag, err := reader.Next() |
| 141 | + if err == io.EOF { |
| 142 | + break |
| 143 | + } |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + |
| 148 | + switch tag { |
| 149 | + case nar.TagDir: |
| 150 | + fmt.Printf("Dir: %s\n", reader.Path()) |
| 151 | + case nar.TagReg: |
| 152 | + fmt.Printf("File: %s\n", reader.Path()) |
| 153 | + case nar.TagExe: |
| 154 | + fmt.Printf("File: %s\n", reader.Path()) |
| 155 | + fmt.Printf(" (executable)\n") |
| 156 | + case nar.TagSym: |
| 157 | + fmt.Printf("Link: %s -> %s\n", reader.Path(), reader.Target()) |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
0 commit comments