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

ChatGPT4 experiment for Opal coding #2541

Merged
merged 10 commits into from
Jun 15, 2023
Merged

ChatGPT4 experiment for Opal coding #2541

merged 10 commits into from
Jun 15, 2023

Conversation

hmdne
Copy link
Member

@hmdne hmdne commented Jun 2, 2023

I will expand the description later on.

New version includes separate methods for generating the names definition
and the names assignments, and for generating the sexp in the two
different situations.

The comments are preserved and expanded to give more context on what
each part of the code is doing.

(ChatGPT4)
Broken it down into several smaller functions, each handling one specific
part of the transformation process. I've also added more detailed comments
and improved variable names to increase readability.

(ChatGPT4)
Current Proc#call implementation has several conditional branches and
a try/catch block, all of which might impact performance. The main
logic is to call a block, and the exceptional scenarios are when
$$brk or $$ret is thrown. The current implementation always checks
for these exceptions which could be avoided in the common case.

An optimized version that reduces the amount of branching and avoids
setting up a try/catch block unless needed.

In this version, we've factored out the invocation of a proc or a
lambda into helper functions $call_proc and $call_lambda. In each
function, we set up the try/catch block only if necessary (i.e.,
if $$brk or $$ret is present). In Proc#call, we simply check if
a block is passed and attach it to the proc. Then we delegate to
the appropriate function based on whether the proc is a lambda.

The main goal of this refactor is to streamline the common path
and remove unnecessary branching and exception handling overhead.

(ChatGPT4)

$ bundle exec rake bench:ips FILE=benchmark-ips/bm_block_vs_yield.rb
before:              block:  3033756.5 i/s - 1.30x  slower
after:               block:  3272548.5 i/s - 1.26x  slower

The difference is small, but tangible and resulting code looks much
cleaner.
Instead of using a for...in loop to convert arguments into an array,
I've used Array.prototype.slice.call(arguments). This is more efficient
and performs better.

(ChatGPT 3.5)

before:      method missing      2.354M (± 0.3%) i/s -     11.781M in   5.003958s
after:       method missing      2.447M (± 1.2%) i/s -     12.372M in   5.057387s
Pre-allocate the memory for the result array, which can help performance
when dealing with very large hashes.

Pre-allocating memory is beneficial because it prevents the JavaScript
engine from having to continuously resize the array as elements are added.

In this version, we create an array with a predefined size (equal to the
number of keys in the hash) using new Array(length). Then, instead of
pushing elements into the array, we assign each key-value pair directly
to the corresponding index in the result array.

(ChatGPT4)
In this version, we're using new Array(length) to create an array
with a size equal to the input array. This preallocates the required
memory for the array. Then, instead of pushing each element into the
array, we're assigning the values directly to the corresponding index
in the result array.

(ChatGPT4)
The new version is based on semantics of MRI implementation of
Range#size and is rewritten in JavaScript.

(ChatGPT4)
@hmdne hmdne added this to the v1.8 milestone Jun 14, 2023
@hmdne hmdne marked this pull request as ready for review June 14, 2023 22:39
Copy link
Member

@elia elia left a comment

Choose a reason for hiding this comment

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

👍👍👍

Lint should be ok when merging

@hmdne hmdne merged commit 743fefb into master Jun 15, 2023
19 of 21 checks passed
@hmdne hmdne deleted the hmdne/chatgpt4 branch June 15, 2023 08:55
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.

None yet

2 participants