Skip to content

Commit

Permalink
simplify quadrant buffering
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Jul 24, 2024
1 parent 9f8eabd commit 1ab06a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
40 changes: 19 additions & 21 deletions store/file/ods.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ func CreateODSFile(
return nil, fmt.Errorf("file create: %w", err)
}

h, err := writeODSFile(f, eds, roots)
bufSize := int(eds.Width()) * share.Size
buf := bufio.NewWriterSize(f, bufSize)

h, err := writeODSFile(buf, eds, roots)
if err != nil {
return nil, fmt.Errorf("writing ODS file: %w", err)
}

err = buf.Flush()
if err != nil {
return nil, fmt.Errorf("flushing ODS file: %w", err)
}

err = f.Sync()
if err != nil {
return nil, fmt.Errorf("syncing file: %w", err)
Expand Down Expand Up @@ -110,36 +118,26 @@ func writeODSFile(w io.Writer, eds *rsmt2d.ExtendedDataSquare, axisRoots *share.
}

// write quadrants
err = writeQuadrant(w, eds, int(h.shareSize), 0)
err = writeQ1(w, eds)
if err != nil {
return nil, fmt.Errorf("writing Q1: %w", err)
}

return h, nil
}

// writeQuadrant writes the quadrant of the square to the writer. it writes the quadrant in row-major
// order. It uses buffer to write the shares in bulk (row by row), which improves the write performance.
func writeQuadrant(w io.Writer, eds *rsmt2d.ExtendedDataSquare, shareSize, quadrantIdx int) error {
fromRow := quadrantIdx / 2 * int(eds.Width()) / 2
toRow := fromRow + int(eds.Width())/2

fromCol := quadrantIdx % 2 * int(eds.Width()) / 2
toCol := fromCol + int(eds.Width())/2

buf := bufio.NewWriterSize(w, shareSize*int(eds.Width()))
for rowIdx := fromRow; rowIdx < toRow; rowIdx++ {
for colIdx := fromCol; colIdx < toCol; colIdx++ {
share := eds.GetCell(uint(rowIdx), uint(colIdx))
_, err := buf.Write(share)
// writeQ1 writes the first quadrant of the square to the writer. It writes the quadrant in row-major
// order
func writeQ1(w io.Writer, eds *rsmt2d.ExtendedDataSquare) error {
for i := range eds.Width() / 2 {
for j := range eds.Width() / 2 {
shr := eds.GetCell(i, j) // TODO: Avoid copying inside GetCell
_, err := w.Write(shr)
if err != nil {
return fmt.Errorf("writing shares: %w", err)
return fmt.Errorf("writing share: %w", err)
}
}
}
err := buf.Flush()
if err != nil {
return fmt.Errorf("flushing buffer: %w", err)
}
return nil
}

Expand Down
27 changes: 26 additions & 1 deletion store/file/q1q4_file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package file

import (
"bufio"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -39,16 +40,40 @@ func CreateQ1Q4File(path string, roots *share.AxisRoots, eds *rsmt2d.ExtendedDat
return nil, err
}

err = writeQuadrant(ods.fl, eds, int(ods.hdr.shareSize), 3)
bufSize := int(eds.Width()) * int(ods.hdr.shareSize)
buf := bufio.NewWriterSize(ods.fl, bufSize)

err = writeQ4(buf, eds)
if err != nil {
return nil, fmt.Errorf("writing Q4: %w", err)
}

err = buf.Flush()
if err != nil {
return nil, fmt.Errorf("flushing Q4: %w", err)
}

return &Q1Q4File{
ods: ods,
}, nil
}

// writeQ4 writes the frth quadrant of the square to the writer. iIt writes the quadrant in row-major
// order
func writeQ4(w io.Writer, eds *rsmt2d.ExtendedDataSquare) error {
half := eds.Width() / 2
for i := range half {
for j := range half {
shr := eds.GetCell(i+half, j+half) // TODO: Avoid copying inside GetCell
_, err := w.Write(shr)
if err != nil {
return fmt.Errorf("writing share: %w", err)
}
}
}
return nil
}

func (f *Q1Q4File) Size(ctx context.Context) int {
return f.ods.Size(ctx)
}
Expand Down

0 comments on commit 1ab06a6

Please sign in to comment.