Skip to content

Commit

Permalink
Fix cache files being padded with trailing 0's
Browse files Browse the repository at this point in the history
buffer() was offseting the array instead of truncating the returned
array.
  • Loading branch information
ben-clayton committed Oct 31, 2023
1 parent cb5b33c commit 2f3b68c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
14 changes: 7 additions & 7 deletions src/unittests/serialization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ g.test('value').fn(t => {
]) {
const s = new BinaryStream(new Uint8Array(1024).buffer);
serializeValue(s, value);
const d = new BinaryStream(s.buffer());
const d = new BinaryStream(s.buffer().buffer);
const deserialized = deserializeValue(d);
t.expect(
objectEquals(value, deserialized),
Expand Down Expand Up @@ -246,7 +246,7 @@ g.test('fpinterval_f32').fn(t => {
]) {
const s = new BinaryStream(new Uint8Array(1024).buffer);
serializeFPInterval(s, interval);
const d = new BinaryStream(s.buffer());
const d = new BinaryStream(s.buffer().buffer);
const deserialized = deserializeFPInterval(d);
t.expect(
objectEquals(interval, deserialized),
Expand Down Expand Up @@ -282,7 +282,7 @@ g.test('fpinterval_f16').fn(t => {
]) {
const s = new BinaryStream(new Uint8Array(1024).buffer);
serializeFPInterval(s, interval);
const d = new BinaryStream(s.buffer());
const d = new BinaryStream(s.buffer().buffer);
const deserialized = deserializeFPInterval(d);
t.expect(
objectEquals(interval, deserialized),
Expand Down Expand Up @@ -318,7 +318,7 @@ g.test('fpinterval_abstract').fn(t => {
]) {
const s = new BinaryStream(new Uint8Array(1024).buffer);
serializeFPInterval(s, interval);
const d = new BinaryStream(s.buffer());
const d = new BinaryStream(s.buffer().buffer);
const deserialized = deserializeFPInterval(d);
t.expect(
objectEquals(interval, deserialized),
Expand All @@ -340,7 +340,7 @@ g.test('expression_expectation').fn(t => {
]) {
const s = new BinaryStream(new Uint8Array(1024).buffer);
serializeExpectation(s, expectation);
const d = new BinaryStream(s.buffer());
const d = new BinaryStream(s.buffer().buffer);
const deserialized = deserializeExpectation(d);
t.expect(
objectEquals(expectation, deserialized),
Expand Down Expand Up @@ -370,7 +370,7 @@ g.test('anyOf').fn(t => {
]) {
const s = new BinaryStream(new Uint8Array(1024).buffer);
serializeComparator(s, c.comparator);
const d = new BinaryStream(s.buffer());
const d = new BinaryStream(s.buffer().buffer);
const deserialized = deserializeComparator(d);
for (const val of c.testCases) {
const got = deserialized.compare(val);
Expand Down Expand Up @@ -398,7 +398,7 @@ g.test('skipUndefined').fn(t => {
]) {
const s = new BinaryStream(new Uint8Array(1024).buffer);
serializeComparator(s, c.comparator);
const d = new BinaryStream(s.buffer());
const d = new BinaryStream(s.buffer().buffer);
const deserialized = deserializeComparator(d);
for (const val of c.testCases) {
const got = deserialized.compare(val);
Expand Down
10 changes: 5 additions & 5 deletions src/webgpu/shader/execution/expression/case_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ export class CaseCache implements Cacheable<Record<string, CaseList>> {
*/
serialize(data: Record<string, CaseList>): Uint8Array {
const maxSize = 32 << 20; // 32MB - max size for a file
const s = new BinaryStream(new Uint8Array(maxSize).buffer);
s.writeU32(Object.keys(data).length);
const stream = new BinaryStream(new ArrayBuffer(maxSize));
stream.writeU32(Object.keys(data).length);
for (const name in data) {
s.writeString(name);
s.writeArray(data[name], serializeCase);
stream.writeString(name);
stream.writeArray(data[name], serializeCase);
}
return new Uint8Array(s.buffer());
return stream.buffer();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/webgpu/util/binary_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export default class BinaryStream {
}

/** buffer() returns the stream's buffer sliced to the 8-byte rounded read or write offset */
buffer(): ArrayBufferLike {
return new Uint8Array(this.view.buffer, align(this.offset, 8)).buffer;
buffer(): Uint8Array {
return new Uint8Array(this.view.buffer, 0, align(this.offset, 8));
}

/** writeBool() writes a boolean as 255 or 0 to the buffer at the next byte offset */
Expand Down

0 comments on commit 2f3b68c

Please sign in to comment.