Skip to content

Instantly share code, notes, and snippets.

View trevnorris's full-sized avatar

Trevor Norris trevnorris

View GitHub Profile
var smalloc = require('smalloc');
var uid = 0;
function MemStruct() {
this._id = 0; // 4 bytes (uint32)
this._address = 1; // 4 bytes (uint32)
this._data = 2; // 8 bytes (uint64)
var size = 4;
smalloc.alloc(size, this, smalloc.Types.Uint32);
function reblaze(vals, fn) {
if (typeof fn !== 'function')
throw new TypeError('fn should be a function');
// V8 will automatically throw if vals is not an object.
var keys = Object.keys(vals);
var regexp;
var fn_name = fn.name;
Below results use latest iojs v1.x branch and node v0.11 branch.
$ /usr/bin/time iojs primality-test.js
13.01user 0.16system 0:13.16elapsed 100%CPU (0avgtext+0avgdata 682960maxresident)k
0inputs+0outputs (0major+45067minor)pagefaults 0swaps
$ /usr/bin/time node primality-test.js
3.05user 0.20system 0:03.25elapsed 100%CPU (0avgtext+0avgdata 871784maxresident)k
@trevnorris
trevnorris / tracing-additions.cc
Created February 5, 2015 18:25
How to hook into additional V8 tracing API. Also, commented out is how to use backtrace(3)
#include <v8.h>
#include <node.h>
#include <assert.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

This is a very rough and verbose overview of an API that is simplistic and can be kept consistent across the entire code base. I visited many API possibilities that could have been used, and this API came out as the best combination of everything available.

First we'll start with examples from the file system API. This is a partial set of all features that already exist, also included are some new APIs.

File System

@trevnorris
trevnorris / generator-vs-stateful.js
Last active August 29, 2015 14:16
Comparing generators with stateful objects, for performance
'use strict';
const ITER = 1e7;
let t;
function* G() {
let i = 0;
for (; i < ITER; i++)
yield i;
}
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000000001fb50f0 in ?? ()
(gdb) bt
#0 0x0000000001fb50f0 in ?? ()
#1 0x000000000040be54 in uv__finish_close (handle=0x1df91e0) at lib/libnub/deps/uv/src/unix/core.c:245
#2 uv__run_closing_handles (loop=0x1b141e0) at lib/libnub/deps/uv/src/unix/core.c:259
#3 uv_run (loop=0x1b141e0, mode=UV_RUN_DEFAULT) at lib/libnub/deps/uv/src/unix/core.c:326
#4 0x0000000000405eef in hw_http_open ()
#5 0x0000000000403b57 in main ()
(gdb) f 1

In the following example data is being read from a file, undergoes a transform then is written to a port. How are the following things handled?

  1. If attempting to write to the port results in an error, how do we let the file know it's time to close?

  2. If the file is deleted under our feet, how do we let the port know to close?

  3. If the transform has a parsing error how does it alert both the file and the port they need to close and cleanup?

@trevnorris
trevnorris / .eslintrc
Last active August 29, 2015 14:18
My .eslintrc of choice. Still have a few things to work out.
{
'ecmaFeatures': {
'binaryLiterals': true,
'blockBindings': true,
'forOf': true,
'generators': true,
'octalLiterals': true,
'templateStrings': true,
},
'env': {
'use strict';
class Readable {
constructor() {
this._onreadable = null;
// Acquire the default onreadable callback if it's been set.
if (typeof this.constructor._onreadable === 'function')
this._onreadable = this.constructor._onreadable;
}