1. Список говнокодов пользователя 3.14159265

    Всего: 150

  2. C++ / Говнокод #27619

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    95. 95
    96. 96
    const int sum(int a, int b){
        return a+b;
    }
    
    int a(int x)
    {
        const std::function<int(int)> sum4 = std::bind(sum,_1, 4);    
        return sum4(123);
    }
    
    int b(int x)
    {
        puts("bagor");    
        const std::function<int(int)> sum4 = std::bind(sum,_1, 4);    
        return sum4(123);
    }
    
    int c(int x)
    {
        const std::function<int(int)> sum4 = std::bind(sum,_1, 4);    
        puts("bagor");    
        return sum4(123);
    }
    
    // Функции a и b нормально инлайнятся. Ассемблерный выхлоп:
    
    sum(int, int):                               # @sum(int, int)
            lea     eax, [rdi + rsi]
            ret
    a(int):                                  # @a(int)
            mov     eax, 127
            ret
    b(int):                                  # @b(int)
            push    rax
            mov     edi, offset .L.str
            call    puts
            mov     eax, 127
            pop     rcx
            ret
    
    //А вот int c(int x)
    c(int):                                  # @c(int)
            push    rbx
            sub     rsp, 32
            mov     edi, 16
            call    operator new(unsigned long)
            mov     rbx, rax
            mov     qword ptr [rax], offset sum(int, int)
            mov     dword ptr [rax + 8], 4
            mov     qword ptr [rsp], rax
            mov     qword ptr [rsp + 24], offset std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_invoke(std::_Any_data const&, int&&)
            mov     qword ptr [rsp + 16], offset std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation)
            mov     edi, offset .L.str
            call    puts
            mov     esi, dword ptr [rbx + 8]
            mov     edi, 123
            call    qword ptr [rbx]
            mov     ebx, eax
            mov     rax, qword ptr [rsp + 16]
            test    rax, rax
            je      .LBB3_3
            mov     rdi, rsp
            mov     rsi, rdi
            mov     edx, 3
            call    rax
    .LBB3_3:
            mov     eax, ebx
            add     rsp, 32
            pop     rbx
            ret
            mov     rdi, rax
            call    __clang_call_terminate
            mov     rbx, rax
            mov     rax, qword ptr [rsp + 16]
            test    rax, rax
            je      .LBB3_6
            mov     rdi, rsp
            mov     rsi, rdi
            mov     edx, 3
            call    rax
    .LBB3_6:
            mov     rdi, rbx
            call    _Unwind_Resume@PLT
            mov     rdi, rax
            call    __clang_call_terminate
    __clang_call_terminate:                 # @__clang_call_terminate
            push    rax
            call    __cxa_begin_catch
            call    std::terminate()
    std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_invoke(std::_Any_data const&, int&&): # @std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_invoke(std::_Any_data const&, int&&)
            mov     rax, qword ptr [rdi]
            mov     rcx, qword ptr [rax]
            mov     edi, dword ptr [rsi]
            mov     esi, dword ptr [rax + 8]
            jmp     rcx                             # TAILCALL
    std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation): # @std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation)

    Решил попробовать std::bind.

    https://godbolt.org/z/eW5eT5oj4

    3.14159265, 27 Августа 2021

    Комментарии (86)
  3. JavaScript / Говнокод #27616

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    Steps to reproduce:
    
      var s = "a huge, huge, huge string...";
      s = s.substring(0, 5);
    
    Expected results: s takes five bytes of memory, plus some overhead.
    Actual results: s takes a huge, huge, huge amount of memory.
    Unfortunately, most String functions use substring() or no-ops internally: concatenating with empty string, trim(), slice(), match(), search(), replace() with no match, split(), substr(), substring(), toString(), trim(), valueOf().
    My workaround is:
    
    function unleakString(s) { return (' ' + s).substr(1); }
    
    But it's not satisfying, because it breaks an abstraction and forces me to think about memory allocation.

    https://bugs.chromium.org/p/v8/issues/detail?id=2869

    Status: Assigned (Open)
    Reported on: Sep 3, 2013

    3.14159265, 26 Августа 2021

    Комментарии (49)
  4. Куча / Говнокод #27601

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    fn main() {
        println!("Hello World!");
    }
    
    rustc --version --verbose:
    
    rustc 1.52.1 (9bc8c42bb 2021-05-09)
    binary: rustc
    commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
    commit-date: 2021-05-09
    host: powerpc-unknown-linux-gnu
    release: 1.52.1
    LLVM version: 12.0.0
    
    Error output
    
    rustc ./hello.rs
    Illegal instruction (core dumped)

    https://github.com/rust-lang/rust/issues/85238

    Open: clienthax opened this issue on May 12 · 6 comments

    3.14159265, 22 Августа 2021

    Комментарии (26)
  5. Куча / Говнокод #27576

    +5

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    // And then I replaced the idiomatic Rust code for working with block like
    
        for (dline, (sline0, sline1)) in dst.chunks_mut(dstride).zip(tmp.chunks(TMP_BUF_STRIDE).zip(tmp2.chunks(TMP_BUF_STRIDE))).take(h) {
            for (pix, (&a, &b)) in dline.iter_mut().zip(sline0.iter().zip(sline1.iter())).take(w) {
                *pix = ((u16::from(a) + u16::from(b) + 1) >> 1) as u8;
            }
        }
    
    // with raw pointers:
    
        unsafe {
            let mut src1 = tmp.as_ptr();
            let mut src2 = tmp2.as_ptr();
            let mut dst = dst.as_mut_ptr();
            for _ in 0..h {
                for x in 0..w {
                    let a = *src1.add(x);
                    let b = *src2.add(x);
                    *dst.add(x) = ((u16::from(a) + u16::from(b) + 1) >> 1) as u8;
                }
                dst = dst.add(dstride);
                src1 = src1.add(TMP_BUF_STRIDE);
                src2 = src2.add(TMP_BUF_STRIDE);
            }
        }

    What do you know, the total decoding time for the test clip I used shrank from 6.6 seconds to 4.9 seconds. That’s just three quarters of the original time!

    And here is the problem. In theory if Rust compiler knew that the input satisfies certain parameters i.e. that there’s always enough data
    to perform full block operation in this case, it would be able to optimise code as good as the one I wrote using pointers or even better.
    But unfortunately there is no way to tell the compiler that input slices are large enough to perform the operation required amount of times.
    Even if I added mathematically correct check in the beginning it would not eliminate most of the checks.

    https://codecs.multimedia.cx/2021/05/missing-optimisation-opportunity-in-rust/

    3.14159265, 17 Августа 2021

    Комментарии (125)
  6. Куча / Говнокод #27570

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    C	5.2s 	gcc test.c
    C++	1m 25s 	g++ test.cpp
    Zig	10.1s 	zig build-exe test.zig
    Nim	45s 	nim c test.nim
    Rust	Stopped after 30 minutes	rustc test.rs
    Swift	Stopped after 30 minutes 	swiftc test.swift
    D	Segfault after 6 minutes 	dmd test.d
    
    Rust and Swift took too long to compile 400k lines, so I tried smaller numbers: 
    
    # lines	Rust	Swift 	D
    2k	3.4s 	0.8s
    4k	9.0s 	1.0s
    8k	30.8s 	2.3s
    20k	3m 52s 	11.8s 	4.7s
    100k	- 	5m 57s 	segfault

    https://vlang.io/compilation_speed

    3.14159265, 15 Августа 2021

    Комментарии (40)
  7. Java / Говнокод #27041

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    final class Point {
        public final int x;
        public final int y;
    
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        // state-based implementations of equals, hashCode, toString
        // nothing else
    
    }

    is "just" the data (x, y). Its representation is (x, y), its construction protocol accepts an (x, y) pair and stores it directly into the representation,
    it provides unmediated access to that representation, and derives the core Object methods directly from that representation.
    And in the middle, there are grey areas where we're going to have to draw a line.

    Other OO languages have explored compact syntactic forms for modeling data-oriented classes: case classes in Scala, data classes in Kotlin, and record classes in C#.
    These have in common that some or all of the state of a class can be described directly in the class header -- though they vary in their semantics
    (such as constraints on the mutability or accessibility of fields, extensibility of the class, and other restrictions.)

    Committing in the class declaration to at least part of the relationship between state and interface enables suitable defaults to be derived for many common members.
    All of these mechanisms (let's call them "data classes") seek to bring us closer to the goal of being able to define Point as something like:

    record Point(int x, int y) { }

    [u]https://openjdk.java.net/jeps/359
    https://cr.openjdk.java.net/~briangoetz/amber/datum.html[u]

    3.14159265, 19 Октября 2020

    Комментарии (49)
  8. Куча / Говнокод #26984

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    pub struct Vec { x: u32, y: u32, z: u32, }
    
    pub extern "C" fn sum_c(a: &Vec, b: &Vec) -> Vec {
        return Vec {x: a.x + b.x, y: a.y + b.y, z: a.z + b.z };
    }
    
    pub fn sum_rust(a: &Vec, b: &Vec) -> Vec {
        return Vec {x: a.x + b.x, y: a.y + b.y, z: a.z + b.z };
    }
    
    Выхлоп:
    
    example::sum_c:
            mov     eax, dword ptr [rsi]
            add     eax, dword ptr [rdi]
            mov     ecx, dword ptr [rsi + 4]
            add     ecx, dword ptr [rdi + 4]
            mov     edx, dword ptr [rsi + 8]
            add     edx, dword ptr [rdi + 8]
            shl     rcx, 32
            or      rax, rcx
            ret
    
    example::sum_rust:
            mov     ecx, dword ptr [rdx]
            mov     r8d, dword ptr [rdx + 4]
            add     ecx, dword ptr [rsi]
            add     r8d, dword ptr [rsi + 4]
            mov     edx, dword ptr [rdx + 8]
            add     edx, dword ptr [rsi + 8]
            mov     rax, rdi
            mov     dword ptr [rdi], ecx
            mov     dword ptr [rdi + 4], r8d
            mov     dword ptr [rdi + 8], edx
            ret

    «Дак йаже как Сишка!», «Даёшь пuтушатню в Ядро!»
    https://godbolt.org/z/Tcnz75
    rustc 1.46 (latest)

    3.14159265, 28 Сентября 2020

    Комментарии (138)
  9. Куча / Говнокод #26981

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    Function/method calling convention. Here’s a simple example:
    
    struct Foo { a: i32 }
    impl Foo { fn bar(&mut self, val: i32) { self.a = val + 42; } }
    fn main() {
      let mut foo = Foo { a: 0 };
      foo.bar(foo.a);
    }
    
    For now this won’t compile because of the borrowing but shouldn’t the compiler be smart enough to create a copy of foo.a before call?
    I’m not sure but IIRC current implementation first mutably borrows object for the call and only then tries to borrow the arguments.
    Is it really so and if yes, why?
    Update: I’m told that newer versions of the compiler handle it just fine but the question still stands (was it just a compiler problem or the call definition has been changed?).
    
    The other thing is the old C caveat of function arguments evaluation. Here’s a simple example:
    
    let mut iter = “abc”.chars();
    foo(iter.next().unwrap(), iter.next().unwrap(), iter.next().unwrap());
    
    So would it be foo('a','b','c') or foo('c','b','a') call. In C it’s undefined because it depends on how arguments are passed on the current platform 
    (consider yourself lucky if you don’t remember __pascal or __stdcall). 
    
    In Rust it’s undefined because there’s no formal specification to tell you even that much. 
    And it would be even worse if you consider that you may use the same source for indexing the caller object like 
    handler[iter.next().unwrap() as usize].process(iter.next().unwrap()); in some theoretical bytecode handler 
    (of course it’s a horrible way to write code and you should use named temporary variables but it should illustrate the problem).

    https://codecs.multimedia.cx/2020/09/why-rust-is-not-a-mature-programming-language/

    3.14159265, 27 Сентября 2020

    Комментарии (62)
  10. Куча / Говнокод #26961

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    Top 5 most loved languages:
    
    Rust: 86.1%
    TypeScript: 67.1%
    Python: 66.7%
    Kotlin: 62.9%
    Go: 62.3%
    
    Top 5 most dreaded languages:
    
    VBA: 80.4%
    Objective-C: 76.6%
    Perl: 71.4%
    Assembly: 70.6%
    C: 66.9%
    
    Top 5 most wanted languages:
    
    Python: 30.0%
    JavaScript: 18.5%
    Go: 17.9%
    TypeScript: 17.0%
    Rust: 14.6%

    https://insights.stackoverflow.com/survey/2020#technology-most-loved-dreaded-and-wanted-languages-wanted

    Миллионы мух не могут ошибаться.

    3.14159265, 18 Сентября 2020

    Комментарии (346)
  11. Си / Говнокод #26951

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    #include <stdio.h>
    #include <csptr/smart_ptr.h>
    #include <csptr/array.h>
    
    void print_int(void *ptr, void *meta) {
        (void) meta;
        // ptr points to the current element
        // meta points to the array metadata (global to the array), if any.
        printf("%d\n", *(int*) ptr);
    }
    
    int main(void) {
        // Destructors for array types are run on every element of the
        // array before destruction.
        smart int *ints = unique_ptr(int[5], {5, 4, 3, 2, 1}, print_int);
        // ints == {5, 4, 3, 2, 1}
    
        // Smart arrays are length-aware
        for (size_t i = 0; i < array_length(ints); ++i) {
            ints[i] = i + 1;
        }
        // ints == {1, 2, 3, 4, 5}
    
        return 0;
    }

    Allocating a smart array and printing its contents before destruction.

    C Smart Pointers

    What this is
    This project is an attempt to bring smart pointer constructs to the (GNU) C programming language.

    Features: unique_ptr, shared_ptr macros, and smart type attribute

    https://github.com/Snaipe/libcsptr

    3.14159265, 15 Сентября 2020

    Комментарии (1)