Skip to content

Instantly share code, notes, and snippets.

@KirillLykov
KirillLykov / system-performance.md
Last active September 18, 2023 15:02
Systems performance book by B. Gregg

Summary for Systems Performance book by Brendan Gregg

Book it sel on o'reilly

OS-related things

  • Kernel -- software that manages the system by providing access to hardware and other resources (memory, network stack, scheduling cpu, etc). Runs in priveleged CPU mode -- kernel mode. It uses kernel-space stack.
  • Process -- an OS abstraction for running programs. Runs in user mode with access to kernel mode via system calls. It consists of: memory address space, file descriptors, thread stacks, registers. Process uses user-space stacks. For example, syscalls use a s kernel exception stack associated with each thread.
@KirillLykov
KirillLykov / find-shortest-palindrome.md
Last active August 24, 2023 08:52
Find shortest palindrome which can be obtained by mutating input string by adding elements to the front

Solution for https://leetcode.com/problems/shortest-palindrome/submissions/

everyone is writing KMP and Manacher, I would just write a simple hashing algorithm:

    const int p = 31;
    const int M = 1e9 + 7; // I prefer always use mod prime even if we don't need to 
	// find a reverse element. Just for consistancy, it is faster to write one standard way
	// every time instead of creating something ad hoc
    string shortestPalindrome(string s) {
        const int n = s.size();
@KirillLykov
KirillLykov / how-to-use-another-linker-with-rust.md
Created May 24, 2023 14:52
How to set up a linker with rust

In order to speed up your linking stage, you can use a linker that takes advantage of multiple CPU cores.

Such linkers are lld and mold.

mold is considered more cutting edge, but it works for me.

Just add

[build]
rustflags = ["-C", "link-arg=-fuse-ld=mold"]
use criterion::{criterion_group, criterion_main, Criterion, BenchmarkId};
use itertools::Itertools;
use rand::Rng;
type T = u64;
fn sum_1(data: &Vec<T>) -> (T, T, T)
{
let mut sum: (T, T, T) = (T::default(), T::default(), T::default());
for (x,y,z) in data.iter().tuples() {
sum.0 += x;

Kitty

  1. Search in kitty text ctrl + shift + h + /

How to disable fn on keyboard

@KirillLykov
KirillLykov / .tmux.conf
Last active March 1, 2022 09:58
My TMUX config
# remap prefix to Control + q
set -g prefix C-q
unbind C-b
bind C-q send-prefix
# force a reload of the config file
unbind r
bind r source-file ~/.tmux.conf
# quick pane cycling
@KirillLykov
KirillLykov / pre-hook.md
Last active February 22, 2022 13:36
How to set up pre-commit hook

It works for rust and for C++. The difference is that for rust you need to specify rustfmt and for C++ clang-format -i.

  1. Add file pre-commit to the folder .githooks in your repo with the following text:
#!/bin/bash

exe=$(which rustfmt)

if [ -n "$exe" ]
@KirillLykov
KirillLykov / interval-map.md
Created August 15, 2021 09:11
Interval maps -- cheap alternative to segment tree

Given a data stream input of non-negative integers a1, a2, ..., an, summarize the numbers seen so far as a list of disjoint intervals.

The basic idea is to use map : start -> end where end is not inclusive. Invariant is that this map is always disjoint.

lc

class SummaryRanges {
    map<int, int> m; // start to end, [x,y)
@KirillLykov
KirillLykov / cf_1165_E.md
Last active November 13, 2021 21:48
Problem about minimizing sum over all the subarrays

codeforces

Quite interesing problem. We need to minize function of sum_l sum_r f(l,r), where f(l,r) = sum_i ai * bi, i<-[l,r]. So first of all forget about the fact we have two vectors, try to rewrite sum of all the sum of subarrys in one sum. Should have a form sum_i ai * coef(i, n).

How many subarrays are in the array of length n? The same is how many ordered pairs <i,j>? For each i there (n - i) subarrays starting at this i. The total number is S(n) = sum_i (n - i) = n^2 - n(n-1)/2 = n(n+1)/2.

@KirillLykov
KirillLykov / lc_move_box.cpp
Last active September 19, 2021 07:20
Leetcode Move A Box To Target Location
// further readings: http://forskning.diku.dk/PATH05/GoldbergSlides.pdf
// problem itself https://leetcode.com/problems/minimum-moves-to-move-a-box-to-their-target-location/
class Solution {
public:
using State = tuple<int,int,int,int>; // xbox, ybox, xme, yme
static constexpr int x[] = {1,-1,0,0};
static constexpr int y[] = {0,0, 1, -1};
bool impossible(vector<vector<char>>& grid, const pair<int,int>& pos) {
if (pos.first < 0 || pos.first >= grid.size() ||