Skip to content

Instantly share code, notes, and snippets.

View ttsugriy's full-sized avatar

Taras Tsugrii ttsugriy

  • Meta
  • Seattle, WA
View GitHub Profile
@ttsugriy
ttsugriy / mod.rs
Created July 30, 2023 04:09
binary_search_slice original implementation
/// Uses a sorted slice `data: &[E]` as a kind of "multi-map". The
/// `key_fn` extracts a key of type `K` from the data, and this
/// function finds the range of elements that match the key. `data`
/// must have been sorted as if by a call to `sort_by_key` for this to
/// work.
pub fn binary_search_slice<'d, E, K>(data: &'d [E], key_fn: impl Fn(&E) -> K, key: &K) -> &'d [E]
where
K: Ord,
{
let Ok(mid) = data.binary_search_by_key(key, &key_fn) else {
use criterion::{criterion_group, criterion_main, Criterion};
/// Uses a sorted slice `data: &[E]` as a kind of "multi-map". The
/// `key_fn` extracts a key of type `K` from the data, and this
/// function finds the range of elements that match the key. `data`
/// must have been sorted as if by a call to `sort_by_key` for this to
/// work.
pub fn binary_search_slice<'d, E, K>(data: &'d [E], key_fn: impl Fn(&E) -> K, key: &K) -> &'d [E]
where
K: Ord,
diff --git a/compiler/rustc_data_structures/src/binary_search_util/mod.rs b/compiler/rustc_data_structures/src/binary_search_util/mod.rs
index d40172a2e2f..c6e3354025a 100644
--- a/compiler/rustc_data_structures/src/binary_search_util/mod.rs
+++ b/compiler/rustc_data_structures/src/binary_search_util/mod.rs
@@ -10,59 +10,22 @@ pub fn binary_search_slice<'d, E, K>(data: &'d [E], key_fn: impl Fn(&E) -> K, ke
where
K: Ord,
{
- let Ok(mid) = data.binary_search_by_key(key, &key_fn) else {
+ let size = data.len();
@ttsugriy
ttsugriy / midpoint_bench.cpp
Created July 22, 2023 22:25
midpoint benchmark
#include <numeric>
int midpointb(int l, int r) {
return (l + r) >> 1;
}
int midpointw(int l, int r) {
return l + (r - l) / 2;
}
@ttsugriy
ttsugriy / midpoints.cpp
Created July 22, 2023 22:11
different midpoints in C++
int midpointb(int l, int r) {
return (l + r) >> 1;
}
int midpointw(int l, int r) {
return l + (r - l) / 2;
}
int midpoint(int l, int r) {
return static_cast<unsigned int>(l + r) >> 1;
@ttsugriy
ttsugriy / fadd_fast_bench.rs
Created July 9, 2023 01:49
fadd_fast benchmark
#![feature(core_intrinsics)]
use std::intrinsics::fadd_fast;
use criterion::{criterion_group, criterion_main, Criterion};
const XS: &'static [f32] = &[3.14; 1000];
fn slow_sum(xs: &[f32]) -> f32 {
xs.iter().sum()
@ttsugriy
ttsugriy / std_func_vs_y_comb.cpp
Created November 25, 2022 01:55
std::function vs y combinator benchmark
#include <functional>
int fact_std_func(int n) {
std::function<int(int)> fact = [&](auto n) {
if (n == 1) return 1;
return n * fact(n-1);
};
return fact(n);
}
@ttsugriy
ttsugriy / go_embed_bench.go
Created July 10, 2021 19:36
Benchmarking go:embed directive
package main
import (
"embed"
"io/ioutil"
"testing"
)
//go:embed citadel_of_the_star_lords.txt
var f embed.FS
@ttsugriy
ttsugriy / benchmark_transform_reduce.cpp
Created June 27, 2021 20:43
Benchmark Transform Reduce algo
#include <numeric>
#include <vector>
#include <execution>
const int N = 1000000;
std::vector<double> xs(N, 1.1);
std::vector<double> ys(N, 1.1);
double inner_product_1(const std::vector<double>& xs, const std::vector<double>& ys) {
@ttsugriy
ttsugriy / padding_bench.cpp
Created June 26, 2021 19:43
C++ benchmark for record padding.
#include <vector>
using namespace std;
struct Record1 {
int a;
char c;
double d;
bool e;
};