Created
November 7, 2015 07:42
-
-
Save bmeck/9548d2bc875bbe67fdb3 to your computer and use it in GitHub Desktop.
an abomination while reducing mem usage during node knockout
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
| import fs from "fs"; | |
| import path from "path"; | |
| const readFileRange = (file, start, stop) => { | |
| const fd = fs.openSync(file, 'r'); | |
| let length = stop - start; | |
| let read = 0; | |
| const buf = new Buffer(length); | |
| while (read !== length) { | |
| read += fs.readSync(fd, buf, read, length - read, start + read); | |
| } | |
| fs.close(fd); | |
| return buf; | |
| } | |
| // Yes, this uses a *lot* of sync methods | |
| export default class SplitSnapshotProvider { | |
| constructor(outdir) { | |
| this.outdir = outdir; | |
| this.snapshot = JSON.parse(fs.readFileSync(path.join(outdir, 'snapshot.json'))); | |
| this.node_arr_length = this.snapshot.node_count * this.snapshot.meta.node_fields.length; | |
| this.node_fields_size = this.snapshot.meta.node_fields.length * 4; | |
| this.node_struct_size = this.node_fields_size + 4; | |
| this.edge_arr_length = this.snapshot.edge_count * this.snapshot.meta.edge_fields.length; | |
| this.edge_struct_size = this.snapshot.meta.edge_fields.length * 4 + 4; | |
| this.string_index_struct_size = 4 + 4; | |
| } | |
| getNodeArraySize() { | |
| return this.node_arr_length; | |
| } | |
| getNodeArraySlice(b,e) { | |
| if (b === e) return []; | |
| const chunk = readFileRange( | |
| path.join(this.outdir, 'nodes'), | |
| b * this.node_struct_size, | |
| e * this.node_struct_size | |
| ); | |
| let ret = []; | |
| // console.log('chunk',b,e, chunk, chunk.length) | |
| for (let i = 0; i < chunk.length; i += this.node_struct_size) { | |
| for (let ii = 0; ii < this.node_fields_size; ii += 4) { | |
| ret.push( | |
| chunk.readUInt32BE(i + ii) | |
| ); | |
| } | |
| } | |
| return ret; | |
| } | |
| getEdgeArraySize() { | |
| return this.edge_arr_length; | |
| } | |
| getEdgeArraySlice(b,e) { | |
| if (b === e) return []; | |
| const chunk = readFileRange( | |
| path.join(this.outdir, 'edges'), | |
| b * this.edge_struct_size, | |
| e * this.edge_struct_size | |
| ); | |
| let ret = []; | |
| for (let i = 0; i < chunk.length; i += 4) { | |
| ret.push( | |
| chunk.readUInt32BE(i * 4) | |
| ); | |
| } | |
| return ret; | |
| } | |
| getEdgeIndexForNode(n) { | |
| const chunk = readFileRange( | |
| path.join(this.outdir, 'nodes'), | |
| n * this.node_struct_size + this.node_fields_size, | |
| (n + 1) * this.node_struct_size | |
| ); | |
| return chunk.readUInt32BE(0); | |
| } | |
| getTraceFunctionInfoArraySize() { | |
| return 0; | |
| } | |
| getTraceFunctionInfoArraySlice(b,e) { | |
| return []; | |
| } | |
| getTraceTreeArraySize() { | |
| return 0; | |
| } | |
| getTraceTreeArraySlice(b,e) { | |
| return []; | |
| } | |
| getString(n) { | |
| const chunk = readFileRange( | |
| path.join(this.outdir, 'string_indices'), | |
| n * this.string_index_struct_size, | |
| (n + 1) * this.string_index_struct_size | |
| ); | |
| let offset = chunk.readUInt32BE(0); | |
| let length = chunk.readUInt32BE(4); | |
| return readFileRange( | |
| path.join(this.outdir, 'strings'), | |
| offset, | |
| offset + length | |
| ).toString(); | |
| } | |
| getMeta() { | |
| return this.snapshot.meta; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment