Skip to content

Commit c66ed9e

Browse files
committed
Used untagged integers in external calls
1 parent fdd68a4 commit c66ed9e

File tree

4 files changed

+567
-261
lines changed

4 files changed

+567
-261
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Fixed bigarray library dependencies. No need for the user to explicitly
44
link with `bigarray` when using certain functions anymore.
55

6+
* Used untagged integer representations in external calls for improved
7+
efficiency.
8+
69
### 4.1.0 (2017-08-02)
710

811
* Switched to jbuilder and topkg

src/postgresql.ml

Lines changed: 139 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,12 @@ type ftype =
103103
| ANYELEMENT
104104
| JSONB
105105

106-
external ftype_of_oid : oid -> ftype = "ftype_of_oid_stub"
107-
external oid_of_ftype : ftype -> oid = "oid_of_ftype_stub"
106+
external ftype_of_oid :
107+
(int (* oid *) [@untagged]) -> ftype
108+
= "ftype_of_oid_stub_bc" "ftype_of_oid_stub"
109+
110+
external oid_of_ftype : ftype -> (int (* oid *) [@untagged])
111+
= "oid_of_ftype_stub_bc" "oid_of_ftype_stub" [@@noalloc]
108112

109113
let string_of_ftype = function
110114
| BOOL -> "BOOL"
@@ -351,10 +355,12 @@ module Stub = struct
351355
connection -> connection_status = "PQstatus_stub" [@@noalloc]
352356

353357
external error_message : connection -> string = "PQerrorMessage_stub"
354-
external backend_pid : connection -> int = "PQbackendPID_stub" [@@noalloc]
355358

356-
external server_version :
357-
connection -> int = "PQserverVersion_stub" [@@noalloc]
359+
external backend_pid : connection -> (int [@untagged])
360+
= "PQbackendPID_stub_bc" "PQbackendPID_stub" [@@noalloc]
361+
362+
external server_version : connection -> (int [@untagged])
363+
= "PQserverVersion_stub_bc" "PQserverVersion_stub" [@@noalloc]
358364

359365
(* Command Execution Functions *)
360366

@@ -381,76 +387,111 @@ module Stub = struct
381387
external make_empty_res :
382388
connection -> result_status -> result = "PQmakeEmptyPGresult_stub"
383389

384-
external ntuples : result -> int = "PQntuples_stub" [@@noalloc]
385-
external nparams : result -> int = "PQnparams_stub"
390+
external ntuples : result -> (int [@untagged])
391+
= "PQntuples_stub_bc" "PQntuples_stub" [@@noalloc]
392+
393+
external nparams : result -> (int [@untagged])
394+
= "PQnparams_stub_bc" "PQnparams_stub"
395+
396+
external nfields : result -> (int [@untagged])
397+
= "PQnfields_stub_bc" "PQnfields_stub" [@@noalloc]
398+
399+
external fname : result -> (int [@untagged]) -> string
400+
= "PQfname_stub_bc" "PQfname_stub"
401+
402+
external fnumber : result -> string -> (int [@untagged])
403+
= "PQfnumber_stub_bc" "PQfnumber_stub" [@@noalloc]
386404

387-
external nfields : result -> int = "PQnfields_stub" [@@noalloc]
388-
external fname : result -> int -> string = "PQfname_stub"
389-
external fnumber : result -> string -> int ="PQfnumber_stub" [@@noalloc]
390-
external fformat : result -> int -> FFormat.t = "PQfformat_stub" [@@noalloc]
391-
external ftype : result -> int -> oid = "PQftype_stub" [@@noalloc]
392-
external paramtype : result -> int -> oid = "PQparamtype_stub"
405+
external fformat : result -> (int [@untagged]) -> FFormat.t
406+
= "PQfformat_stub_bc" "PQfformat_stub" [@@noalloc]
407+
408+
external ftype :
409+
result -> (int [@untagged]) -> (int (* oid *) [@untagged])
410+
= "PQftype_stub_bc" "PQftype_stub" [@@noalloc]
411+
412+
external paramtype :
413+
result -> (int [@untagged]) -> (int (* oid *) [@untagged])
414+
= "PQparamtype_stub_bc" "PQparamtype_stub"
415+
416+
external fmod : result -> (int [@untagged]) -> (int [@untagged])
417+
= "PQfmod_stub_bc" "PQfmod_stub" [@@noalloc]
418+
419+
external fsize : result -> (int [@untagged]) -> (int [@untagged])
420+
= "PQfsize_stub_bc" "PQfsize_stub" [@@noalloc]
393421

394-
external fmod : result -> int -> int = "PQfmod_stub" [@@noalloc]
395-
external fsize : result -> int -> int = "PQfsize_stub" [@@noalloc]
396422
external binary_tuples : result -> bool = "PQbinaryTuples_stub" [@@noalloc]
397423

398-
external getvalue : result -> int -> int -> string = "PQgetvalue_stub"
424+
external getvalue : result -> (int [@untagged]) -> (int [@untagged]) -> string
425+
= "PQgetvalue_stub_bc" "PQgetvalue_stub"
399426

400427
external get_escaped_value :
401-
result -> int -> int -> string = "PQgetescval_stub"
428+
result -> (int [@untagged]) -> (int [@untagged]) -> string
429+
= "PQgetescval_stub_bc" "PQgetescval_stub"
402430

403-
external getisnull :
404-
result -> int -> int -> bool = "PQgetisnull_stub" [@@noalloc]
431+
external getisnull : result -> (int [@untagged]) -> (int [@untagged]) -> bool
432+
= "PQgetisnull_stub_bc" "PQgetisnull_stub" [@@noalloc]
405433

406434
external getlength :
407-
result -> int -> int -> int = "PQgetlength_stub" [@@noalloc]
435+
result -> (int [@untagged]) -> (int [@untagged]) -> (int [@untagged])
436+
= "PQgetlength_stub_bc" "PQgetlength_stub" [@@noalloc]
408437

409438
external cmd_status : result -> string = "PQcmdStatus_stub"
410439
external cmd_tuples : result -> string = "PQcmdTuples_stub"
411-
external oid_value : result -> oid = "PQoidValue_stub" [@@noalloc]
440+
441+
external oid_value : result -> (int (* oid *) [@untagged])
442+
= "PQoidValue_stub_bc" "PQoidValue_stub" [@@noalloc]
412443

413444

414445
(* Asynchronous Query Processing *)
415446

416447
external connect_poll :
417448
connection -> polling_status = "PQconnectPoll_stub" [@@noalloc]
418449

419-
external reset_start :
420-
connection -> bool = "PQresetStart_stub" [@@noalloc]
450+
external reset_start : connection -> bool = "PQresetStart_stub" [@@noalloc]
421451

422452
external reset_poll :
423453
connection -> polling_status = "PQresetPoll_stub" [@@noalloc]
424454

425-
external set_nonblocking :
426-
connection -> bool -> int = "PQsetnonblocking_stub" [@@noalloc]
455+
external set_nonblocking : connection -> bool -> (int [@untagged])
456+
= "PQsetnonblocking_stub_bc" "PQsetnonblocking_stub" [@@noalloc]
427457

428458
external is_nonblocking :
429459
connection -> bool = "PQisnonblocking_stub" [@@noalloc]
430460

431461
external send_query_params :
432-
connection -> string -> string array -> bool array -> int
433-
= "PQsendQueryParams_stub"
462+
connection -> string -> string array -> bool array -> (int [@untagged])
463+
= "PQsendQueryParams_stub_bc" "PQsendQueryParams_stub"
434464

435465
external send_prepare :
436-
connection -> string -> string -> int = "PQsendPrepare_stub" [@@noalloc]
466+
connection -> string -> string -> (int [@untagged])
467+
= "PQsendPrepare_stub_bc" "PQsendPrepare_stub" [@@noalloc]
437468

438469
external send_query_prepared :
439-
connection -> string -> string array -> bool array -> int
440-
= "PQsendQueryPrepared_stub"
470+
connection -> string -> string array -> bool array -> (int [@untagged])
471+
= "PQsendQueryPrepared_stub_bc" "PQsendQueryPrepared_stub"
472+
473+
external send_describe_prepared : connection -> string -> (int [@untagged])
474+
= "PQsendDescribePrepared_stub_bc" "PQsendDescribePrepared_stub"
441475

442-
external send_describe_prepared : connection -> string -> int
443-
= "PQsendDescribePrepared_stub"
476+
external send_describe_portal : connection -> string -> (int [@untagged])
477+
= "PQsendDescribePortal_stub_bc" "PQsendDescribePortal_stub"
444478

445-
external send_describe_portal : connection -> string -> int
446-
= "PQsendDescribePortal_stub"
479+
external set_single_row_mode : connection -> (int [@untagged])
480+
= "PQsetSingleRowMode_stub_bc" "PQsetSingleRowMode_stub"
447481

448-
external set_single_row_mode : connection -> int = "PQsetSingleRowMode_stub"
449482
external get_result : connection -> result = "PQgetResult_stub"
450-
external consume_input : connection -> int = "PQconsumeInput_stub" [@@noalloc]
483+
484+
external consume_input : connection -> (int [@untagged])
485+
= "PQconsumeInput_stub_bc" "PQconsumeInput_stub" [@@noalloc]
486+
451487
external is_busy : connection -> bool = "PQisBusy_stub" [@@noalloc]
452-
external flush : connection -> int = "PQflush_stub" [@@noalloc]
453-
external socket : connection -> int = "PQsocket_stub" [@@noalloc]
488+
489+
external flush : connection -> (int [@untagged])
490+
= "PQflush_stub_bc" "PQflush_stub" [@@noalloc]
491+
492+
external socket : connection -> (int [@untagged])
493+
= "PQsocket_stub_bc" "PQsocket_stub" [@@noalloc]
494+
454495
external request_cancel : connection -> string option = "PQCancel_stub"
455496

456497

@@ -462,26 +503,35 @@ module Stub = struct
462503
(* Functions Associated with the COPY Command *)
463504

464505
external getline :
465-
connection -> Bytes.t -> int -> int -> int = "PQgetline_stub"
506+
connection -> Bytes.t ->
507+
(int [@untagged]) -> (int [@untagged]) -> (int [@untagged])
508+
= "PQgetline_stub_bc" "PQgetline_stub"
466509

467510
external getline_async :
468-
connection -> Bytes.t -> int -> int -> int
469-
= "PQgetlineAsync_stub" [@@noalloc]
511+
connection -> Bytes.t ->
512+
(int [@untagged]) -> (int [@untagged]) -> (int [@untagged])
513+
= "PQgetlineAsync_stub_bc" "PQgetlineAsync_stub" [@@noalloc]
470514

471-
external putline : connection -> string -> int = "PQputline_stub"
515+
external putline : connection -> string -> (int [@untagged])
516+
= "PQputline_stub_bc" "PQputline_stub"
472517

473518
external putnbytes :
474-
connection -> string -> int -> int -> int = "PQputnbytes_stub"
519+
connection -> string ->
520+
(int [@untagged]) -> (int [@untagged]) -> (int [@untagged])
521+
= "PQputnbytes_stub_bc" "PQputnbytes_stub"
475522

476-
external endcopy : connection -> int = "PQendcopy_stub"
523+
external endcopy : connection -> (int [@untagged])
524+
= "PQendcopy_stub_bc" "PQendcopy_stub"
477525

478526
external escape_string_conn :
479-
connection -> string -> pos : int -> len : int -> string
480-
= "PQescapeStringConn_stub"
527+
connection -> string ->
528+
pos : (int [@untagged]) -> len : (int [@untagged]) -> string
529+
= "PQescapeStringConn_stub_bc" "PQescapeStringConn_stub"
481530

482531
external escape_bytea_conn :
483-
connection -> string -> pos : int -> len : int -> string
484-
= "PQescapeByteaConn_stub"
532+
connection -> string ->
533+
pos : (int [@untagged]) -> len : (int [@untagged]) -> string
534+
= "PQescapeByteaConn_stub_bc" "PQescapeByteaConn_stub"
485535

486536

487537
(* Control Functions *)
@@ -492,32 +542,59 @@ module Stub = struct
492542

493543
(* Large objects *)
494544

495-
external lo_creat : connection -> oid = "lo_creat_stub"
496-
external lo_import : connection -> string -> oid = "lo_import_stub"
497-
external lo_export : connection -> oid -> string -> int = "lo_export_stub"
498-
external lo_open : connection -> oid -> large_object = "lo_open_stub"
499-
external lo_close : connection -> large_object -> int = "lo_close_stub"
500-
external lo_tell : connection -> large_object -> int = "lo_tell_stub"
501-
external lo_unlink : connection -> oid -> oid = "lo_unlink_stub"
545+
external lo_creat : connection -> (int (* oid *) [@untagged])
546+
= "lo_creat_stub_bc" "lo_creat_stub"
547+
548+
external lo_import : connection -> string -> (int (* oid *) [@untagged])
549+
= "lo_import_stub_bc" "lo_import_stub"
550+
551+
external lo_export :
552+
connection -> (int (* oid *) [@untagged]) -> string -> (int [@untagged])
553+
= "lo_export_stub_bc" "lo_export_stub"
554+
555+
external lo_open :
556+
connection -> (int (* oid *) [@untagged]) ->
557+
(int (* large_object *) [@untagged])
558+
= "lo_open_stub_bc" "lo_open_stub"
559+
560+
external lo_close :
561+
connection -> (int (* large_object *) [@untagged]) -> (int [@untagged])
562+
= "lo_close_stub_bc" "lo_close_stub"
563+
564+
external lo_tell :
565+
connection -> (int (* large_object *) [@untagged]) -> (int [@untagged])
566+
= "lo_tell_stub_bc" "lo_tell_stub"
567+
568+
external lo_unlink :
569+
connection -> (int (* oid *) [@untagged]) -> (int (* oid *) [@untagged])
570+
= "lo_unlink_stub_bc" "lo_unlink_stub"
502571

503572
external lo_read :
504-
connection -> large_object -> Bytes.t -> int -> int -> int = "lo_read_stub"
573+
connection -> (int (* large_object *) [@untagged]) ->
574+
Bytes.t -> (int [@untagged]) -> (int [@untagged]) -> (int [@untagged])
575+
= "lo_read_stub_bc" "lo_read_stub"
505576

506577
external lo_read_ba :
507-
connection -> large_object ->
578+
connection -> (int (* large_object *) [@untagged]) ->
508579
(char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t ->
509-
int -> int -> int = "lo_read_ba_stub"
580+
(int [@untagged]) -> (int [@untagged]) -> (int [@untagged])
581+
= "lo_read_ba_stub_bc" "lo_read_ba_stub"
510582

511583
external lo_write :
512-
connection -> large_object -> string -> int -> int -> int = "lo_write_stub"
584+
connection -> (int (* large_object *) [@untagged]) ->
585+
string -> (int [@untagged]) -> (int [@untagged]) -> (int [@untagged])
586+
= "lo_write_stub_bc" "lo_write_stub"
513587

514588
external lo_write_ba :
515-
connection -> large_object ->
589+
connection -> (int (* large_object *) [@untagged]) ->
516590
(char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t ->
517-
int -> int -> int = "lo_write_ba_stub"
591+
(int [@untagged]) -> (int [@untagged]) -> (int [@untagged])
592+
= "lo_write_ba_stub_bc" "lo_write_ba_stub"
518593

519594
external lo_seek :
520-
connection -> large_object -> int -> seek_cmd -> int = "lo_lseek_stub"
595+
connection -> (int (* large_object *) [@untagged]) ->
596+
(int [@untagged]) -> seek_cmd -> (int [@untagged])
597+
= "lo_lseek_stub_bc" "lo_lseek_stub"
521598
end
522599

523600

@@ -1091,5 +1168,4 @@ object (self)
10911168
method escape_bytea ?pos ?len str =
10921169
let pos, len = get_str_pos_len ~loc:"escape_bytea" ?pos ?len str in
10931170
wrap_conn (fun conn -> Stub.escape_bytea_conn conn str ~pos ~len)
1094-
10951171
end

src/postgresql.mli

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,16 @@ exception Oid of oid
173173

174174
(** {6 Utility functions}*)
175175

176-
external unescape_bytea : string -> string = "PQunescapeBytea_stub"
176+
val unescape_bytea : string -> string
177177
(** [unescape_bytea str] unescapes binary string [str]. This function
178178
supports the new hex format for encoding bytea strings (introduced
179179
in Postgresql 9.0) even if the local libpq library is from an
180180
older version. *)
181181

182-
external ftype_of_oid : oid -> ftype = "ftype_of_oid_stub"
182+
val ftype_of_oid : oid -> ftype
183183
(** [ftype_of_oid oid] converts [oid] to an [ftype]. *)
184184

185-
external oid_of_ftype : ftype -> oid = "oid_of_ftype_stub"
185+
val oid_of_ftype : ftype -> oid
186186
(** [oid_of_ftype ftype] converts [ftype] to an [oid]. *)
187187

188188
val string_of_ftype : ftype -> string
@@ -194,7 +194,7 @@ val ftype_of_string : string -> ftype
194194

195195
(** {6 Handling results of commands and queries} *)
196196

197-
external result_status : result_status -> string = "PQresStatus_stub"
197+
val result_status : result_status -> string
198198
(** [result_status stat] convert status [stat] to a human-readable message *)
199199

200200
val invalid_oid : oid
@@ -423,7 +423,7 @@ module Notification : sig
423423
}
424424
end (* Notification *)
425425

426-
external conndefaults : unit -> conninfo_option array = "PQconndefaults_stub"
426+
val conndefaults : unit -> conninfo_option array
427427
(** [conndefaults ()] @return array of all records of type [conninfo_option] *)
428428

429429

0 commit comments

Comments
 (0)