Skip to content

Commit

Permalink
cleanup: optimize algorithm used for load/loadInput/store
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Nov 17, 2023
1 parent 2bf07ca commit 5540059
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions extism_pdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,54 @@ const (

func load(offset extismPointer, buf []byte) {
length := len(buf)
chunkCount := length >> 3

for i := 0; i < length; i++ {
if length-i >= 8 {
x := extism_load_u64(offset + extismPointer(i))
binary.LittleEndian.PutUint64(buf[i:i+8], x)
i += 7
continue
}
buf[i] = extism_load_u8(offset + extismPointer(i))
for chunkIdx := 0; chunkIdx < chunkCount; chunkIdx++ {
i := chunkIdx << 3
binary.LittleEndian.PutUint64(buf[i:i+8], extism_load_u64(offset+extismPointer(i)))
}

remainder := length & 7
remainderOffset := chunkCount << 3
for index := remainderOffset; index < (remainder + remainderOffset); index++ {
buf[index] = extism_load_u8(offset + extismPointer(index))
}
}

func loadInput() []byte {
length := int(extism_input_length())
buf := make([]byte, length)

for i := 0; i < length; i++ {
if length-i >= 8 {
x := extism_input_load_u64(extismPointer(i))
binary.LittleEndian.PutUint64(buf[i:i+8], x)
i += 7
continue
}
buf[i] = extism_input_load_u8(extismPointer(i))
chunkCount := length >> 3

for chunkIdx := 0; chunkIdx < chunkCount; chunkIdx++ {
i := chunkIdx << 3
binary.LittleEndian.PutUint64(buf[i:i+8], extism_input_load_u64(extismPointer(i)))
}

remainder := length & 7
remainderOffset := chunkCount << 3
for index := remainderOffset; index < (remainder + remainderOffset); index++ {
buf[index] = extism_input_load_u8(extismPointer(index))
}

return buf
}

func store(offset extismPointer, buf []byte) {
length := len(buf)
chunkCount := length >> 3

for i := 0; i < length; i++ {
if length-i >= 8 {
x := binary.LittleEndian.Uint64(buf[i : i+8])
extism_store_u64(offset+extismPointer(i), x)
i += 7
continue
}
for chunkIdx := 0; chunkIdx < chunkCount; chunkIdx++ {
i := chunkIdx << 3
x := binary.LittleEndian.Uint64(buf[i : i+8])
extism_store_u64(offset+extismPointer(i), x)
}

extism_store_u8(offset+extismPointer(i), buf[i])
remainder := length & 7
remainderOffset := chunkCount << 3
for index := remainderOffset; index < (remainder + remainderOffset); index++ {
extism_store_u8(offset+extismPointer(index), buf[index])
}
}

Expand Down

0 comments on commit 5540059

Please sign in to comment.