github.com/moreal/bencodex-go@v0.0.0-20231021172012-18277a477d15/README.md (about)

     1  # go-bencode
     2  [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
     3  [![Build Status](https://img.shields.io/travis/IncSW/go-bencodex.svg?style=flat-square)](https://travis-ci.org/IncSW/go-bencode)
     4  [![Coverage Status](https://img.shields.io/coveralls/IncSW/go-bencode/master.svg?style=flat-square)](https://coveralls.io/github/IncSW/go-bencode)
     5  [![Go Report Card](https://goreportcard.com/badge/github.com/moreal/bencodex-go?style=flat-square)](https://goreportcard.com/report/github.com/moreal/bencodex-go)
     6  [![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](http://godoc.org/github.com/moreal/bencodex-go)
     7  
     8  ## Installation
     9  
    10  `go get github.com/moreal/bencodex-go`
    11  
    12  ```go
    13  import bencode "github.com/moreal/bencodex-go"
    14  ```
    15  
    16  ## Quick Start
    17  
    18  ```go
    19  var dict interface{} = map[string]interface{}{
    20  	"int":    123,
    21  	"string": "Hello, World",
    22  	"list":   []interface{}{"foo", "bar"},
    23  }
    24  data, err := bencodex.Marshal(dict)
    25  if err != nil {
    26  	panic(err)
    27  }
    28  fmt.Println(string(data))
    29  
    30  // Output:
    31  // d3:inti123e4:listl3:foo3:bare6:string12:Hello, Worlde
    32  ```
    33  
    34  ```go
    35  data, err := bencodex.Unmarshal(value)
    36  ```
    37  
    38  ## Performance [benchmarks](https://github.com/moreal/bencodex-go/tree/master/benchmarks)
    39  
    40  ### Go 1.16, Debian 9.1, i7-7700
    41  
    42  ### Marshal
    43  
    44  | Library             |    Time     | Bytes Allocated | Objects Allocated | Notes |
    45  | :------------------ | :---------: | :-------------: | :---------------: | :---: |
    46  | IncSW/go-bencode    | 598.5 ns/op |    512 B/op     |    1 allocs/op    |       |
    47  | cristalhq/bencode   | 939.4 ns/op |    928 B/op     |    4 allocs/op    |       |
    48  | marksamman/bencode  | 1002 ns/op  |    736 B/op     |    9 allocs/op    |   4   |
    49  | aleksatr/go-bencode | 1060 ns/op  |    736 B/op     |    9 allocs/op    |       |
    50  | chihaya/chihaya     | 1722 ns/op  |    1009 B/op    |   53 allocs/op    |   1   |
    51  | lajide/bencode      | 1725 ns/op  |    1011 B/op    |   53 allocs/op    |   1   |
    52  | nabilanam/bencode   | 2865 ns/op  |    3192 B/op    |   54 allocs/op    |   5   |
    53  | anacrolix/torrent   | 3179 ns/op  |    1328 B/op    |   25 allocs/op    |       |
    54  | lwch/bencode        | 3340 ns/op  |    1792 B/op    |   75 allocs/op    | 1, 2  |
    55  | tumdum/bencoding    | 3419 ns/op  |    1752 B/op    |   60 allocs/op    |       |
    56  | stints/bencode      | 4018 ns/op  |    3120 B/op    |   100 allocs/op   | 1, 2  |
    57  | ehmry/go-bencode    | 4569 ns/op  |    1496 B/op    |   33 allocs/op    |       |
    58  | jackpal/bencode-go  | 4702 ns/op  |    2016 B/op    |   45 allocs/op    |       |
    59  | zeebo/bencode       | 5003 ns/op  |    1376 B/op    |   33 allocs/op    |       |
    60  | owenliang/dht       | 5180 ns/op  |    3279 B/op    |   80 allocs/op    |   5   |
    61  | cuberat/go-bencode  | 5589 ns/op  |    1929 B/op    |   71 allocs/op    |   2   |
    62  
    63  ### MarshalTo
    64  
    65  | Library            |    Time     | Bytes Allocated | Objects Allocated | Notes |
    66  | :----------------- | :---------: | :-------------: | :---------------: | :---: |
    67  | IncSW/go-bencode   | 503.0 ns/op |     0 B/op      |    0 allocs/op    |       |
    68  | cristalhq/bencode  | 668.4 ns/op |     0 B/op      |    0 allocs/op    |       |
    69  | chihaya/chihaya    | 1432 ns/op  |    307 B/op     |   49 allocs/op    |   1   |
    70  | lajide/bencode     | 1462 ns/op  |    307 B/op     |   49 allocs/op    |   1   |
    71  | anacrolix/torrent  | 2954 ns/op  |    720 B/op     |   21 allocs/op    |       |
    72  | lwch/bencode       | 3093 ns/op  |    1089 B/op    |   71 allocs/op    | 1, 2  |
    73  | tumdum/bencoding   | 3474 ns/op  |    1752 B/op    |   60 allocs/op    |       |
    74  | jackpal/bencode-go | 4479 ns/op  |    1408 B/op    |   41 allocs/op    |       |
    75  | ehmry/go-bencode   | 4650 ns/op  |    1528 B/op    |   33 allocs/op    |       |
    76  | cuberat/go-bencode | 5360 ns/op  |    1321 B/op    |   67 allocs/op    |   2   |
    77  
    78  ### Unmarshal
    79  
    80  | Library             |    Time     | Bytes Allocated | Objects Allocated | Notes |
    81  | :------------------ | :---------: | :-------------: | :---------------: | :---: |
    82  | IncSW/go-bencode    | 991.5 ns/op |    960 B/op     |   18 allocs/op    |       |
    83  | cristalhq/bencode   | 1160 ns/op  |    960 B/op     |   18 allocs/op    |       |
    84  | nabilanam/bencode   | 1379 ns/op  |    1240 B/op    |   39 allocs/op    |       |
    85  | owenliang/dht       | 1702 ns/op  |    1352 B/op    |   46 allocs/op    |       |
    86  | aleksatr/go-bencode | 2279 ns/op  |    1816 B/op    |   51 allocs/op    |       |
    87  | jackpal/bencode-go  | 2597 ns/op  |    1688 B/op    |   59 allocs/op    |       |
    88  | marksamman/bencode  | 2758 ns/op  |    5768 B/op    |   54 allocs/op    |       |
    89  | ehmry/go-bencode    | 2865 ns/op  |    2064 B/op    |   41 allocs/op    |       |
    90  | chihaya/chihaya     | 2961 ns/op  |    5880 B/op    |   61 allocs/op    |       |
    91  | lajide/bencode      | 2973 ns/op  |    5880 B/op    |   61 allocs/op    |       |
    92  | anacrolix/torrent   | 3723 ns/op  |    2456 B/op    |   62 allocs/op    |       |
    93  | cuberat/go-bencode  | 4687 ns/op  |    6544 B/op    |   119 allocs/op   |       |
    94  | zeebo/bencode       | 5954 ns/op  |    6376 B/op    |   91 allocs/op    |       |
    95  | tumdum/bencoding    | 7891 ns/op  |    6568 B/op    |   157 allocs/op   |       |
    96  | lwch/bencode        |      -      |        -        |         -         |   3   |
    97  | stints/bencode      |      -      |        -        |         -         |   6   |
    98  
    99  ### RealWorld [ubuntu-21.04-desktop-amd64.iso.torrent](https://releases.ubuntu.com/21.04/ubuntu-21.04-desktop-amd64.iso.torrent)
   100  
   101  | Library                       |    Time     | Bytes Allocated | Objects Allocated |
   102  | :---------------------------- | :---------: | :-------------: | :---------------: |
   103  | IncSW/go-bencode Unmarshal    | 1269 ns/op  |    1016 B/op    |   21 allocs/op    |
   104  | IncSW/go-bencode Marshal      | 27929 ns/op |   262816 B/op   |    2 allocs/op    |
   105  | IncSW/go-bencode MarshalTo    | 7851 ns/op  |     0 B/op      |    0 allocs/op    |
   106  |                               |
   107  |                               |
   108  | cristalhq/bencode Unmarshal   | 1552 ns/op  |    1016 B/op    |   21 allocs/op    |
   109  | cristalhq/bencode Marshal     | 49125 ns/op |   443168 B/op   |    5 allocs/op    |
   110  | cristalhq/bencode MarshalTo   | 8183 ns/op  |     0 B/op      |    0 allocs/op    |
   111  |                               |
   112  |                               |
   113  | aleksatr/go-bencode Unmarshal | 21615 ns/op |   223352 B/op   |   69 allocs/op    |
   114  | aleksatr/go-bencode Marshal   | 26401 ns/op |   222689 B/op   |   13 allocs/op    |
   115  |                               |
   116  |                               |
   117  | jackpal/bencode-go Unmarshal  | 48384 ns/op |   444484 B/op   |   81 allocs/op    |
   118  | jackpal/bencode-go Marshal    | 55097 ns/op |   445694 B/op   |   60 allocs/op    |
   119  | jackpal/bencode-go MarshalTo  | 35800 ns/op |   223128 B/op   |   54 allocs/op    |
   120  
   121  #### Notes
   122  
   123  1. BUG: Keys must be strings and appear in sorted order (sorted as raw strings, not alphanumerics). http://bittorrent.org/beps/bep_0003.html#bencoding
   124  2. BUG: []byte encoding as a list
   125  3. BUG: not supported list in list
   126  4. WARN: ignoring unsupported types without errors
   127  5. INFO: just 4 types supported
   128  6. INFO: files only
   129  
   130  ## License
   131  
   132  [MIT License](LICENSE).