[hexxy] DEV branch - extremely speed up hex encoding

This commit is contained in:
mizi 2024-02-01 16:21:56 -09:00
parent a769eb2024
commit 25db94290c
2 changed files with 31 additions and 66 deletions

View file

@ -1,7 +1,6 @@
package main
import (
"bytes"
"strconv"
"unsafe"
)
@ -11,12 +10,12 @@ const CLR = "\x1b[0m"
var ESC = []byte{0x5c, 0x78, 0x31, 0x62, 0x5b}
var CLEAR = []byte{0x5c, 0x78, 0x31, 0x62, 0x5b, 0x30, 0x6d}
var CLRR = []byte("\x1b[0m")
type Color struct {
disable bool
values [256]string
bvalues [256][]byte
cvalues map[byte][]byte
cvalues [256][]byte
}
func (c *Color) Compute() {
@ -34,6 +33,7 @@ func (c *Color) Compute() {
bg = ""
}
c.values[i] = bg + fg
c.cvalues[i] = []byte(bg + fg)
}
}
@ -42,64 +42,9 @@ func (c *Color) Colorize(s string, clr byte) string {
return c.values[clr] + s + NOCOLOR
}
func (c *Color) ColorizeBytes(s string, byteColor []byte) []byte {
const NOCOLOR = "\x1b[0m"
b := ByteArrayToInt(byteColor)
return []byte(c.values[b] + s + NOCOLOR)
}
func (c *Color) ComputeBytes() {
const WHITEB = "\x1b[1;37m"
for i := 0; i < 256; i++ {
var fg, bg string
b := byte(i)
lowVis := i == 0 || (i >= 16 && i <= 20) || (i >= 232 && i <= 242)
if lowVis {
fg = WHITEB + "\x1b[38;5;" + "255" + "m"
bg = "\x1b[48;5;" + strconv.Itoa(int(i)) + "m"
} else {
fg = "\x1b[38;5;" + strconv.Itoa(int(i)) + "m"
bg = ""
}
c.values[i] = bg + fg
c.cvalues[b] = []byte(bg + fg)
}
}
func (c *Color) xComputeBytes() {
const Marker = '\x1b'
var b bytes.Buffer
for i := 0; i < 256; i++ {
// var fg, bg []byte
b.Write(ESC)
// x := string(i)
// y := []byte(x)
lowVis := i == 0 || (i >= 16 && i <= 20) || (i >= 232 && i <= 242)
if lowVis {
b.Write([]byte{'[', '1', ';', '3', '7', 'm'})
b.Write(ESC)
b.Write([]byte{'[', '4', '8', ';', '5'})
bg := make([]byte, 3)
bg = IntToByteArray(i)
b.Write(bg)
b.WriteByte('m')
} else {
b.Write([]byte{'[', '3', '8', ';', '5'})
fg := make([]byte, 3)
fg = IntToByteArray(i)
b.Write(fg)
b.WriteByte('m')
}
// c.values[i] = bg + fg
// c.bvalues[i] = bytes.Join([]byte(bg), []byte(fg))
c.bvalues[i] = b.Bytes()
}
// function to colorize bytes - avoiding string conversions
func (c *Color) Colorize2(clr byte) ([]byte, []byte) {
return c.cvalues[clr], CLRR
}
func IntToByteArray(num int) []byte {

View file

@ -7,6 +7,7 @@ import (
"io"
"log"
"os"
"runtime/pprof"
"strconv"
"github.com/jessevdk/go-flags"
@ -42,8 +43,8 @@ const (
)
const (
ldigits = "0123456789abcdef"
udigits = "0123456789ABCDEF"
ldigits = "0123456789abcdef"
)
var (
@ -64,6 +65,10 @@ var (
bar = []byte("|")
)
var (
STDOUT_ISPIPE bool
)
func inputIsPipe() bool {
stat, _ := os.Stdin.Stat()
return stat.Mode()&os.ModeCharDevice != os.ModeCharDevice
@ -263,9 +268,16 @@ func XXD(r io.Reader, w io.Writer, filename string, color *Color) error {
for i, k := 0, octs; i < n; i, k = i+1, k+octs {
hexEncode(char, line[i:i+1], caps)
// s := color.Colorize(string(char), byte(i))
// w.Write([]byte(s))
if !opts.NoColor && !STDOUT_ISPIPE {
i := line[i : i+1][0]
b, c := color.Colorize2(i)
w.Write(b)
w.Write(char)
w.Write(c)
} else {
w.Write(char)
}
c++
if k == octs*groupSize {
@ -438,7 +450,9 @@ func main() {
log.Fatal(err)
}
if !inputIsPipe() && len(args) == 0 {
STDOUT_ISPIPE = inputIsPipe()
if !STDOUT_ISPIPE && len(args) == 0 {
parser.WriteHelp(os.Stderr)
fmt.Print(usage_msg)
os.Exit(0)
@ -448,6 +462,12 @@ func main() {
Debug = log.Printf
}
f, err := os.Create("mem.cpu")
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
if err := Hexxy(args); err != nil {
log.Fatal(err)
}