If I call one lambda function with one request, but within that function, there are three calls made to different functions then would this count as 4 calls or is it just one call since its based on one request?
So if the count is 4 then (from economic stand point) wouldnt it be better if one writes one long function instead of many small functions, despite it being ill advised from design pattern stand point?
Every invocation of a Lambda function counts. It doesn't matter whether you call it from the console, from a CLI, from an event source, or from another Lambda function it will count as invocation.
Personally, I would focus on writing my Lambda functions in a way that makes sense and allowed me to use them effectively. If you find your costs are a factor later, you can always adjust then.
Related
This is a problem I doubt has a solution meeting all of its constraints, but I may be - I hope I am - wrong.
Within a C++ - currently C++11 - program I have some code that we can assume to be a function call. Based on inputs before that call, that function might run in a time that is milliseconds, or could be up to and beyond any reasonable amount of time were it to be allowed to complete.
I would like to set a time limit on the length of time that function is allowed to run, after which it is stopped. In an ideal word it would be as if that function threw a specified exception, but any form of stopping, up to and including calling terminate() is acceptable.
It's not practical to modify the function, for various reasons, it has to be external to the function. Any solution allowed, as long as the features it uses are all standard C++ - as close to C++11 as possible but if needing to go later, OK. If the solution involves threads - that is not a requirement - those also would need to be cleaned up - including quickly if the function evaluates quickly (but need not be exactly at its time) and the function would need to stay in the main thread.
Since Lambda charges me per invocation, wouldn't it be more cost efficient if I define all functions within a single file?
Or do they charge per function invocations regardless of the .py files?
Lambda charges are based on the number of invocations of your function and the time it takes for your function to run.
Whenever your lambda function is called, it will be charged for the invocation and its duration.
Case 1 (Single purpose design):
You want to implement 3 functions,
For example - ADD(),SUBTRACT(),DIVIDE()
you created a lambda function for each function.
Whenever any of your functions are called,you will be charged for that invocation.
If you called ADD()[Lambda function] then SUBTRACT()[Lambda function] then DIVIDE()[Lambda function], you will be charged once for each lambda function means total of 3.
Case 2 (Monolithic design):
You want to implement 3 functions, you wrote all three functions into different .py files but into a single Lambda function because AWS Lambda allows only single handler per Lambda function and these functions will be invoked according to the condition.
Whenever you are going to call any of the function, same Lambda function will be invoked everytime.
If you call ADD() function then SUBTRACT() function then DIVIDE() function, you will be charged for same Lambda function call every time means total of 3.
So, whether you write a separate lambda function for each function or single Lambda function for all functions, you will be charged for the number of calls and duration.
It is better to create separate Lambda functions for each function
because debugging will be easy in this case.
To know more about AWS Lambda pricing - https://aws.amazon.com/lambda/pricing/
For pricing, you get charged at the millisecond level, so the quicker and tighet each function is the better.
To keep with the idea and design of "Microservices" and follow the "S" in SOLID principles, you usually want a lambda function to do one thing.
The minute you start having lambda functions do two or more things, you're now tying those lambdas and logic together. Ideally a lambda function shouldn't need to know about another lambda function.
You can have one lambda invoke another, but you typically want some sort of eventing orchestration in place to do that for you like Eventbridge or SNS or Step Functions.
So I would avoid putting 3 sets of functionality into one lambda function because now you're vastly limited that functions ability to be reused by other parts of the application potentially.
I have a function that I need to make multiple instances of, but that function requires variables from the previous instance to run. There are 5 variables at different stages of the function that the other ones need to run, so I want to be able to create 5 different instances of the function because the function that inputs data into this function is much faster.
What I am in the process of doing is creating a class with a buffer that will notify each other when each stage is computed by using conditional variables and ofcourse mutex to lock.
What is the fastest way to do this to minimize any time lost since the whole goal is to create multiple instances of this function to process data in multi-threaded manner?
This is a followup to Clojure: Compile time insertion of pre/post functions
My goal is to call a debug function instead of throwing an exception. I am looking for the best way to store a list of stack frames, function calls and their arguments, to accomplish this.
I want to have a function (my-uber-debug), so that when I call it (instead of throwing an exception), the following things happen:
a new Java window pops up
there is a record of the current clojure stack frame
for each stack frame, there is a record of the argument passed to the function
This is so that I can move up/down the stack frames, and examine the arguments passed to get to this current point. [If somehow, magically, we can get the variables defined in "let" environments, that'd be awesome too.]
Current Idea
I'm going to have a thread local variable uber-debug, which has type:
List of StackFrames
where StackFrame = function + arguments
At each function call, it's going to push (cons the current function + arguments to uber-debug), then at the end of a function call, it's going to remove the first element from uber-debug
Then, when I call (my-uber-debug), it just pops up a new java window, and lets me interact with uber-debug
Question
The ideas I've had so far are probably not ideal for setting this up. What is the right way to solve this problem?
Edit:
The question is NOT about the Swing/GUI part. It's about how to store the stack frames.
Thanks!
Your answer may depend on a lot of factors, so I am going to answer this by giving you my thoughts.
If you merely want to store function calls and their parameters when an exception occurs, then either write a macro or function as a wrapper to accomplish this. You would then have to pass all functions to be called to this wrapper. The wrapper would perform the try catch operation and whatever else you need.
You might also want to look into Clojure meta data in addition to writing the wrapper, because your running code could look at its meta-data and make some decisions based on that as well. I have never used meta data, but the information at the link looks promising.
As a final thought, it might be helpful for you to further delineate what you want to accomplish by doing this by editing your original post and putting the information there.
For example, are these stack traces for a library or a main program?
As to storing all this information, are multiple threads going to need it, or just one?
Can you get by storing the information in a let binding at the highest level of your program, or do you need something like a ref?
My question is based on curiosity and not whether there is another approach to the problem or not. It is a strange/interesting question, so please read it with an open mind.
Let's assume there is a game loop that is being called every frame. The game loop in turn calls several functions through a myriad of if statements. For example, if the user has GUI to false then don't refresh the GUI otherwise call RefreshGui(). There are many other if statements in the loop and they call their respective functions if they are true. Some are if/if-else.../else which are more costly in the worst case. Even the functions that are called, if the if statement is true, have logic. If user wants raypicking on all objects call FunctionA(), if user wants raypicking on lights, call FunctionB(), ... , else call all functions. Hopefully you get the idea.
My point is, that is a lot of redundant if statements. So I decided to use function pointers instead. Now my assumption is that a function pointer is always going to be faster than an if statement. It is a replacement for if/else. So if the user wants to switch between two different camera modes, he/she presses the C key to toggle between them. The callback function for the keyboard changes the function pointer to the correct UpdateCamera function (in this case, the function pointer can point to either UpdateCameraFps() or UpdateCameraArcBall() )... you get the gist of it.
Now to the question itself. What if I have several update functions all with the same signature (let's say void (*Update)(float time) ), so that a function pointer can potentially point to any one of them. Then, I have a vector which is used to store the pointers. Then in my main update loop, I go through the vector and call each update function. I can remove/add and even change the order of the updates, without changing the underlying code. In the best case, I might only be calling one update function or in the worst case all of them, all with a very clean while loop and no nasty (potentially nested) if statements. I have implemented this part and it works great. I am aware, that, with each iteration of the while loop responsible for iterating through the vector, I am checking whether the itrBegin == itrEnd. More specifically while (itrBegin != itrEnd). Is there any way to avoid the call to the if statements? Can I use branch prediction to my advantage (or am I taking advantage of it already without knowing)?
Again, please take the question as-is, i.e. I am not looking for a different approach (although you are more than welcome to give one).
EDIT: A few replies state that this is an unneeded premature optimization and I should not be focusing on it and that the if-statement(s) cost is minuscule compared to the work done in all the separate update functions. Very true, and I completely agree, but that was not the point of the question and I apologize if I did not make the question clearer. I did learn quite a few new things with all the replies though!
there is a game loop that is being called every frame
That's a backwards way of describing it. A game loop doesn't run during a frame, a frame is handled in the body of the game loop.
my assumption is that a function pointer is always going to be faster than an if statement
Have you tested that? It's not likely to be true, especially if you're changing the pointer frequently (which really messes with the CPU's branch prediction).
Can I use branch prediction to my advantage (or am I taking advantage of it already without knowing)?
This is just wishful thinking. By having one indirect call inside your loop calling a bunch of different functions you are definitely working against the CPU branch prediction logic.
More specifically while (itrBegin != itrEnd). Is there any way to avoid the call to the if statements?
One thing you could do in order to avoid conditionals as you iterate the chain of functions is to use a linked list. Then each function can call the next one unconditionally, and you simply install your termination logic as the last function in the chain (longjmp or something). Or you could hopefully just never terminate, include glSwapBuffers (or the equivalent for your graphics API) in the list and just link it back to the beginning.
First, profile your code. Then optimize the parts that need it.
"if" statements are the least of your concerns. Typically, with optimization, you focus on loops, I/O operations, API calls (e.g. SQL), containers/algorithms that are inefficient and used frequently.
Using function pointers to try to optimize is typically the worst thing you can do. You kill any chance at code readability and work against the CPU and compiler. I recommend using polymorphism or just use the "if" statements.
To me, this is asking for an event-driven approach. Rather than checking every time if you need to do something, monitor for the incoming request to do something.
I don't know if you consider it a deviation from your approach, but it would reduce the number of if...then statements to 1.
while( active )
{
// check message queue
if( messages )
{
// act on each message and update flags accordingly
}
// draw based on flags (whether or not they changed is irrelevant)
}
EDIT: Also I agree with the poster who stated that the loop should not be based on frames; the frames should be based on the loop.
If the conditions checked by your ifs are not changing during the loop, you could check them all once, and set a function pointer to the function you'd like to call in that case. Then in the loop call the function the function pointer points to.