Do modern x86-64 CPUs speculate on the branch destination? [duplicate] - performance

This question already has answers here:
branch prediction on a function pointer
(3 answers)
How does Branch Prediction affect performance in R?
(1 answer)
How has CPU architecture evolution affected virtual function call performance?
(1 answer)
Closed 2 years ago.
Given the following code:
func_pointer functions[] = { &func1, &func2, ..., &funcn }
void dispatch(int i) {
functions[i]();
}
The address of the destination is not known in the compile time. Can the CPU speculate the destination of the call in case function dispatch() is called several times with the same value for parameter i?

Related

How can I get built-in operator as a function? [duplicate]

This question already has answers here:
How to Use Infix (Comparison) operators as parameters in Go
(1 answer)
How to assign an operator to a variable in golang?
(2 answers)
Closed 9 months ago.
The goal
How can I get a built-in operator as a function?
I want to take advantage of functional programming.
I have some files with vectorizing functions (eg. Remap[inT any](sl []inT, f func(inT) inT)), which take a scalar function to apply on every element of one or two slices.
I know I could:
func eq(a, b int)bool{
return a==b
}
and hope that after inlining there'll be no overhead.
But I prefer a short, performant & consistent way, if exists.
In Python 3 I would:
int.__eq__
In Rust I would:
i32::eq
In C++ I would:
#include <functional>
/*code here*/
std::equal_to<int>()
How would you achieve this in Go?
How can I get built-in operator as a function? Any functional package in Go?
You cannot.

Why do unconditional jumps take up BTB space? [duplicate]

This question already has answers here:
Why are Branch Target Buffers needed for non register jump instructions?
(1 answer)
What branch misprediction does the Branch Target Buffer detect?
(2 answers)
Slow jmp-instruction
(1 answer)
Which instructions can produce a branch misprediction on x86 CPUs?
(1 answer)
Closed 1 year ago.
https://blog.cloudflare.com/branch-predictor/ contains an excellent analysis of the performance of branches on modern hardware.
One thing that surprised me was the finding that unconditional jumps take up space in the branch target buffer. Why?
Conditional branches require use of the BTB because at the time when the CPU has just decoded the branch instruction and wants to fetch the next one, it does not yet know the value of the condition. But for unconditional jumps, there is no condition to know the value of. There is an offset that would need to be added to the IP where the jump instruction was found, but that is a constant in the instruction; it seems to me that you already have it by the time you have the opcode. What am I missing?

Increasing time slices for a particular process via implementing a system call in xv6 [duplicate]

This question already has answers here:
How to modify process preemption policies (like RR time-slices) in XV6?
(2 answers)
Closed 2 years ago.
I am trying to implement a system call in xv6 OS Increase_time( int n) which when executed will increase the timeslice of a program that calls it by n times. The default xv6 scheduler uses a simple FCFS and RR policies with each process having the same time slice. My implementation of Increase_time() will allow different processes to have different amount of time slices.
Can you please tell me a way how I can work around this?
I know how to add a system call in xv6. I just need an idea as to how I can code my system call and what files to change in xv6.
It seems to me that you are asking this question.
In short : don't change amount of time in a slice according to the process, rather change number of timeslices received by it (refer the linked post).

Difference between golang new() vs struct literal, especially in memory allocation [duplicate]

This question already has answers here:
Is there a difference between new() and "regular" allocation?
(3 answers)
Difference in Go between allocating memory by new(Type) and &Type{}
(1 answer)
Why would I make() or new()?
(10 answers)
What's the difference between these three ways to create DIY stucts? [duplicate]
(1 answer)
Closed 5 years ago.
Say I have a struct:
type foo struct {
}
Are there any difference between
f := &foo{}
and
f := new(foo)
in terms of the machine code it gets compiled into, or it's just merely a syntax difference?
A more specific question:
As for struct literal (&foo{}), the memory can be allocated in stack or heap, depends on the escape analysis.
However for new(foo), I am not quite sure:
calloc() in c is similar to new() in golang from my understanding. But calloc() always allocate on heap. I am wondering if new() always allocate in heap?
The golang spec (https://golang.org/ref/spec#Allocation) only mentioned:
The built-in function new takes a type T, allocates storage for a
variable of that type at run time, and returns a value of type *T
pointing to it. The variable is initialized as described in the
section on initial values.
It does not say where new() will allocate the memory. So does new() always allocate on heap, or it can also be allocated in stack as well --- Exactly same as struct literal in memory allocation?

Lua math.random? [duplicate]

This question already has answers here:
Generating uniform random numbers in Lua
(4 answers)
Closed 8 years ago.
I've been having trouble generating a random number for a while now using Lua. I tried starting the script with math.randomseed(os.time()) and I was still getting the same results. How would I get the script below to generate a new random number every time I run the script?
function rand()
local x = math.random(1, #Questions) --Pick a random question from a table
return x
end
This is a well known problem. Just call math.random once or twice before using the result in your program.

Resources