Skip to content

Commit 13bbbfd

Browse files
author
Benjamin Pasero
committed
textfiles - introduce an option to read without stream too
1 parent 31e7866 commit 13bbbfd

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

src/vs/workbench/services/textfile/electron-browser/nativeTextFileService.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { WORKSPACE_EXTENSION } from 'vs/platform/workspaces/common/workspaces';
2222
import { joinPath, extname, isEqualOrParent } from 'vs/base/common/resources';
2323
import { Disposable } from 'vs/base/common/lifecycle';
2424
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
25-
import { VSBufferReadable } from 'vs/base/common/buffer';
25+
import { VSBufferReadable, bufferToStream } from 'vs/base/common/buffer';
2626
import { Readable } from 'stream';
2727
import { createTextBufferFactoryFromStream } from 'vs/editor/common/model/textModel';
2828
import { ITextSnapshot } from 'vs/editor/common/model';
@@ -42,6 +42,7 @@ import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
4242
import { IDialogService, IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
4343
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
4444
import { ConfirmResult } from 'vs/workbench/common/editor';
45+
import { assign } from 'vs/base/common/objects';
4546

4647
export class NativeTextFileService extends AbstractTextFileService {
4748

@@ -79,7 +80,16 @@ export class NativeTextFileService extends AbstractTextFileService {
7980
}
8081

8182
async read(resource: URI, options?: IReadTextFileOptions): Promise<ITextFileContent> {
82-
const [bufferStream, decoder] = await this.doRead(resource, options);
83+
const [bufferStream, decoder] = await this.doRead(resource,
84+
assign({
85+
// optimization: since we know that the caller does not
86+
// care about buffering, we indicate this to the reader.
87+
// this reduces all the overhead the buffered reading
88+
// has (open, read, close) if the provider supports
89+
// unbuffered reading.
90+
preferUnbuffered: true
91+
}, options || Object.create(null))
92+
);
8393

8494
return {
8595
...bufferStream,
@@ -98,13 +108,22 @@ export class NativeTextFileService extends AbstractTextFileService {
98108
};
99109
}
100110

101-
private async doRead(resource: URI, options?: IReadTextFileOptions): Promise<[IFileStreamContent, IDecodeStreamResult]> {
111+
private async doRead(resource: URI, options?: IReadTextFileOptions & { preferUnbuffered?: boolean }): Promise<[IFileStreamContent, IDecodeStreamResult]> {
102112

103113
// ensure limits
104114
options = this.ensureLimits(options);
105115

106-
// read stream raw
107-
const bufferStream = await this.fileService.readFileStream(resource, options);
116+
// read stream raw (either buffered or unbuffered)
117+
let bufferStream: IFileStreamContent;
118+
if (options.preferUnbuffered) {
119+
const content = await this.fileService.readFile(resource, options);
120+
bufferStream = {
121+
...content,
122+
value: bufferToStream(content.value)
123+
};
124+
} else {
125+
bufferStream = await this.fileService.readFileStream(resource, options);
126+
}
108127

109128
// read through encoding library
110129
const decoder = await toDecodeStream(streamToNodeReadable(bufferStream.value), {

0 commit comments

Comments
 (0)