Skip to content

Instantly share code, notes, and snippets.

View corporatepiyush's full-sized avatar

Piyush Katariya corporatepiyush

View GitHub Profile
"""
The most atomic way to train and run inference for a GPT in pure, dependency-free Python.
This file is the complete algorithm.
Everything else is just efficiency.
@karpathy
"""
import os # os.path.exists
import math # math.log, math.exp
@corporatepiyush
corporatepiyush / For.java
Created October 16, 2025 21:26
Optimizing For loops
import java.util.Random;
public class For {
static final int SIZE = 1_000_000;
public static void main(String[] args) {
int[] data = new int[SIZE];
Random rnd = new Random(42);
for (int i = 0; i < SIZE; i++) data[i] = rnd.nextInt(1000) - 500;
@corporatepiyush
corporatepiyush / Matmul.c
Last active October 16, 2025 08:11
Matrix Multiplication
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
// REQUIRED for the M1/Apple Silicon native BLAS implementation (Accelerate Framework)
// gcc -O3 -Wall -Wextra Matmul.c -o Matmul -lm -framework Accelerate
#include "cblas.h"
// --- Utility Functions ---
echo 'export DEB_CFLAGS_MAINT_APPEND="-O3 -march=native -ftree-vectorize -flto -fprefetch-loop-arrays"' | sudo tee -a /etc/dpkg/buildflags.conf
echo 'export DEB_CXXFLAGS_MAINT_APPEND="-O3 -march=native -ftree-vectorize -flto -fprefetch-loop-arrays"' | sudo tee -a /etc/dpkg/buildflags.conf
@corporatepiyush
corporatepiyush / BufferPool.dart
Last active October 10, 2025 20:43
High Performace Application level Buffer Pool for Networking, Image Processing, Graphics, Game engines and File IO Tasks
/// @file A high-performance buffer pool implementation in Dart for managing TypedData.
/// @author Piyush Katariya
/// @version 1.5.0
library buffer_pool;
import 'dart:typed_data';
import 'dart:math';
import 'dart:ffi';
import 'package:ffi/ffi.dart';
@corporatepiyush
corporatepiyush / bp-golang.md
Created October 4, 2025 11:30
Top 100 Best Practices for Golang

## Memory Management & Allocation

  1. Pre-allocate Slices with Known Capacity When the eventual size of a slice is known, pre-allocating with make([]T, 0, capacity) creates the underlying array a single time. This critical practice avoids multiple, inefficient reallocations and the expensive process of copying all existing elements to a new, larger array as you append data.

  2. Use the arena Package for Short-Lived Objects New in recent Go versions, the arena package provides a safe way to allocate memory that can be freed all at once. This is perfect for functions that create many temporary objects (like during a single request), as it can nearly eliminate GC pressure from that workload.

@corporatepiyush
corporatepiyush / benchmark.js
Created September 23, 2025 08:40
Benchmark for JSON vs Binary and why JS/V8 so stupid
// benchmark.js
import { faker } from '@faker-js/faker';
import fs from 'fs';
import crypto from 'crypto';
// Text encoder/decoder for binary operations
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
// Mock MessageType enum
@corporatepiyush
corporatepiyush / bufferpool.c
Last active September 19, 2025 15:29
Custom memory allocator with Buffer Pool
#define _GNU_SOURCE
#include "bufferpool.h"
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <stdatomic.h>
#include <dlfcn.h>
#include <errno.h>
#include <sys/mman.h>
#include <inttypes.h>
@corporatepiyush
corporatepiyush / PerfGuide.md
Last active September 15, 2025 09:59
General guide to tune CPU, Memory and IO

A Language-Agnostic Guide to High-Performance Software Refactoring

1. CPU Optimization: From Microarchitecture to Application Logic

1.1. Low-Level CPU and Cache Optimization

1.1.1. Understanding CPU-Bound vs. I/O-Bound Workloads

@corporatepiyush
corporatepiyush / http2.js
Last active September 15, 2025 08:09
HTTP 2 implementation
'use strict';
process.title = 'http2server';
console.error('SERVER PID', process.pid);
/* ---------- Linux early tuning ------------------------------ */
if (process.platform === 'linux') {
try {
require('fs').writeFileSync('/proc/sys/net/core/somaxconn', '65535');