Skip to content

Instantly share code, notes, and snippets.

View bagbag's full-sized avatar

Patrick Hein bagbag

View GitHub Profile
@bagbag
bagbag / optimization.md
Created May 12, 2025 21:53
Rust compiler optimization
  1. Dead Code Elimination (DCE) Isn't Perfect (or happens late):

    • Ideal Scenario: You'd expect the compiler's DCE pass to recognize that the Deserialize implementations generated by the derive macro are never called and completely eliminate them from the final binary anyway. If DCE worked perfectly and early, removing the derive manually shouldn't change the size much, as the code was already being discarded.
    • Reality: The analysis for DCE, especially across crates and with complex features like generics and macros (which serde uses heavily), can be intricate. Sometimes, code might appear reachable during earlier compilation stages or might have subtle linkages that prevent easy removal until LTO.
  2. The Butterfly Effect of Optimization Heuristics:

    • Compilers use complex heuristics to decide how to optimize code: when to inline functions, how to allocate registers, how to arrange code blocks for cache efficiency, etc.
  • Removing the #[derive(Deserialize)] cha
@bagbag
bagbag / display.rs
Created May 5, 2025 18:07
mipidsi async usage
use core::{
fmt::{self, Write},
str::FromStr,
};
use embassy_embedded_hal::shared_bus::asynch::spi::SpiDevice;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
use embassy_time::{Delay, Duration, Instant};
use embedded_graphics::{
Drawable,
mono_font::{MonoFont, MonoTextStyle, iso_8859_1::FONT_10X20},
@bagbag
bagbag / trace.rs
Created April 3, 2025 12:51
embassy trace
use core::sync::atomic::{AtomicU32, Ordering};
use defmt::trace;
use embassy_sync::{
blocking_mutex::{CriticalSectionMutex, raw::CriticalSectionRawMutex},
signal::Signal,
};
use embassy_time::{Duration, Instant};
use heapless::Vec;
// type PendingTasksMap = FnvIndexMap<u32, TaskInstant, 16>;
@bagbag
bagbag / stepper_decoder.rs
Created March 26, 2025 16:24
stepper decoder
use embassy_futures::select::{Either, select, select_array};
use embassy_time::Timer;
use esp_hal::{
gpio::{Input, InputConfig, InputPin, Pull},
peripheral::Peripheral,
};
const STATE_A: u8 = 0b1000;
const STATE_AB: u8 = 0b1100;
const STATE_B: u8 = 0b0100;
@bagbag
bagbag / cloudSettings
Last active October 8, 2021 09:58
Visual Studio Code Settings Sync Gist
{"lastUpload":"2021-10-08T09:58:29.031Z","extensionVersion":"v3.4.3"}
(<any>Symbol).asyncIterator = Symbol.asyncIterator || Symbol.for("Symbol.asyncIterator");
const sleep = async (ms: number) => new Promise((resolve, reject) => setTimeout(() => resolve(), ms));
class TestClass {
private awaitable = Promise.resolve([1, 2, 3, 4, 5]);
async *count(count: number): AsyncIterableIterator<number> {
for (let i = 0; i < count; i++) {
yield* (async function* () {
await sleep(100);
@bagbag
bagbag / index.html
Last active October 29, 2023 10:20
MediathekViewWeb API Samplecode
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>MediathekViewWeb API</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>