Skip to content

Instantly share code, notes, and snippets.

View 2bam's full-sized avatar

Martin 2bam 2bam

  • Valencia, Spain
  • 11:38 (UTC +02:00)
View GitHub Profile
@2bam
2bam / 0-results-array-extension-benchmark.md
Last active December 21, 2024 19:45
JavaScript array extension methods, rough benchmarks for QuickJS and Node V8

JavaScript array extension methods, rough benchmarks for QuickJS and Node V8

Results / TL;DR:

  • In QuickJS dst.push.apply(dst, ext) is faster by a 200% margin over others.
  • In V8 concat is better for few extensions specially for big arrays, otherwise push wins for more than a few extensions of small sized arrays. Somewhat depends of GC frequency.
@2bam
2bam / dungeons-n-diagrams-puzzle-generator.js
Last active December 9, 2024 00:33
Dungeons and diagrams like puzzle generator using DFS/backtracking
//
// Dungeons and diagrams like puzzle generator
// 2bam.com 2024
//
// There's room for improvement (in C):
// - Fit board into int64 masks
// - Use "&" + popcount intrinsics to count walls
const STOP_ON_FIRST_FOUND = false;
const PRINT_EACH_BOARD = true;
@2bam
2bam / turn.ts
Last active August 17, 2024 11:40
Idea for animating model-view turn based games with queues
/*
This is a great question!
For me the breakthrough was not to focus on the Model or the View but the line
in between them: Queues.
The most important part of abstractions is the communication.
By enqueuing you don't infer the animation from a state diff, you rather encode
the diff patch into an event message (aka Command pattern) which commits a game
@2bam
2bam / batlog.gnuplot
Last active July 16, 2023 19:22
Record battery and cpu status to CSV file every minute until stopped (Linux / Bash)
#
# gnuplot script to show energy and cpu by time from batlog.sh
#
# To run:
# gnuplot -persist batlog.gnuplot
#
set datafile separator ","
set xdata time
my_fmt = "%a %b %d %H:%M:%S %Y"
@2bam
2bam / gist:0d51158a865765e3c16ddcdc8b8ca525
Created May 7, 2023 10:02
JS sorting algorithm benchmarks [keeping it in case it's lost]
//
// Thx https://www.measurethat.net/Benchmarks/Show/3549/0/javascript-sorting-algorithms
//
// 1000x random ordered: https://www.measurethat.net/Benchmarks/Show/3549/0/javascript-sorting-algorithms
// 1000x almost ordered: https://www.measurethat.net/Benchmarks/Show/25092/0/javascript-sorting-algorithms-best-case-almost-ordered
//
function swap(ary, a, b) {
var t = ary[a];
ary[a] = ary[b];
@2bam
2bam / gist:dc4ca3ec60c744e72868b58a67b4d423
Created October 10, 2022 18:48
Capture libcamera in OpenCV through GStreamer pipeline for a Raspberry Pi
VideoCapture cap("libcamerasrc ! video/x-raw,width=640,height=480 ! appsink", CAP_GSTREAMER);
@2bam
2bam / setup-git-auth.cs
Last active September 11, 2022 14:46
Setup Git For Windows authentication to run git commands from within C# program
void SetupGitAuthentication()
{
// Set the path so git's ssh executables are called instead of Windows'
Environment.SetEnvironmentVariable("PATH", "C:\\Program Files\\Git\\usr\\bin;" + Environment.GetEnvironmentVariable("PATH"));
// This duplicates what "start-ssh-agent.cmd" does to find the agent PID and socket file,
// in order to set SSH_AGENT_PID, SSH_AUTH_SOCK so git works.
// We heuristically take the first we find, as the script does. This seems to work, as
// possibly the PID is not even necessary.
var tmpPath = Path.GetTempPath();
@2bam
2bam / git-for-windows-ssh-problem-permission-denied-publickey.md
Last active April 4, 2023 21:44
Git For Windows "Permission Denied (publickey)" SSH problem calling `git` from CMD/PowerShell, only accepting id_rsa, asking each time the passphrase.

If you:

  • are using PowerShell or CMD directly
  • don't want to be forced to use id_rsa
  • don't want to input a passphrase on each call to git
  • don't want to use Git Bash because you want to use it from CMD/PowerShell directly you need some troubleshooting to use git installation executables and a prior setup:

1. Start or get currently running agent into env vars (PID and socket/pipe file)

start-ssh-agent.cmd

@2bam
2bam / CInteropProxy.cs
Last active March 31, 2022 01:25
Acceptable way to pin/fix structs for C#-C interop including swap chain arrays by using-Dispose pattern without GetPinnableReference because that's garbage.
internal class CInteropProxy {
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
unsafe delegate int C_stepDelegate(World.Pinned* world);
C_stepDelegate? c_step; // Set with kernel32.dll GetProcAddress()
public void Step(in World world)
{
unsafe
{