Repository navigation
Reduce memory used by repeated buffered File writes #32
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| require 'tmpdir' | ||
|
|
||
| count = Integer(ARGV.fetch(0, '16384')) | ||
| piece = "aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa\n" | ||
| Dir.mktmpdir do |dir| | ||
| path = File.join(dir, 'data') | ||
| start = Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
| File.open(path, 'wb') do |file| | ||
| count.times { file.write(piece) } | ||
| file.flush | ||
| p [file.pos, File.size(path)] | ||
| end | ||
| warn "writes=#{count} seconds=#{Process.clock_gettime(Process::CLOCK_MONOTONIC) - start}" | ||
| p File.binread(path) == piece * count | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Small appends must survive collection, seeking, truncation, and independent | ||
| # handles. File#dup starts with its own flushed write buffer. | ||
| require 'tmpdir' | ||
|
|
||
| Dir.mktmpdir do |dir| | ||
| path = File.join(dir, 'data') | ||
| expected = '' | ||
| File.open(path, 'w+b') do |f| | ||
| 1200.times do |i| | ||
| piece = [i % 256, 0, 255].pack('C*') | ||
| expected << piece | ||
| f.write(piece) | ||
| GC.start if i % 200 == 0 | ||
| end | ||
| p f.pos | ||
| f.flush | ||
| p File.binread(path) == expected | ||
| f.seek(10) | ||
| f.write('xy') | ||
| f.write('z') | ||
| f.flush | ||
| expected[10, 3] = 'xyz' | ||
| p File.binread(path) == expected | ||
| f.truncate(20) | ||
| f.seek(0, IO::SEEK_END) | ||
| f.write('tail') | ||
| f.rewind | ||
| p f.read == expected[0, 20] + 'tail' | ||
| end | ||
|
|
||
| File.open(path, 'ab') do |f| | ||
| f.write('a') | ||
| f.write('b') | ||
| f.seek(0) | ||
| f.write('c') | ||
| end | ||
| p File.binread(path).end_with?('tailabc') | ||
|
|
||
| File.open(path, 'wb') do |f| | ||
| f.write('first') | ||
| other = f.dup | ||
| other.seek(0, IO::SEEK_END) | ||
| other.write('second') | ||
| other.flush | ||
| f.seek(0, IO::SEEK_END) | ||
| f.write('third') | ||
| f.flush | ||
| other.close | ||
| end | ||
| p File.binread(path) | ||
|
|
||
| File.open(path, 'wb') do |f| | ||
| f.sync = true | ||
| f.write('a') | ||
| p File.binread(path) | ||
| f.print('b', 'c') | ||
| p File.binread(path) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45557,18 +45557,22 @@ and file_univ_code = fn ivars -> fn (k: str) -> fn (mode: str) -> | |
| and file_write_at = fn world -> fn (id: int) -> fn (piece: str) -> | ||
| let (_, _, _, iv9) = world in | ||
| let k9 = str_of_int id in | ||
| let cur9 = (match (if map_has iv9 (k9 ++ "@__f_buf") then map_get iv9 (k9 ++ "@__f_buf") else VNil) with | ||
| | VStr h9 -> sget h9 | _ -> "") in | ||
| let buf9 = (if map_has iv9 (k9 ++ "@__f_buf") then map_get iv9 (k9 ++ "@__f_buf") else VNil) in | ||
| // Do not flatten the pending chunks just to ask whether the buffer is | ||
| // empty: repeated small writes must not copy all preceding bytes. | ||
| let empty9 = (match buf9 with VStr h9 -> str_blen h9 == 0 | _ -> true) in | ||
|
284km marked this conversation as resolved.
Resolved
|
||
| let pos9 = (match (if map_has iv9 (k9 ++ "@__f_pos") then map_get iv9 (k9 ++ "@__f_pos") else VNil) with | ||
| | VInt n9 -> n9 | _ -> 0) in | ||
| // what #ungetc pushed back is unread: a write goes where the program's | ||
| // position is (ruby drops its read buffer and seeks back), so | ||
| // `read(3); ungetc("c"); write("Z")` writes at 2, not after the "c" | ||
| let ugl9 = file_unget_len iv9 k9 in | ||
| let pos9 = (if ugl9 > 0 && str_eq cur9 "" | ||
| let pos9 = (if ugl9 > 0 && empty9 | ||
| then (let _ = map_delete iv9 (k9 ++ "@__f_unget") in pos9 - ugl9) else pos9) in | ||
| let _ = (if str_eq cur9 "" then map_set iv9 (k9 ++ "@__f_wpos") (VInt pos9) else ()) in | ||
| let _ = map_set iv9 (k9 ++ "@__f_buf") (mkstr (cur9 ++ piece)) in | ||
| let _ = (if empty9 then map_set iv9 (k9 ++ "@__f_wpos") (VInt pos9) else ()) in | ||
| let _ = (match buf9 with | ||
| | VStr h9 -> str_push h9 piece | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Positive: The existing queued String append is preserved in the merged code. Its buffered-write regression also passes alongside the rebased positioned-read implementation in #33 with indexing ON and OFF. |
||
| | _ -> map_set iv9 (k9 ++ "@__f_buf") (mkstr piece)) in | ||
| map_set iv9 (k9 ++ "@__f_pos") (VInt (pos9 + str_len piece)) | ||
|
|
||
| and file_truncate = fn world -> fn (path: str) -> fn (len: int) -> | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.