Navigation Menu

Skip to content

Commit

Permalink
allow buffer to store MAX_VALUE bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosc90 committed Jun 29, 2020
1 parent ae54595 commit f6b9bdd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
22 changes: 9 additions & 13 deletions cli/js/buffer.ts
Expand Up @@ -133,17 +133,17 @@ export class Buffer implements Reader, ReaderSync, Writer, WriterSync {
// we instead let capacity get twice as large so we
// don't spend all our time copying.
copyBytes(this.#buf.subarray(this.#off), this.#buf);
} else if (c > MAX_SIZE - c - n) {
} else if (c + n > MAX_SIZE) {
throw new Error("The buffer cannot be grown beyond the maximum size.");
} else {
// Not enough space anywhere, we need to allocate.
const buf = new Uint8Array(2 * c + n);
const buf = new Uint8Array(Math.min(2 * c + n, MAX_SIZE));
copyBytes(this.#buf.subarray(this.#off), buf);
this.#buf = buf;
}
// Restore this.#off and len(this.#buf).
this.#off = 0;
this.#reslice(m + n);
this.#reslice(Math.min(m + n, MAX_SIZE));
return m;
};

Expand All @@ -157,31 +157,27 @@ export class Buffer implements Reader, ReaderSync, Writer, WriterSync {

async readFrom(r: Reader): Promise<number> {
let n = 0;
const buf = new Uint8Array(32 * 1024);
while (true) {
const i = this.#grow(MIN_READ);
this.#reslice(i);
const fub = new Uint8Array(this.#buf.buffer, i);
const nread = await r.read(fub);
const nread = await r.read(buf);
if (nread === null) {
return n;
}
this.#reslice(i + nread);
n += nread;
this.writeSync(buf.subarray(0, nread));
}
}

readFromSync(r: ReaderSync): number {
let n = 0;
const buf = new Uint8Array(32 * 1024);
while (true) {
const i = this.#grow(MIN_READ);
this.#reslice(i);
const fub = new Uint8Array(this.#buf.buffer, i);
const nread = r.readSync(fub);
const nread = r.readSync(buf);
if (nread === null) {
return n;
}
this.#reslice(i + nread);
n += nread;
this.writeSync(buf.subarray(0, nread));
}
}
}
Expand Down
23 changes: 19 additions & 4 deletions cli/tests/unit/buffer_test.ts
Expand Up @@ -11,8 +11,8 @@ import {
unitTest,
} from "./test_util.ts";

// N controls how many iterations of certain checks are performed.
const MAX_SIZE = 2 ** 32 - 2;
// N controls how many iterations of certain checks are performed.
const N = 100;
let testBytes: Uint8Array | null;
let testString: string | null;
Expand Down Expand Up @@ -170,15 +170,14 @@ unitTest(async function bufferTooLargeByteWrites(): Promise<void> {

unitTest(function bufferGrowWriteMaxBuffer(): void {
const bufSize = 16 * 1024;
const capacities = [MAX_SIZE, MAX_SIZE - 1, MAX_SIZE - 512];
const capacities = [MAX_SIZE, MAX_SIZE - 1];
for (const capacity of capacities) {
let written = 0;
const buf = new Deno.Buffer();
const writes = Math.floor(capacity / bufSize);
for (let i = 0; i < writes; i++)
written += buf.writeSync(repeat("x", bufSize));

console.log(written, capacity, capacity - written);
if (written < capacity) {
written += buf.writeSync(repeat("x", capacity - written));
}
Expand All @@ -188,20 +187,36 @@ unitTest(function bufferGrowWriteMaxBuffer(): void {
});

unitTest(function bufferGrowReadSyncCloseToMaxBuffer(): void {
const capacities = [MAX_SIZE, MAX_SIZE - 1, MAX_SIZE - 512];
const capacities = [MAX_SIZE, MAX_SIZE - 1];
for (const capacity of capacities) {
const reader = new Deno.Buffer(new ArrayBuffer(capacity));
const buf = new Deno.Buffer();
buf.readFromSync(reader);

assertEquals(buf.length, capacity);
}
});

unitTest(async function bufferGrowReadCloseToMaxBuffer(): Promise<void> {
const capacities = [MAX_SIZE, MAX_SIZE - 1];
for (const capacity of capacities) {
const reader = new Deno.Buffer(new ArrayBuffer(capacity));
const buf = new Deno.Buffer();
await buf.readFrom(reader);
assertEquals(buf.length, capacity);
}
});

unitTest(async function bufferReadCloseToMaxBufferWithInitialGrow(): Promise<
void
> {
const capacities = [MAX_SIZE, MAX_SIZE - 1, MAX_SIZE - 512];
for (const capacity of capacities) {
const reader = new Deno.Buffer(new ArrayBuffer(capacity));
const buf = new Deno.Buffer();
buf.grow(MAX_SIZE);
await buf.readFrom(reader);
assertEquals(buf.length, capacity);
}
});

Expand Down

0 comments on commit f6b9bdd

Please sign in to comment.