Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
update: reduce dynamic allocation
Browse files Browse the repository at this point in the history
Use a globally allocated buffer when viable.
Reduce static buffer size from 64k to 16k.

Signed-off-by: Gaukas Wang <i@gaukas.wang>
  • Loading branch information
gaukas committed Feb 8, 2024
1 parent 38c2361 commit ab7c0dd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
15 changes: 12 additions & 3 deletions tinygo/v0/examples/reverse/wrapper_transport.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package main

import (
"io"

v0 "github.com/gaukas/watm/tinygo/v0"
v0net "github.com/gaukas/watm/tinygo/v0/net"
)

var dupBuf []byte = make([]byte, 16384) // 16k buffer for reversing

// type guard: ReverseWrappingTransport must implement [v0.WrappingTransport].
var _ v0.WrappingTransport = (*ReverseWrappingTransport)(nil)

Expand All @@ -20,8 +24,15 @@ type ReverseConn struct {
}

func (rc *ReverseConn) Read(b []byte) (n int, err error) {
var dupBuf []byte = make([]byte, len(b))
n, err = rc.Conn.Read(dupBuf)
if err != nil {
return 0, err
}

if n > len(b) {
err = io.ErrShortBuffer
n = len(b)
}

// reverse all bytes read successfully so far
for i := 0; i < n; i++ {
Expand All @@ -32,8 +43,6 @@ func (rc *ReverseConn) Read(b []byte) (n int, err error) {
}

func (rc *ReverseConn) Write(b []byte) (n int, err error) {
var dupBuf []byte = make([]byte, len(b))

// reverse the bytes to be written
for i := 0; i < len(b); i++ {
dupBuf[i] = b[len(b)-i-1]
Expand Down
10 changes: 5 additions & 5 deletions tinygo/v0/exports.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v0

import (
"bytes"
"errors"
"log"
"os"
Expand Down Expand Up @@ -66,15 +67,14 @@ func readConfig() (config []byte, err error) {
}

// read the config file
var fileContent []byte = make([]byte, 65536)
n, err := file.Read(fileContent)
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(file)
if err != nil {
log.Println("readConfig: file.Read:", err)
log.Println("readConfig: (*bytes.Buffer).ReadFrom:", err)
return nil, syscall.EIO
}

config = make([]byte, n)
copy(config, fileContent[:n])
config = buf.Bytes()

// close the file
if err := file.Close(); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions tinygo/v0/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var cancelConn v0net.Conn // cancelConn is used to cancel the entire worker.

var workerFn func() int32 = unfairWorker // by default, use unfairWorker

var readBuf []byte = make([]byte, 16384) // 16k buffer for reading

// WorkerFairness sets the fairness of a worker.
//
// If sourceConn or remoteConn will not work in non-blocking mode,
Expand Down Expand Up @@ -90,7 +92,6 @@ func untilError(f func() error) error {
//
// TODO: use poll_oneoff instead of busy polling
func unfairWorker() int32 {
var readBuf []byte = make([]byte, 65536)
for {
pollFd := []pollFd{
{
Expand Down Expand Up @@ -195,7 +196,6 @@ func unfairWorker() int32 {
//
// TODO: use poll_oneoff instead of busy polling
func fairWorker() int32 {
var readBuf []byte = make([]byte, 65536)
for {
pollFd := []pollFd{
{
Expand Down Expand Up @@ -256,8 +256,8 @@ func fairWorker() int32 {
}

func copyOnce(dstName, srcName string, dst, src net.Conn, buf []byte) error {
if buf == nil {
buf = make([]byte, 65536)
if len(buf) == 0 {
buf = make([]byte, 16384) // 16k buffer for reading
}

readN, readErr := src.Read(buf)
Expand Down

0 comments on commit ab7c0dd

Please sign in to comment.