Sep. 12th, 2019

thedeemon: (Default)
fn helper<'a, 'b, T>(_: &'a &'b (), v: &'b T) -> &'a T { v }

/// Turn any `&T` into a `&'static T`. May introduce undefined behavior.
pub fn make_static<'a, T>(input: &'a T) -> &'static T {
    let f: fn(_, &'a T) -> &'static T = helper;
    f(&&(), input)
}

(отсюда)
Не ожидал, что так просто. Безо всякого unsafe берем и превращаем "одолженную" ссылку на временное значение в "вечную", с соответствующими use-after-free последствиями. А просто дырка в borrow checker'e, и давно уже, все никак не залатают.

RC

Sep. 12th, 2019 12:26 pm
thedeemon: (Default)
В интернетах обсуждают очередное сравнение скорости работы разных языков, на этот раз на примере user mode сетевых драйверов. С непреодолим как скорость света. Раст отстает, то ли из-за bounds checks, то ли из-за другого кодогенератора (LLVM vs. GCC). Неожиданно хорошо выступил C#, там есть value типы и слайсы/спаны, можно снизить нагрузку на GC до нуля практически, есть unsafe блоки и указатели. Близко к нему Go. Java отстала. Окамл получился быстрее Хаскеля. Но самый позорный результат из компилируемых языков - у Swift'a. И ведь не первый раз я вижу, что он так тормозит. Комментарии из их статьи:

Swift increments a reference counter for each object passed into a function and decreases it when leaving the function. This is done for every single packet as they are wrapped in Swift-native wrappers for bounds checks. There is no good way to disable this behavior for the wrapper objects while maintaining an idiomatic API for applications using the driver. A total of 76% of the CPU time is spent incrementing and decrementing reference counters. This is the only language runtime evaluated here that incurs a large cost even for objects that are never free’d.

Вот вам и "RC быстрее GC".

Занятное про Хаскель:
Compiler (GHC 8.4.3) optimizations seem to do more harm than good in this workload. Increasing the optimization level in the default GHC backend from O1 to O2 reduces throughput by 11%. The data in the graph is based on the LLVM backend which is 3.5% faster than the default backend at O1. Enabling the threaded runtime in GHC decreases performance by 8% and causes the driver to lose packets even at loads below 1 Mpps due to regular GC pauses of several milliseconds.

Profile

thedeemon: (Default)
Dmitry Popov

May 2025

S M T W T F S
    123
45678910
11 121314151617
18192021222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 18th, 2025 07:31 am
Powered by Dreamwidth Studios