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 2ef3f32 commit c541e60
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
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 c541e60

Please sign in to comment.