Skip to content

Instantly share code, notes, and snippets.

@joelonsql
joelonsql / half_karatsuba.md
Created April 16, 2024 17:34
The Half-Karatsuba Multiplication Algorithm

Karatsuba Multiplication for factors with significant length disparity.

The Half-Karatsuba Multiplication Algorithm is a specialized case of the normal Karatsuba multiplication algorithm, designed for the scenario where var2 has at least twice as many base digits as var1.

In this case var2 (the longer input) is split into high2 and low1, at m2 (half the length of var2) and var1 (the shorter input), is used directly without splitting.

@joelonsql
joelonsql / enhance_rust_rfcs_with_a_minimal_but_realistic_code_example.md
Created August 30, 2023 12:57
Enhance Rust RFCs with a Minimal but Realistic Code Example

Enhance Rust RFCs with a Minimal but Realistic Code Example

Imagine opening a Rust RFC and, right at the top, before any detailed discussion or jargon kicks in, you're met with a small, relatable code example. This snippet isn't just for show; it clearly demonstrates the existing problem, pinpoints why the current approach feels cumbersome or inelegant, and showcases the elegance introduced by the RFC's proposal.

The Idea: Start every Rust RFC with a succinct real-life code example. This example would:

  1. Spotlight the Issue: Directly show what’s problematic in the current system.
@joelonsql
joelonsql / 0243-trait-based-exception-handling.md
Last active August 30, 2023 13:00
0243-trait-based-exception-handling.md with added Illustrative Example
@joelonsql
joelonsql / flashback_logging.md
Created August 18, 2023 07:14
Flashback Logging

Flashback Logging

NOTE: Post-writing, I discovered a similar concept named Ring Buffer Logging. What sets Flashback Logging apart is its approach to handling the buffer. When a log event matches the standard log level threshold, such as 'INFO', the buffer is cleared. This behavior is based on the rationale that such a log event indicates the system's return to normal operations, eliminating the need to keep previously buffered entries.

Introduction

Flashback Logging is a design pattern in the logging and debugging domain. Its primary goal is to reduce the time spent on debugging by making verbose logs readily available during error occurrences without the storage overhead of always keeping verbose logs.

@joelonsql
joelonsql / etalon.c
Created March 21, 2023 10:10
ETALON v1.0 - The ultimate tool for achieving maximum productivity!
/*
* ETALON v1.0
*
* Introducing Elon's Estimator: ETALON
*
* The ultimate tool for achieving maximum productivity!
*
* With just a single command, you can now estimate the completion date of your
* next big project, just like Elon himself. Say goodbye to endless project
* planning and hello to efficiency!
@joelonsql
joelonsql / 0005-fixed-buf.patch
Last active February 20, 2023 21:49
0005-fixed-buf.patch
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index a83feea396..dea55847fd 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -301,6 +301,7 @@ struct NumericData
* This is feasible because the digit buffer is separate from the variable.
* ----------
*/
+#define FIXED_BUF_LEN 8
typedef struct NumericVar
@joelonsql
joelonsql / numeric_from_bytes.sql
Created January 23, 2023 19:20
Function to convert non-negative integer represented as a byte array in big-endian order to numeric
CREATE OR REPLACE FUNCTION numeric_from_bytes(bytea)
RETURNS numeric
LANGUAGE plpgsql
AS $$
declare
bits bit varying;
result numeric := 0;
exponent numeric := 0;
bit_pos integer;
begin
@joelonsql
joelonsql / create_or_lookup_function.sql
Last active January 13, 2023 16:41
Content-Addressed Functions in PostgreSQL
--
-- Content-Addressed Functions
--
-- Inspired by Unison's concept of using a hash of a function's syntax tree
-- as its name, sometimes referred to as "content-addressed naming",
-- we can avoid the need to create/drop lots of different temp function,
-- by using the hash of its definition as its name,
-- and simply reuse the function for all tests that need the same function.
--
-- While Unison uses 512-bit SHA3, we use sha224() since the maximum length
const std = @import("std");
const bigint = std.math.big.int.Managed;
pub fn main() !void {
var a = try bigint.initSet(std.heap.c_allocator, 5);
try a.pow(&a, try std.math.powi(u32, 4, try std.math.powi(u32, 3, 2)));
defer a.deinit();
var as = try a.toString(std.heap.c_allocator, 10, .lower);
defer std.heap.c_allocator.free(as);
@joelonsql
joelonsql / tarjan.sql
Created December 7, 2022 13:05
Tarjan's strongly connected components algorithm implemented in PL/pgSQL
CREATE OR REPLACE FUNCTION tarjan(from_nodes int4range[], to_nodes int[])
RETURNS SETOF int[]
LANGUAGE plpgsql
AS
$$
--
-- https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
--
DECLARE
current_index integer := 0;