Skip to content

Compile records as objects: single commit. - #3785

Closed
cristianoc wants to merge 3 commits into
masterfrom
records_as_objects2
Closed

cristianoc wants to merge 3 commits into
masterfrom
records_as_objects2

Conversation

@cristianoc

@cristianoc cristianoc commented Aug 28, 2019 •

Copy link
Copy Markdown
Collaborator

Cleaned up version of #3741.

Changes

  • When creating a block with Blk_record tag, create an object.
  • Type int64 becomes an object with lo and hi.
  • Field lookup and update for normal and float records.
  • Record copy.
  • caml_lexer.ml and caml.parser.ml are "manually compiled" and need to switch to objects.
  • Pervasives operations on references need adapting, as well as the uses in caml_obj.ml which does not use the modified pervasives.
  • stream.ml has an internal optimization assuming array runtime and needs changing.

Questions

  • Check bytecomp/translobj.ml for implicit assumptions about the memory layout.
  • Check "compatibility with old hash tables".
  • Replace Object.assign with a more direct implementation.
  • Check inline record on 4.06.
  • Check interaction with hooks PPX.

cristianoc added a commit to rescript-association/genType that referenced this pull request Sep 16, 2019
Still need to activate based on version, once the version number where the corresponding bucklescript changes ship is known:  rescript-lang/rescript#3785.
Comment thread jscomp/core/js_long.ml

let get_lo x = E.dot x "lo"
let get_hi x = E.dot x "hi"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mark to check if such inlining is really worth such complexity later

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I will send a PR to rely less on internals of int64, it makes more sense, since we probably will switch the implementation relying on bigint in the future

Comment thread jscomp/runtime/caml_array.ml Resolved
Comment thread jscomp/runtime/caml_int64.ml Resolved
tbl.lex_check = caml_lex_array(tbl[lex_check]);
tbl.lex_trans = caml_lex_array(tbl[lex_trans]);
tbl.lex_default = caml_lex_array(tbl[lex_default]);
tbl.processed = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what's the rational of adding tbl.processed, is it a bug fix?

@cristianoc cristianoc Sep 18, 2019 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is subtle:
This code already existed before my change:

tbl.lex_default = caml_lex_array(tbl[lex_default]);

So the code was effectively using object fields in addition to array index.
And then the check

if (!tbl.lex_default) {

semantically means: "I have already processed this code".
In my changes, I've made this subtle distinction explicit. The behavior has not changed.

*)

external caml_update_dummy : Caml_obj_extern.t -> Caml_obj_extern.t -> unit = "Object.assign" [@@bs.val]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

caml_update_dummy is used in initializating rec values, how is it related to record encoding?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is used when initializing:

type a = {
  b: b;
}
and b = {
  a: a
};;

And it affects gpr_1716_test.ml.

Is it used for other things as well, and should there be 2 versions of it?

@bobzhang bobzhang Sep 19, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

type a = {
  b: b;
}
and b = {
  a: a
};;

let rec a = { b } and b = { a };;

This seems more subtle. Here the recursive value could be record, but other blocks too, I guess changing caml_update_dummy will break other recursive values, will dig into later

(* compatibility with old hash tables *)
if Obj.size (Obj.repr h) >= 3
if (* XXX *) true || Obj.size (Obj.repr h) >= 3
then (seeded_hash_param 10 100 h.seed key) land (Array.length h.data - 1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we may remove such thing, since we don't need maintain compatibility with old hashtbl

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Happy to remove.
Does it mean I should remove the false branch and any dead code it could result from that?

external incr : int ref -> unit = "%incr"
external decr : int ref -> unit = "%decr"
(* external ( ! ) : 'a ref -> 'a = "%field0" *)
let (!) x = x.contents

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe we can introduce new primitives for deferencing and update

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This may affect an optimization pass of reference elimination

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sure, can be done in a cleanup phase. Reminder: search field0 and create another version.
In this pass, I'd like to focus on correctness first.

@bobzhang bobzhang left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Review not finished yet, my major concern is how code as below will be compiled

type t  = { x :  int  ;  y : int}
let f (x : t) = {x with y = 3 }

can we keep it as good as we have now
Edit: can you also do a rebase against the master?

comment=comment_with_tag
}
| _ ->
let comment =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about we delay the creation of Object .., stay with Caml_block, only convert it to Object in js_dump.ml to preserve existing semantics the most

Comment thread jscomp/core/js_long.ml

let get_lo x = E.dot x "lo"
let get_hi x = E.dot x "hi"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I will send a PR to rely less on internals of int64, it makes more sense, since we probably will switch the implementation relying on bigint in the future

| Fld_record comment
-> E.array_index_by_int ~comment e i
| Fld_record comment ->
E.dot e comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can we do something similiar as module_access below to preserve as much as semantics as possible

| Fld_record s
| Fld_module s
-> E.array_index_by_int ~comment:s e i
-> E.dot e s

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Float record does not make too much sense in JS context, I will send a PR to remove such stuff

(Js_arr.set_array (E.var id) (E.int (Int32.of_int i)) (E.var lid))
->
S.exp
(E.assign (E.dot (E.var id) fields.(i)) (E.var lid))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's create a dedicated module for record operations.

Comment thread jscomp/runtime/caml_int64.ml Resolved
@cristianoc

Copy link
Copy Markdown
Collaborator Author

@bobzhang attempting the rebase on master in another PR: #3900.

cristianoc added a commit to rescript-association/genType that referenced this pull request Oct 24, 2019
Still need to activate based on version, once the version number where the corresponding bucklescript changes ship is known:  rescript-lang/rescript#3785.
cristianoc added a commit to rescript-association/genType that referenced this pull request Oct 26, 2019
Still need to activate based on version, once the version number where the corresponding bucklescript changes ship is known:  rescript-lang/rescript#3785.
cristianoc added a commit to rescript-association/genType that referenced this pull request Nov 6, 2019
* Prepare for records represented as objects.

Still need to activate based on version, once the version number where the corresponding bucklescript changes ship is known:  rescript-lang/rescript#3785.

* Rebase on master.

* Version check.
@cristianoc

Copy link
Copy Markdown
Collaborator Author

In master.

@cristianoc cristianoc closed this Nov 6, 2019
@cristianoc
cristianoc deleted the records_as_objects2 branch June 18, 2022 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants