• úvod
  • témata
  • události
  • tržiště
  • diskuze
  • nástěnka
  • přihlásit
    registrace
    ztracené heslo?
    SPIRALIRust - Programovací jazyk
    RESURRECTION
    RESURRECTION --- ---
    RESURRECTION: A jenom doplnim, ze multithreaded varianta nepouzije `Rc`, ale `Arc` (ten lock ma read() a write() misto borrow() a borrow_mut()), ale jinak to bude stejny.
    RESURRECTION
    RESURRECTION --- ---
    BONEFLUTE: Hlavni problem je implementace `App`, nevidim moc duvod pro `self: &Rc`, proc ne `&self`? `Rc` je typicky to co pouziva klientsky/uzivatelsky kod, ne implementace. `Rc` je immutable single-threaded reference counted pointer. `RefCell` je pak runtime borrow checker. `Rc` je zero abstraciton, `RefCell` ma overhead (dela borrow checking v runtimeu). Pro to co chces mas vice moznosti:

    1. Uzivatel si bude resit borrowing v compile tam. Nepotrebujes ani `Rc`, proste signatury v impl budou:

    pub fn new(on_startup: ...) -> Self { /* */}
    pub fn run(&self) { /* */}
    pub fn add_something(&mut self, x: ...) { /* */ }

    2. Uzivatel si bude resit ten borrowing v runtimeu:

    // stejne signatury jako v 1.
    let app = Rc::new(RefCell::new(App::new()));
    app.borrow().run();
    app.borrow_mut().add_something(x);

    3. Hidnes implementaci a ten runtime borrowing budes delat sam stejne jako ve 2 (hledej "interior mutability"), kde budou trochu jine ty signatiry:

    #[derive(Clone)]
    struct App {
        callback: Rc<RefCell<Callback>>
    }
    
    pub fn new(on_startup: ...) -> Self { Self { callback: Rc::new(RefCell::new(on_startup)) } }
    pub fn run(&self) { self.callback.borrow()... } // predpoklad je, ze callback je `Fn` nebo obdoba
    pub fn add_something(&self, x: ...) { self.callback.borrow_mut()... } // <-- povsimni si, ze toto taky bere &self a ne `&mut self`

    Samozrejme nejlepsi je 1. Ty ostatni maji runtime overhead (borrow checking pri runtime). Zejmena jelikoz to mas single-threaded, tak neni moc duvod resit reference counting, pokud `App` nekde neskladujes jako member promennou. V kazdem pripade nejlepsi performance (za cenu nejake ergonomie) m a i v takovem pripade nasledujici pattern:

    struct App { pub callback: ... }
    type Callabck = ...;
    
    impl App {
        pub fn run(callback: &Callback)
        pub fn add_something: &mut Callback, x: ...);
    }
    
    fn main() {
        let app = App { callback: ... };
        App::run(&app.callback);
        App::add_something(&mut app.callback, x);
    }

    Tahle varianta presouva ten borrow checking zpet do compile time a je vhodna tam, kde ten ownership neni uplne jasny a kde to chces volat z ruznych mist. Takhle se vlatne pise cecko (minus prave ten borrow checking).
    BONEFLUTE
    BONEFLUTE --- ---
    Ahoj.

    Prosím poraďte, nebo mi vysvětlete, že to nejde.

    V kostce:
    Jde převést
    Rc<App>
    na
    Rc<RefCell<App>>
    ?

    Kontext:
    Mám strukturu App, nějak takto:
    struct App {
        callback: ...
    }
    impl App {
    	pub fn new(on_startup: ...) -> Rc<Self> {}
    	pub fn run(self: &Rc<Self>) {
    		... volání callbacku
    	}
    	pub fn add_something(mut self: &Rc<Self>, x: ...) {
    		let _1: &Rc<ApplicationView> = self;
    		let appview: Rc<RefCell<ApplicationView>> = ???;
    	}
    }
    Následně vytvořím instanci této struktury:

    let app = App::new(move |this| {
    	let _: Rc<RefCell<App>> = this;
    	this.add_something(obj);
    });
    Ta lambda se uloží do vlastnosti struktury a zavolá se následně při zavolání metody run():
    app.run();

    Jak je doufám z kódu vidět, potřebuji převést
    Rc<App>
    na
    Rc<RefCell<App>>
    .

    (GPT mi radí různé způsoby, které žádné nefungují. Jak známo, když problematice nerozumíš, je GPT dost naprd.)

    Je samozřejmě možné, že toto nejde, a budu to muset udělat jinak. Ale rád bych si nechal vynadat a vysvětlit, že to mám dělat jinak a ideálně proč :-)

    Děkuji za nasměrování.
    SPIKE411
    SPIKE411 --- ---
    Where Does the Time Go? Rust's Problem with Slow Compiles - The New Stack
    https://thenewstack.io/where-does-the-time-go-rusts-problem-with-slow-compiles/
    SPIKE411
    SPIKE411 --- ---
    SPIKE411
    SPIKE411 --- ---
    Rust is the latest open source project to face burnout • The Register
    https://www.theregister.com/2024/01/22/rust_project_burnout/
    SATAI
    SATAI --- ---
    Coool

    Welcome - cargo-mutants
    https://mutants.rs/
    SPIRALI
    SPIRALI --- ---
    Budete dnes nekdo na meetupu?
    SPIRALI
    SPIRALI --- ---
    Rust meetup pristi tyden v Praze:
    Rust Meetup Reloaded 2024, Wed, Jan 17, 2024, 6:00 PM | Meetup
    https://www.meetup.com/rust-prague/events/298005196/
    MARASAN
    MARASAN --- ---
    Interview with Senior Rust Developer in 2023
    https://youtu.be/TGfQu0bQTKc?si=E3ML3SwLlCIa9MrG
    SPIKE411
    SPIKE411 --- ---
    Adopting Rust: the missing playbook for managers and CTOs | Mainmatter
    https://mainmatter.com/blog/2023/12/13/rust-adoption-playbook-for-ctos-and-engineering-managers/
    SPIKE411
    SPIKE411 --- ---
    Title Page - The Rust Performance Book
    https://nnethercote.github.io/perf-book/
    SPIRALI
    SPIRALI --- ---
    RESURRECTION: A bude hur. Po tom co se asi primo do stdlib C++ dostane knihovna na linearni algebru tak uz se muze stat asi vsechno... (ale zase jestli se chteji stat legacy jazykem na psani 3D her, tak to asi dava smysl).
    RESURRECTION
    RESURRECTION --- ---
    SPIKE411: Díky za odkazy, jsem to zblbnul v těch tagách. :-) Ještě jeden vtipnější k tomu UB, tentokrát Undefined Behaviour is magic: https://www.youtube.com/watch?v=g7entxbQOCc :D
    LYCO
    LYCO --- ---
    RESURRECTION: To tvrzení že "ukazatele v Cčku mají jednuduchou a intuitivní sémantiku" je taky blbost. Donedávna se nevědělo co je to ukazatel(!) a teprve nedávno se to asi podařilo vyřešit (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2676.pdf). Lidštější vysvětlení jsou třeba https://www.ralfj.de/blog/2018/07/24/pointers-and-bytes.html a https://faultlore.com/blah/fix-rust-pointers/#cheri

    (Neptejte se mě na detaily, sám tomu skoro vůbec nerozumím, ale aspoň vím že nevím.)
    MARASAN
    MARASAN --- ---
    SPIKE411
    SPIKE411 --- ---
    Nebo možná spíš
    https://security.googleblog.com/2022/12/memory-safe-languages-in-android-13.html
    Mobile devices have limited resources and we’re always trying to make better use of them to provide users with a better experience (for example, by optimizing performance, improving battery life, and reducing lag). Using memory unsafe code often means that we have to make tradeoffs between security and performance, such as adding additional sandboxing, sanitizers, runtime mitigations, and hardware protections. Unfortunately, these all negatively impact code size, memory, and performance.

    Using Rust in Android allows us to optimize both security and system health with fewer compromises. For example, with the new UWB stack we were able to save several megabytes of memory and avoid some IPC latency by running it within an existing process. The new DNS-over-HTTP/3 implementation uses fewer threads to perform the same amount of work by using Rust’s async/await feature to process many tasks on a single thread in a safe manner.
    SPIKE411
    SPIKE411 --- ---
    UB asi tohle
    CppCon 2017: Piotr Padlewski “Undefined Behaviour is awesome!”
    https://www.youtube.com/watch?v=ehyHyAIa5so

    https://cpp.mimuw.edu.pl/files/ub.pdf

    Google debunk bude hádám tohle
    Rust fact vs. fiction: 5 Insights from Google's Rust journey in 2022 | Google Open Source Blog
    https://opensource.googleblog.com/2023/06/rust-fact-vs-fiction-5-insights-from-googles-rust-journey-2022.html

    Software in Decline asi tohle
    Software is in Decline - Jonathan Blow
    https://www.youtube.com/watch?v=FeAMiBKi_EM
    MARASAN
    MARASAN --- ---
    RESURRECTION: mi ty tvoje odkazy davaji v Brave Browseru
    <a rel="noopener noreferrer">Google to krásně debunknul</a>
    Nechces prosimte postnout rovnou URLs? Dik.
    Kliknutím sem můžete změnit nastavení reklam