Skip to content

Instantly share code, notes, and snippets.

View CAFxX's full-sized avatar

Carlo Alberto Ferraris CAFxX

View GitHub Profile
Verifying that I control the following Nostr public key: npub1j67s9mwffj6ue909esy4ldyhte9xheu5nh2lwed2qycqfdmfjmuq40chpa
@CAFxX
CAFxX / go_wishlist.md
Last active January 24, 2024 00:11
Go wishlist

Language/syntax

Shorthand error definition

Instead of things like var ErrFoo = errors.New("foo") or return fmt.Errorf("foo: %d", n) I would like a shorthand syntax that allows to define a new error type.

Simple error

@CAFxX
CAFxX / bulk_insert.go
Last active December 31, 2022 02:39
SQL bulk insert
package bulkinsert
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
)
@CAFxX
CAFxX / load_data.go
Last active December 29, 2022 06:42
MySQL bulk data loader (via LOAD DATA LOCAL INFILE)
package mysql
import (
"bytes"
"context"
"database/sql"
"encoding/csv"
"errors"
"fmt"
"io"
package xsync
import "sync"
type TryLocker interface {
sync.Locker // Lock(); Unlock()
TryLock() bool
}
// LockAndDo will acquire the lock l and execute fn.
@CAFxX
CAFxX / batch_getter.go
Last active July 27, 2023 01:42
Request batcher
package batchgetter
type Getter[I, T any] interface {
Get(context.Context, []I) ([]T, error)
}
type BatchGetter[I, T any] struct {
parent Getter[I, T]
batchWait time.Duration
@CAFxX
CAFxX / genOptIface.go
Created November 16, 2022 08:53
Generate objects with optional methods (Golang)
package main
import "fmt"
func genOptIface(name string, opt ...string) {
if len(opt) == 0 {
panic("zero opt types")
}
if len(opt) > 10 {
panic("too many opt types")
@CAFxX
CAFxX / lookup.cpp
Created November 11, 2022 06:10
AVX-512 vectorized associative lookup
#include <immintrin.h>
#include <stdint.h>
int getIndexOf(__m512i const *values, int64_t target)
{
__m512i valuesSimd = _mm512_loadu_si512(values);
__m512i targetSplatted = _mm512_set1_epi64(target);
__mmask8 equalMask = _mm512_cmpeq_epi64_mask(valuesSimd, targetSplatted);
uint32_t equalMaskInt = _cvtmask8_u32(equalMask);
int index = _tzcnt_u32(equalMaskInt);
@CAFxX
CAFxX / utf8.rl
Last active May 4, 2022 05:16
Ragel machine definition for valid UTF-8 encodings
%%{
utf8 =
( 0..127 | 192..223 128..191 | 224..239 128..191 128..191 | 240..247 128..191 128..191 128..191 )
- ( 244 144..191 any any | 245..247 any any any ) # over U+10FFFF
- ( 0xC0..0xC1 any ) # overlong 2-byte encodings
- ( 224 128..159 any ) # overlong 3-byte encodings
- ( 240 128..143 any any ) # overlong 4-byte encodings
- ( 237 160..191 any ) # invalid utf-16 surrogate pairs
;
main := utf8* ;
@CAFxX
CAFxX / fastdiv.cpp
Created May 2, 2022 09:04
libdivide runtime cache
// libdivide.h - Optimized integer division
// https://libdivide.com
//
// Copyright (C) 2010 - 2021 ridiculous_fish, <[email protected]>
// Copyright (C) 2016 - 2021 Kim Walisch, <[email protected]>
//
// libdivide is dual-licensed under the Boost or zlib licenses.
// You may use libdivide under the terms of either of these.
// See LICENSE.txt for more details.