Mavryk_time_measurement_ppx.Time_ppxThis PPX is used to handle the rewriting of Ocaml expressions annotated with the following attributes: [@time.duration], [@time.duration_lwt], [@time.timestamp_pre] and [@time.flush].
If one of these annotations is detected on an expression, this last one will be wrapped in a thunk that will be given respectively to duration, duration_lwt, timestamp_pre or flush functions from Mavryk_time_measurement_runtime.Default.Time_measurement module along with the label extracted from the attribute payload if needed.
Example:
let x = 40 + 2 [@time.duration label42] in x
(* ==> *)
let x =
Mavryk_time_measurement_runtime.Default.Time_measurement.duration
"label42"
(fun () -> 40 + 2)The PPX handles cases where sub-expressions are annotated.
For example, the following program will be valid and both @time.duration attributes will be effective:
(let x =
"Hi there!" [@time.duration sub_expr]
in x) [@time.duration expr]It however needs to be handled with care: When tagging nested expressions, the outer time measurement will include the time spent to measure the sub-expression.
All dedicated attributes must follow the following rules in order to be semantically correct:
@time.duration, @time.duration_lwt and @time.timestamp_pre must contain a payload.@time.duration, @time.duration_lwt and @time.timestamp_pre's payload should be a valid Ocaml identifier (as described here: https://ocaml.org/manual/lex.html) optionally followed by an expression that evaluates to a string.type rewriter = | Duration of measurement_key * Ppxlib.location| Duration_lwt of measurement_key * Ppxlib.location| Timestamp_pre of measurement_key * Ppxlib.location| Flush of Ppxlib.locationval duration : measurement_key -> Ppxlib.location -> rewriterval duration_lwt : measurement_key -> Ppxlib.location -> rewriterval timestamp_pre : measurement_key -> Ppxlib.location -> rewriterval flush : Ppxlib.location -> rewriterval create_rewriter_constants : string -> rewriter_constantsval duration_constants : rewriter_constantsval duration_lwt_constants : rewriter_constantsval timestamp_pre_constants : rewriter_constantsval flush_constants : rewriter_constantsval constants_of_rewriter : rewriter -> rewriter_constantsval labeled_rewriter_of_attr_name :
string ->
(measurement_key -> Ppxlib.location -> rewriter) optionval locaction_of_rewriter : rewriter -> Ppxlib.locationhas_attribute_name attribute name evaluates if the name of the given attribute is equal to the given name.
filter_attribute_with_name name attributes evaluates in the list of attributes whose elements having the given name have been filtered out.
filter_attribute_with_names names attributes evaluates in the list of attributes whose elements having one of the given names have been filtered out.
fresh_identifier prefix evaluates in a new identifier prefixed by prefix at each call. The function guarantees that it never returns the same identifier twice.
val key_of_payload : Ppxlib.Location.t -> Ppxlib.payload -> measurement_keykey_of_payload location payload extracts a measurement_key from the given payload if possible. The payload can be:
val rewriter_of_attribute : Ppxlib.attribute -> rewriter optionrewriter_of_attribute attribute inspects the given attribute name and tries to recognise if it is related to this PPX. In this case, it will evaluate in the corresponding rewriter.
val extract_rewriters : Ppxlib.attribute list -> rewriter listextract_rewriters attributes inspects the given list of attributes and evaluates in the list of rewriters that have been identified with it.
val validate_rewriters : rewriter list -> unitvalidate_rewriters rewriters inspects the given list of rewriters and ensures that it is semantically correct.
val argument_of_key :
measurement_key ->
Ppxlib.location ->
Ppxlib_ast.Ast.expressionargument_of_key key loc transforms the given measurement key into a labeled argument that intends be used in an expression application at the given location.
val wrap_with_labelled_call :
Ppxlib_ast.Ast.expression ->
Ppxlib.location ->
measurement_key ->
string ->
Astlib.Ast_500.Parsetree.expressionwrap_with_labelled_call expr loc key fn wraps the given expression into a thunk and gives it as an argument to a call of the function fn of module Time_measurement. It will use the given measurement key to compute subsidiary arguments.
val bind_with_flush_call :
Ppxlib_ast.Ast.expression ->
Ppxlib.location ->
Ppxlib_ast.Ast.expressionbind_with_flush_call expr loc binds the given expression with a Lwt promise that memoizes the results of expr and then call Time_measurement.flush. It then binds to a new Lwt promise that returns the memoized result of expr.
val rewrite : rewriter list -> Ppxlib.expression -> Ppxlib_ast.Ast.expressionrewrite rewriters initial_expr sequentially interpretes the given rewriters in order to rewrite the given expression initial_expr.
remove_attributes expr removes the attributes of the given expression if they match attributes handled by this PPX.