Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read 20MBs but send response in chunks of 2MB #7990

Open
wants to merge 3 commits into
base: refactor-usage-sn
Choose a base branch
from

Conversation

vermakhushboo
Copy link
Member

What does this PR do?

The code efficiently reads data in chunks of APP_STORAGE_READ_BUFFER size and sends it in chunks of MAX_OUTPUT_CHUNK_SIZE, optimizing the transfer process for large files.

Test Plan

Tested using node SDK. Download time went down 4x from 68528ms to 25188ms.

Related PRs and Issues

  • (Related PR or issue)

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

$bytesToRead = min(APP_STORAGE_READ_BUFFER, $size - $bytesRead);

// Read data from the device and append to the buffer
$buffer .= $deviceFiles->read($path, $bytesRead, $bytesToRead);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should append to buffer, because this means by the end whole file in the memory, and this logic is to prevent exactly that. Just keep the recent chunk in the buffer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not appending the entire file to the buffer. If you look at the loop below, we remove each chunk from the buffer after processing.

// Remove the sent chunk from the buffer
$buffer = substr($buffer, MAX_OUTPUT_CHUNK_SIZE);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do this, why not instead of $buffer .= $deviceFiles->read($path, $bytesRead, $bytesToRead); simply use $buffer = $deviceFiles->read($path, $bytesRead, $bytesToRead); and when loop ends simply $buffer = null. The substr adds more processing

Copy link
Member

@lohanidamodar lohanidamodar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants