koki-develop / gat

🐱 cat alternative written in Go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible Performance Issues when it comes to large files

Pat3ickI opened this issue · comments

At Printer.go we should change io.ReadAll to io.Copy or io.CopyBuffer and to avoid allocation from src := string(b)

func (p *Printer) Print(ipt *PrintInput) error {
	// read source
	b, err := io.ReadAll(ipt.In)
	if err != nil {
		return err
	}
	src := string(b)

To

// For > Go 1.20 
func b2s(bytes []byte) string {
       return unsafe.String(unsafe.SliceData(bytes), len(bytes))
}

// For < Go 1.20
func b2s(bytes []byte) string {
       return *(*string)(unsafe.Pointer(&bytes))
}



func (p *Printer) Print(ipt *Printlnput) error {
        // read source 
        var buf buffer.Buffer
        _, err := io.Copy(buf, ipt.In)
        if err != nil {
               return err
        } 
        src := b2s(buf.Bytes())

@Patrickmitech
I see, by doing it this way, we can improve the performance of file reading!
Thank you very much!

If you don't mind, could you please open a Pull Request?
I would love to add your name to the list of Contributors for this repository.

@koki-develop, am happy to help but I don’t have sufficient resources to go beyond this, if possible please do that in my stead

@Patrickmitech
Okay, I’ll take care of it!