Skip to content

Instantly share code, notes, and snippets.

View vshabanov's full-sized avatar

Vladimir Shabanov vshabanov

View GitHub Profile
@vshabanov
vshabanov / Graph.cpp
Last active November 23, 2025 15:12
Flat purely functional DAG with updateable inputs, lock-free lookups, and a concurrent node construction with a deadlock detector
// g++ -g -fopenmp -O2 -std=gnu++2c -Wall -Wpedantic -Iboost_1_89_0 Graph.cpp && ./a.out
/* [WIP]
TODO
- removing unused nodes?
- ref data use case -- evaluate immediately and only keep results
- exceptions in recipes -- do not keep unused nodes
- recipes that add nodes but do not use them
// g++ -g -O3 -fopenmp -std=c++20 -Iboost_1_89_0 -Wall -Wpedantic hashtable.cpp && perf stat -e branches,branch-misses,bus-cycles,cache-misses,cache-references,cycles,instructions,ref-cycles ./a.out
#include <atomic>
#include <functional>
#include <utility>
#include <string>
#include <vector>
#include <stdexcept>
#include <map>
#include <unordered_map>
#include <bit>
@vshabanov
vshabanov / rankn.c
Last active November 15, 2025 17:05
RankN types support in C interpreter
/* Experiment with a uniform representation of values that can hold
references to C++ objects and be used polymorphically. Examples of
the Rank2 IO monad dictionary and relation extend are provided at
the bottom.
The approach is the same as in dynamically typed languages.
Primitive values are coupled with types at runtime. Functions only
keep their arity to know when to apply. Empty values ([] or
Nothing) are untyped.
@vshabanov
vshabanov / NDP.hs
Last active November 15, 2025 16:51
Experiments with nested data parallelism
{-# LANGUAGE TypeFamilies #-}
import Control.Concurrent
import Control.Concurrent.Async
import Control.Concurrent.Chan
import Control.Concurrent.MVar
import Control.Concurrent.QSem
import Control.Exception qualified as E
import Control.Monad
import Control.Monad.IO.Class
@vshabanov
vshabanov / OneBRC.hs
Last active April 30, 2024 18:46
One billion lines challenge
{-# LANGUAGE GHC2021, LambdaCase, PatternSynonyms, RecordWildCards,
ViewPatterns, OverloadedStrings, GADTs, NoMonoLocalBinds,
UnboxedTuples, MagicHash #-}
{-# OPTIONS_GHC -O2 -fspec-constr-count=100 -fllvm #-}
-- need at least 8 to completely remove all allocations
-- (I don't know what change lead to it)
{-# OPTIONS_GHC -Wall -Wno-gadt-mono-local-binds -Wno-type-defaults #-}
@vshabanov
vshabanov / dnsutils.hs
Created October 2, 2017 12:53
Some utilities to work with dns package
resolveA :: DNSCache -> String -> IO (Either T.Text HostAddress)
resolveA c d =
handle Left $ checkDomain id d $ fmap errorStr $ resolve c (B.pack d)
resolveCachedA :: DNSCache -> String -> IO (Maybe (Either T.Text HostAddress))
resolveCachedA c d =
handle (Just . Left) $ checkDomain Just d $ fmap (fmap errorStr) $ resolveCache c (B.pack d)
handle f a = a `E.catch` handleError f
@vshabanov
vshabanov / list.hs
Created September 3, 2017 23:58
Полнофункциональные гетерогенные списки
{-# LANGUAGE GADTs #-}
class Clickable a where
click :: a -> String
class Renderable a where
render :: a -> String
data Interface a where
Clickable :: Clickable a => Interface a
Renderable :: Renderable a => Interface a
import qualified Data.ByteString.Lazy.Char8 as B
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.Encoding as T
import qualified Data.Aeson as JSON
import Data.Char
import Text.Printf
import Control.Monad
-- wget http://www.unicode.org/Public/9.0.0/ucd/UnicodeData.txt
@vshabanov
vshabanov / gist:5768546
Created June 12, 2013 19:55
Riak Segmentation Fault in eleveldb.so
Core was generated by `/tank/riak-1.3.1/erts-5.9.1/bin/beam.smp -K true -A 64 -W w -- -root /tank/riak'.
Program terminated with signal 11, Segmentation fault.
#0 0x00007f162547fa30 in MurmurHash64A(void const*, int, unsigned int) ()
from /tank/riak-1.3.1/lib/eleveldb-1.3.0/priv/eleveldb.so
(gdb) bt
#0 0x00007f162547fa30 in MurmurHash64A(void const*, int, unsigned int) ()
from /tank/riak-1.3.1/lib/eleveldb-1.3.0/priv/eleveldb.so
#1 0x00007f162547833c in leveldb::(anonymous namespace)::BloomFilterPolicy2::CreateFilter(leveldb::Slice const*, int, std::string*) const ()
from /tank/riak-1.3.1/lib/eleveldb-1.3.0/priv/eleveldb.so
@vshabanov
vshabanov / XMLParser.hs
Created January 10, 2012 08:23
Более шустрый парсер для tagsoup (где-то раз в 200 быстрее)
{-# LANGUAGE OverloadedStrings, BangPatterns, FlexibleInstances #-}
-- | Тупейший (и, надеюсь, быстрый) парсер xml -> теги.
--
-- Выдает где-то от 40МБ/сек (а где-то и 80 и 200)
--
-- Все теги и атрибуты возвращаются в lowercase (строка меняется INPLACE,
-- для уменьшения числа аллокаций и фрагментации)
--
-- После профилирования выяснилось,
-- что TagSoup отжирает 75% памяти и >50% времени. Более того,