Skip to content

Commit 31785f2

Browse files
authored
Merge performance improvements (#6987)
Draft, waiting for testing and PR reviews to complete. Depends on: #6974 #6971 Also would be good to have an updated ocaml-rpc for the JSONRPC fixes in xs-opam. PRs for ocaml-rpc optimizations: mirage/ocaml-rpc#194 mirage/ocaml-rpc#193 mirage/ocaml-rpc#192 mirage/ocaml-rpc#184 The optimizations are not strictly required, but the first PR about fixing 4.14 build is, otherwise we can't take the new version into xs-opam.
2 parents 13f2494 + 3fe9448 commit 31785f2

15 files changed

Lines changed: 85 additions & 113 deletions

File tree

ocaml/idl/ocaml_backend/gen_server.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ let gen_module api : O.Module.t =
465465
~params:
466466
[
467467
O.Anon (Some "http_req", "Http.Request.t")
468-
; O.Anon (Some "fd", "Unix.file_descr")
468+
; O.Anon (Some "fd", "Unix.file_descr option")
469469
; O.Anon (Some "call", "Rpc.call")
470470
]
471471
~ty:"response"

ocaml/libs/xapi-stdext/lib/xapi-stdext-std/listext.ml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ module List = struct
4040
inv_assoc k t
4141

4242
(* Tail-recursive map. *)
43-
let map_tr f l = rev (rev_map f l)
43+
44+
let[@tail_mod_cons] rec map_tr f l =
45+
match l with
46+
| [] ->
47+
[]
48+
| [x] ->
49+
[f x]
50+
| x1 :: x2 :: xs ->
51+
let fx1 = f x1 in
52+
let fx2 = f x2 in
53+
fx1 :: fx2 :: map_tr f xs
4454

4555
let count pred l =
4656
fold_left

ocaml/libs/xml-light2/dune

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
(library
2-
(name xmllight2)
3-
(public_name xml-light2)
4-
(modules xml)
5-
(wrapped false)
6-
(libraries
7-
threads
8-
xmlm
9-
)
10-
)
2+
(name xmllight2)
3+
(public_name xml-light2)
4+
(modules xml)
5+
(wrapped false)
6+
(libraries threads xapi-stdext-std xmlm))
117

128
(executable
13-
(modes exe)
14-
(name xmlpp)
15-
(modules xmlpp)
16-
(libraries
17-
xml-light2
18-
)
19-
)
20-
9+
(modes exe)
10+
(name xmlpp)
11+
(modules xmlpp)
12+
(libraries xml-light2))

ocaml/libs/xml-light2/xml.ml

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,17 @@ let _ =
4242
(* internal parse function *)
4343
let is_empty xml =
4444
let is_empty_string s =
45-
let is_empty = ref true in
46-
for i = 0 to String.length s - 1 do
47-
if s.[i] <> '\n' && s.[i] <> ' ' && s.[i] <> '\t' then is_empty := false
48-
done ;
49-
!is_empty
45+
String.for_all (function '\n' | ' ' | '\t' -> true | _ -> false) s
5046
in
5147
match xml with PCData data when is_empty_string data -> true | _ -> false
5248

5349
let _parse i =
5450
let el (tag : Xmlm.tag) (children : xml list) : xml =
5551
let name_local = snd (fst tag) in
5652
let attrs' =
57-
List.map (fun (nameattr, str) -> (snd nameattr, str)) (snd tag)
53+
Xapi_stdext_std.Listext.List.map_tr
54+
(fun (nameattr, str) -> (snd nameattr, str))
55+
(snd tag)
5856
in
5957
Element
6058
(name_local, attrs', List.filter (fun xml -> not (is_empty xml)) children)
@@ -93,28 +91,24 @@ let parse_string s =
9391
let esc_pcdata data =
9492
let buf = Buffer.create (String.length data + 10) in
9593
String.iter
96-
(fun c ->
97-
let s =
98-
match c with
99-
| '>' ->
100-
"&gt;"
101-
| '<' ->
102-
"&lt;"
103-
| '&' ->
104-
"&amp;"
105-
| '"' ->
106-
"&quot;"
107-
| c
108-
when (c >= '\x20' && c <= '\xff')
109-
|| c = '\x09'
110-
|| c = '\x0a'
111-
|| c = '\x0d' ->
112-
String.make 1 c
113-
| _ ->
114-
""
115-
in
116-
Buffer.add_string buf s
117-
)
94+
(function
95+
| '>' ->
96+
Buffer.add_string buf "&gt;"
97+
| '<' ->
98+
Buffer.add_string buf "&lt;"
99+
| '&' ->
100+
Buffer.add_string buf "&amp;"
101+
| '"' ->
102+
Buffer.add_string buf "&quot;"
103+
| c
104+
when (c >= '\x20' && c <= '\xff')
105+
|| c = '\x09'
106+
|| c = '\x0a'
107+
|| c = '\x0d' ->
108+
Buffer.add_char buf c
109+
| _ ->
110+
()
111+
)
118112
data ;
119113
Buffer.contents buf
120114

@@ -139,9 +133,7 @@ let to_fct xml f =
139133
let astr = str_of_attrs attrs in
140134
let on = fmt "<%s%s>" name astr in
141135
let off = fmt "</%s>" name in
142-
f on ;
143-
List.iter (fun child -> print child) children ;
144-
f off
136+
f on ; List.iter print children ; f off
145137
| PCData data ->
146138
f (esc_pcdata data)
147139
in
@@ -213,22 +205,3 @@ let to_string_fmt xml =
213205
to_fct_fmt xml (fun s -> Buffer.add_string buffer s) ;
214206
let s = Buffer.contents buffer in
215207
Buffer.reset buffer ; s
216-
217-
(* helpers functions *)
218-
exception Not_pcdata of string
219-
220-
exception Not_element of string
221-
222-
let pcdata = function PCData x -> x | e -> raise (Not_pcdata (to_string e))
223-
224-
let children = function
225-
| Element (_, _, c) ->
226-
c
227-
| e ->
228-
raise (Not_element (to_string e))
229-
230-
let tag = function
231-
| Element (x, _, _) ->
232-
x
233-
| e ->
234-
raise (Not_element (to_string e))

ocaml/libs/xml-light2/xml.mli

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,6 @@ val parse_in : in_channel -> xml
3232

3333
val parse_string : string -> xml
3434

35-
val to_fct : xml -> (string -> unit) -> unit
36-
(** output functions *)
37-
38-
val to_fct_fmt : xml -> (string -> unit) -> unit
39-
4035
val to_string : xml -> string
4136

4237
val to_string_fmt : xml -> string
43-
44-
(** helper functions *)
45-
exception Not_pcdata of string
46-
47-
exception Not_element of string
48-
49-
val pcdata : xml -> string
50-
51-
val children : xml -> xml list
52-
53-
val tag : xml -> string

ocaml/tests/test_client.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
work in unit tests. *)
1111
let make_client_params ~__context =
1212
let req = Xmlrpc_client.xmlrpc ~version:"1.1" "/" in
13-
let rpc = Api_server.Server.dispatch_call req Unix.stdout in
13+
let rpc = Api_server.Server.dispatch_call req None in
1414
let session_id =
1515
let session_id = Ref.make_secret () in
1616
let now = Clock.Date.now () in

ocaml/xapi-consts/constants.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ let owner_key = "owner"
315315
(* set in VBD other-config to indicate that clients can delete the attached VDI on VM uninstall if they want.. *)
316316

317317
(* xapi-cli-server doesn't link xapi-globs *)
318-
let use_event_next = ref true
318+
let use_event_next = ref false
319319

320320
(* the time taken to wait before restarting in a different mode for pool eject/join operations *)
321321
let fuse_time = ref 10.

ocaml/xapi/api_server.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let callback1 ?(json_rpc_version = Jsonrpc.V1) is_json req fd call =
3333
else
3434
let response =
3535
let@ req = Helper.with_tracing ~name:"Server.dispatch_call" req in
36-
Server.dispatch_call req fd call
36+
Server.dispatch_call req (Some fd) call
3737
in
3838
let translated =
3939
if

ocaml/xapi/context.ml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -481,29 +481,28 @@ let get_http_other_config http_req =
481481
let of_http_req ?session_id ?(internal_async_subtask = false) ~generate_task_for
482482
~supports_async ~label ~http_req ~fd () =
483483
let http_other_config = get_http_other_config http_req in
484+
let origin =
485+
match fd with None -> Internal | Some fd -> Http (http_req, fd)
486+
in
484487
let new_task_context () =
485488
let subtask_of =
486489
Option.map Ref.of_string http_req.Http.Request.subtask_of
487490
in
488491
make ?session_id ?subtask_of ~http_other_config ~task_in_database:true
489-
~origin:(Http (http_req, fd))
490-
label
492+
~origin label
491493
in
492494
if internal_async_subtask then
493495
new_task_context ()
494496
else
495497
match http_req.Http.Request.task with
496498
| Some task_id ->
497-
from_forwarded_task ?session_id ~http_other_config
498-
~origin:(Http (http_req, fd))
499+
from_forwarded_task ?session_id ~http_other_config ~origin
499500
(Ref.of_string task_id)
500501
| None ->
501502
if generate_task_for && supports_async then
502503
new_task_context ()
503504
else
504-
make ?session_id ~http_other_config
505-
~origin:(Http (http_req, fd))
506-
label
505+
make ?session_id ~http_other_config ~origin label
507506

508507
let set_test_rpc context rpc = context.test_rpc <- Some rpc
509508

ocaml/xapi/context.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ val of_http_req :
4949
-> supports_async:bool
5050
-> label:string
5151
-> http_req:Http.Request.t
52-
-> fd:Unix.file_descr
52+
-> fd:Unix.file_descr option
5353
-> unit
5454
-> t
5555

0 commit comments

Comments
 (0)