-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid heap allocations in Htonl (#55)
* Avoid heap allocations in Htonl * move new allocation benchmarks in a BenchmarkXXX
- Loading branch information
Showing
2 changed files
with
29 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,17 @@ | ||
package rpmdb | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"log" | ||
) | ||
|
||
func Htonl(val int32) int32 { | ||
buf := new(bytes.Buffer) | ||
if err := binary.Write(buf, binary.LittleEndian, val); err != nil { | ||
log.Println(err) | ||
return 0 | ||
} | ||
|
||
if err := binary.Read(buf, binary.BigEndian, &val); err != nil { | ||
log.Println(err) | ||
return 0 | ||
} | ||
return val | ||
var buf [4]byte | ||
binary.LittleEndian.PutUint32(buf[:], uint32(val)) | ||
return int32(binary.BigEndian.Uint32(buf[:])) | ||
} | ||
|
||
func HtonlU(val uint32) uint32 { | ||
buf := new(bytes.Buffer) | ||
if err := binary.Write(buf, binary.LittleEndian, val); err != nil { | ||
log.Println(err) | ||
return 0 | ||
} | ||
|
||
if err := binary.Read(buf, binary.BigEndian, &val); err != nil { | ||
log.Println(err) | ||
return 0 | ||
} | ||
return val | ||
var buf [4]byte | ||
binary.LittleEndian.PutUint32(buf[:], val) | ||
return binary.BigEndian.Uint32(buf[:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters