Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[barray] adopt the barray module #38

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions .ocp-indent
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# -*- conf -*-
# This is an example configuration file for ocp-indent
#
# Copy to the root of your project with name ".ocp-indent", customise, and
# transparently get consistent indentation on all your ocaml source files.

# Starting the configuration file with a preset ensures you won't fallback to
# definitions from "~/.ocp/ocp-indent.conf".
# These are `normal`, `apprentice` and `JaneStreet` and set different defaults.
normal

#
# INDENTATION VALUES
#

# Number of spaces used in all base cases, for example:
# let foo =
# ^^bar
base = 2

# Indent for type definitions:
# type t =
# ^^int
type = 4

# Indent after `let in` (unless followed by another `let`):
# let foo = () in
# ^^bar
in = 0

# Indent after `match/try with` or `function`:
# match foo with
# ^^| _ -> bar
with = 0

# Indent for clauses inside a pattern-match (after the arrow):
# match foo with
# | _ ->
# ^^^^bar
# the default is 2, which aligns the pattern and the expression
match_clause = 4 # this is non-default

# Indentation for items inside extension nodes:
# [%% id.id
# ^^^^contents ]
# [@@id
# ^^^^foo
# ]
ppx_stritem_ext = 2

# When nesting expressions on the same line, their indentation are in
# some cases stacked, so that it remains correct if you close them one
# at a line. This may lead to large indents in complex code though, so
# this parameter can be used to set a maximum value. Note that it only
# affects indentation after function arrows and opening parens at end
# of line.
#
# for example (left: `none`; right: `4`)
# let f = g (h (i (fun x -> # let f = g (h (i (fun x ->
# x) # x)
# ) # )
# ) # )
max_indent = 4


#
# INDENTATION TOGGLES
#

# Wether the `with` parameter should be applied even when in a sub-block.
# Can be `always`, `never` or `auto`.
# if `always`, there are no exceptions
# if `auto`, the `with` parameter is superseded when seen fit (most of the time,
# but not after `begin match` for example)
# if `never`, `with` is only applied if the match block starts a line.
#
# For example, the following is not indented if set to `always`:
# let f = function
# ^^| Foo -> bar
strict_with = never

# Controls indentation after the `else` keyword. `always` indents after the
# `else` keyword normally, like after `then`.
# If set to `never', the `else` keyword won't indent when followed by a newline.
# `auto` indents after `else` unless in a few "unclosable" cases (`let in`,
# `match`...).
#
# For example, with `strict_else=never`:
# if cond then
# foo
# else
# bar;
# baz
# `never` is discouraged if you may encounter code like this example,
# because it hides the scoping error (`baz` is always executed)
strict_else = always

# Ocp-indent will normally try to preserve your in-comment indentation, as long
# as it respects the left-margin or starts with `(*\n`. Setting this to `true`
# forces alignment within comments.
strict_comments = false

# Toggles preference of column-alignment over line indentation for most
# of the common operators and after mid-line opening parentheses.
#
# for example (left: `false'; right: `true')
# let f x = x # let f x = x
# + y # + y
align_ops = true

# Function parameters are normally indented one level from the line containing
# the function. This option can be used to have them align relative to the
# column of the function body instead.
# if set to `always`, always align below the function
# if `auto`, only do that when seen fit (mainly, after arrows)
# if `never`, no alignment whatsoever
#
# for example (left: `never`; right: `always or `auto)
# match foo with # match foo with
# | _ -> some_fun # | _ -> some_fun
# ^^parameter # ^^parameter
align_params = auto


#
# SYNTAX EXTENSIONS
#

# You can also add syntax extensions (as per the --syntax command-line option):
# syntax = mll lwt
9 changes: 5 additions & 4 deletions bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
"dir": "src",
"subdirs": [
{
"dir": "core",
"public": "none"
"dir": "core"
},
{
"dir": "types",
"public": "none"
"dir": "types"
},
{
"dir": "containers"
}
],
"public": [
Expand Down
78 changes: 78 additions & 0 deletions perf/barray_perf.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
open Perf

let bigArray = Barray.makeWithInit 1_000_000 (fun _ -> Random.int 1024)
let bigSeq = Barray.toSequence bigArray

let toList = Array.to_list

let fromList = Array.of_list

let bigList = toList bigArray

let fromSequenceArrayPush i =
let open Bsequence in
let arr = ref [||] in
let rec aux i =
match i () with
| Nil -> !arr
| Cons(x, r) -> ignore @@ Js.Array.push x !arr; aux r
in aux i

let listToSequence (l: 'a list) : 'a Bsequence.t =
let rec aux ll () = match ll with
| [] -> Bsequence.Nil
| (e::r) -> Bsequence.Cons(e, aux (r))
in aux l

let getSeqLength (s:'a Bsequence.t) : int =
let open Bsequence in
let rec aux i ss =
match ss () with
| Nil -> i
| Cons (_, r) -> aux (i+1) r
in aux 0 s

let getFirstFromSeq (s:'a Bsequence.t) : 'a option =
match s () with
| Bsequence.Nil -> None
| Bsequence.Cons (x, _) -> Some x

let fromSequencePreAllocArray (s:'a Bsequence.t) :'a Barray.t =
let len = getSeqLength s in
let first = getFirstFromSeq s in
match first with
| None -> [||]
| Some f -> begin
let arr = ref (Barray.make len f) in
let rec aux idx ss =
match ss () with
| Bsequence.Nil -> !arr
| Bsequence.Cons (x, r) -> (!arr).(idx) <- x; aux (idx+1) r
in aux 0 s
end

(** Do some correctness test first *)
let _ =
let lst = [1; 2; 3; 4] in
let arr0 = fromList lst in
let arr1 = fromSequenceArrayPush (listToSequence [1; 2; 3; 4]) in
let arr2 = fromSequencePreAllocArray (listToSequence [1; 2; 3; 4]) in
Js.log arr0;
Js.log arr1;
Js.log arr2

let _ =
runPerfTest "listToArray - fromList" ~repeatTimes:100 (fun _ ->
ignore @@ fromList bigList
);
runPerfTest "listToArray - fromSequence" ~repeatTimes:100 (fun _ ->
ignore @@ fromSequenceArrayPush (listToSequence bigList)
);
let listSeq = listToSequence bigList in
runPerfTest "listToArray - fromSequence - predefined seq" ~repeatTimes:100 (fun _ ->
ignore @@ fromSequenceArrayPush listSeq
);
runPerfTest "listToArray - fromSequence - preallocated array" ~repeatTimes: 100 (fun _ ->
ignore @@ fromSequencePreAllocArray (listToSequence bigList)
)

30 changes: 14 additions & 16 deletions perf/bchar_perf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ open Perf

let _ =
runPerfTest "getDigit" (fun _ ->
let c = Bchar.getDigit '1' in
match c with
| Some _ -> ()
| None -> ()
);
let c = Bchar.getDigit '1' in
match c with
| Some _ -> ()
| None -> ()
);
runPerfTest "getDigitOrRaise" (fun _ ->
let _ = (try
Bchar.getDigitOrRaise '1'
with
| Failure _ -> 1) in ();
);
let _ = (try Bchar.getDigitOrRaise '1'
with
| Failure _ -> 1) in ();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look very good.

Either

let _ = (
  try
    Bchar.getDigitOrRaise '1'
  with
  | Failure _ -> 1) in ();

or

let _ = (
  try Bchar.getDigitOrRaise '1'
  with Failure _ -> 1) in ();

or perhaps using begin/end instead of parens would be better

);
runPerfTest "getDigitOrRaise Fail" (fun _ ->
let _ = (try
Bchar.getDigitOrRaise 'a'
with
| Failure _ -> 1
) in ();
)
let _ = (try Bchar.getDigitOrRaise 'a'
with
| Failure _ -> 1
) in ();
)
15 changes: 15 additions & 0 deletions perf/bsequence_perf.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
open Perf

let it = Barray.toSequence [| 1; 2 ; 3|]

let _ =
runPerfTest "Barray.toSequence" (fun _ ->
ignore @@ ([| 1; 2; 3|] |> Barray.toSequence)
);
runPerfTest "Sequence.map" (fun _ ->
ignore @@ Bsequence.map (fun x -> x * x) it
);
runPerfTest "Sequence.foldLeft" (fun _ ->
Bsequence.reduce (fun acc x -> x::acc) [] it
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These have actually gotten worse... Not sure why that would be


Loading