# Do C++ performance optimisations apply to Julia?

**URL:** <https://discourse.julialang.org/t/do-c-performance-optimisations-apply-to-julia/82567>\
**Category:** Performance\
**Created:** [June 10, 2022, 2:19pm UTC](https://discourse.julialang.org/t/do-c-performance-optimisations-apply-to-julia/82567 "2022-06-10T14:19:35Z")\
**Posts on this page:** 1\
**Showing post:** 29

<div class="post-metadata">

**Author:** ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)\
**Post date:** [June 25, 2022, 6:07pm UTC](https://discourse.julialang.org/t/do-c-performance-optimisations-apply-to-julia/82567/29 "2022-06-25T18:07:00Z")

</div>

Can you post an example of the code?

I saw this example from [this old thread](https://discourse.julialang.org/t/compiling-to-branch-table/16599/7) which I have been able to recreate:

```julia
const CODES = (2, 3, 5, 7, 11)
const RCODES = reverse(CODES)

f(x) = 0
f(x, y, ys...) = x == y ? 1 + length(ys) : f(x, ys...)
findcode3(x) = f(x, RCODES...)

@code_llvm findcode3(3)

```

Additionally, I can edit the code to get _close to_ what I wanted originally as we can see that there’s a table lookup happening in the LLVM:

```julia
const RETURNVALUES = (0, 1, 5, 10, 11, 13)
const RRETURNVALUES = reverse(RETURNVALUES)

h(x) = RETURNVALUES[0]
h(x, y, ys...) = x == y ? RRETURNVALUES[length(ys)+1] : h(x, ys...)
hindcode3(x) = h(x, RCODES...)

hindcode3(3)

@code_llvm hindcode3(3)

```

```julia
; Function Attrs: uwtable
define i64 @julia_hindcode3_1356(i64 signext %0) #0 {
top:
; ┌ @ c:\Users\jmmsm\Documents\GitHub\speedy_julia\examples\ss_try2.jl:23 within `h`
   %switch.tableidx = add i64 %0, -2
   %1 = icmp ult i64 %switch.tableidx, 10
   br i1 %1, label %switch.hole_check, label %L16

L16: ; preds = %switch.hole_check, %top
; │ @ c:\Users\jmmsm\Documents\GitHub\speedy_julia\examples\ss_try2.jl:23 within `h` @ c:\Users\jmmsm\Documents\GitHub\speedy_julia\examples\ss_try2.jl:23 @ c:\Users\jmmsm\Documents\GitHub\speedy_julia\examples\ss_try2.jl:23 @ c:\Users\jmmsm\Documents\GitHub\speedy_julia\examples\ss_try2.jl:23 @ c:\Users\jmmsm\Documents\GitHub\speedy_julia\examples\ss_try2.jl:23
   %2 = call nonnull {}* @j_h_1358(i64 signext %0) #0
   call void @llvm.trap()
   unreachable

switch.hole_check: ; preds = %top
; │ @ c:\Users\jmmsm\Documents\GitHub\speedy_julia\examples\ss_try2.jl:23 within `h`
   %switch.maskindex = trunc i64 %switch.tableidx to i16
   %switch.shifted = lshr i16 555, %switch.maskindex
   %3 = and i16 %switch.shifted, 1
   %switch.lobit.not = icmp eq i16 %3, 0
   br i1 %switch.lobit.not, label %L16, label %switch.lookup

switch.lookup: ; preds = %switch.hole_check
   %switch.gep = getelementptr inbounds [10 x i64], [10 x i64]* @switch.table.julia_hindcode3_1356, i64 0, i64 %switch.tableidx
   %switch.load = load i64, i64* %switch.gep, align 8
   ret i64 %switch.load
; └
}

```

---

_[View the full topic](https://discourse.julialang.org/t/do-c-performance-optimisations-apply-to-julia/82567)._
