[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 package main
import ( import (
"bytes"
"strconv" "strconv"
"unsafe" "unsafe"
) )
@ -11,12 +10,12 @@ const CLR = "\x1b[0m"
var ESC = []byte{0x5c, 0x78, 0x31, 0x62, 0x5b} var ESC = []byte{0x5c, 0x78, 0x31, 0x62, 0x5b}
var CLEAR = []byte{0x5c, 0x78, 0x31, 0x62, 0x5b, 0x30, 0x6d} var CLEAR = []byte{0x5c, 0x78, 0x31, 0x62, 0x5b, 0x30, 0x6d}
var CLRR = []byte("\x1b[0m")
type Color struct { type Color struct {
disable bool disable bool
values [256]string values [256]string
bvalues [256][]byte cvalues [256][]byte
cvalues map[byte][]byte
} }
func (c *Color) Compute() { func (c *Color) Compute() {
@ -34,6 +33,7 @@ func (c *Color) Compute() {
bg = "" bg = ""
} }
c.values[i] = bg + fg 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 return c.values[clr] + s + NOCOLOR
} }
func (c *Color) ColorizeBytes(s string, byteColor []byte) []byte { // function to colorize bytes - avoiding string conversions
const NOCOLOR = "\x1b[0m" func (c *Color) Colorize2(clr byte) ([]byte, []byte) {
b := ByteArrayToInt(byteColor) return c.cvalues[clr], CLRR
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()
}
} }
func IntToByteArray(num int) []byte { func IntToByteArray(num int) []byte {

View file

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