Skip to content

Instantly share code, notes, and snippets.

View trevnorris's full-sized avatar

Trevor Norris trevnorris

View GitHub Profile
@trevnorris
trevnorris / prime-generator.js
Last active October 11, 2021 17:41
fastest js prime generator been able to get
// Thanks to @isntitvacant (https://github.com/chrisdickinson) for optimizing the
// bit shift performance tweaks.
var SB = require('buffer').SlowBuffer;
var ITER = 2e4;
var SIZE = 1e3;
function genPrimes(max) {
var primes = new Array();
var len = (max >>> 3) + 1;

build

Clone and build Node for analysis:

$ git clone https://github.com/joyent/node.git
$ cd node
$ export GYP_DEFINES="v8_enable_disassembler=1"
$ ./configure
$ make -j4

Disclaimer: I work for Mozilla and am a Node.js core maintainer. The opinions expressed here do not reflect those of Mozilla or the Node.js project.

After watching the events unfold the last 2 days I decided to sit down, with my two cartons of Trader Joe's eggnog, and simply express with all humility and confidence what libuv@47d98b6 definitely was not.

Over the last year I have had the pleasure of being mentored by Ben. He is largely responsible for my initial major involvement writing performance improvements for Node, and has easily surpassed

@trevnorris
trevnorris / perf-flame-graph-notes.md
Last active December 24, 2023 05:25
Quick steps of how to create a flame graph using perf

The prep-script.sh will setup the latest Node and install the latest perf version on your Linux box.

When you want to generate the flame graph, run the following (folder locations taken from install script):

sudo sysctl kernel.kptr_restrict=0
# May also have to do the following:
# (additional reading http://unix.stackexchange.com/questions/14227/do-i-need-root-admin-permissions-to-run-userspace-perf-tool-perf-events-ar )
sudo sysctl kernel.perf_event_paranoid=0
@trevnorris
trevnorris / hello-world.js
Created April 11, 2014 07:57
Playing the "who's bigger" hello world game.
var TCP = process.binding('tcp_wrap').TCP;
var SlowBuffer = require('buffer').SlowBuffer;
var util = require('util');
var err;
var headers = 'HTTP/1.1 200 OK\r\n' +
'Connection: Keep-Alive\r\n' +
'Content-Type: text/plain; charset=latin-1\r\n' +
'Content-Length: 12\r\n\r\n' +
'hello world\n';
#include <stdio.h>
#include <assert.h>
#include "uv.h"
#define ITER 1e6
#define ACCESS_ONCE(type, var) \
(*(volatile type*) &(var))
static uv_mutex_t mutex;

Notes:

  • Text in [[ ]] are the internal libuv function call.
  • Text in {{ }} are the Node functions that are affected.
  • Text in ( ) are notes about what is happening.
  • While the Windows event loop has minor variations, I don't believe any of those affect Node.

On process.nextTick():

@trevnorris
trevnorris / perf-workshop-snippets.md
Last active August 29, 2015 14:03
(do not delete) Snippets for performance workshops
/**
 * Ordered fastest to slowest.
 * Different ways to extract data from a Buffer in C++.
 */

void RunMe(const FunctionCallbackInfo<Value>& args) {
  /*
  Local<Object> buf = args[0].As<Object>();
 char* data = static_cast(buf-&gt;GetIndexedPropertiesExternalArrayData());
//enter with m<nn/2 and n<nn/2
function hyp(x, n, m, nn) {
var nz, mz;
// best to have n<m
if (m < n) {
nz = m;
mz = n
} else {
nz = n;
@trevnorris
trevnorris / all-my-knowledge.md
Last active December 9, 2017 00:39
Here's a rundown of everything I use to do performance analysis in Node.

Introduction

Here is a collection of tips and tricks I've picked up about doing performance analysis on Node. Included is a build script that should get a base install of Ubuntu fully functional and ready for all the things we'll be going through.

The script pulls a lot of code from the latest master of each repository. So it's possible that something may fail, but to date I haven't had any issues.

First, go ahead and run the script. Then go take a nice long break. It'll