Skip to content

Instantly share code, notes, and snippets.

View corporatepiyush's full-sized avatar

Piyush Katariya corporatepiyush

View GitHub Profile
@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 ---
@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 / 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 / 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 / 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');
@corporatepiyush
corporatepiyush / rsa.dart
Created September 11, 2025 10:29
RSA 2048 Key Algo
import 'dart:typed_data';
import 'dart:math';
// SIMD-optimized big integer operations using Float32x4 and Int32x4
class SIMDBigInt {
static final _rng = Random.secure();
// Convert regular int to SIMD-friendly chunks
static Float32x4List toFloat32x4List(List<int> values) {
final padded = List<int>.from(values);
@corporatepiyush
corporatepiyush / ecs.go
Last active September 10, 2025 15:51
Entity Component System
package main
import "fmt"
// --- CORE TYPES ---
// Entity is a unique identifier.
type Entity uint32
// ComponentType is the identifier for a component type, used for bitmasking.
@corporatepiyush
corporatepiyush / lruCache.js
Created September 6, 2025 07:03
Highly Optimized in memory Cache with Data-Oriented Design
export class LRUCache<K, V> {
private readonly keys: K[];
private readonly values: V[];
private readonly accessOrder: number[]; // Index-based linked list
private readonly keyToIndex = new Map<K, number>();
private readonly maxSize: number;
private head = 0;
private tail = 0;
private size = 0;