Skip to content

Commit

Permalink
- Optimized the op-assign operators (such as: +=, *=, /=)
Browse files Browse the repository at this point in the history
Example:
	var x = [1,2,3];
	x[1] += 1;		# does only one array lookup
	say x;			# prints: [1,3,3]

In addition, we can now chain multiple op-assign operators:

	var x = 42;
	x -= 1 *= 2
	say x;		# prints: 82 (41 * 2)

- Using the same trick, var-mutable methods are also much faster:

	var x = [1,2,3];
	x[1].sqrt!		# does only one array lookup
	say x;			# prints: [1, 1.414213, 3]
  • Loading branch information
trizen committed Dec 11, 2015
1 parent 98eb09f commit 2dd3ec7
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/Sidef/Deparse/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1296,10 +1296,11 @@ HEADER

# Reasign operators, such as: +=, -=, *=, /=, etc...
if (exists $self->{reassign_ops}{$method}) {

$code =
"do { $code=($code->\${\\'$self->{reassign_ops}{$method}'}"
. $self->deparse_args(@{$call->{arg}})
. "); $code }";
"CORE::sub : lvalue {my \$ref=\\$code; \$\$ref=\$\$ref\->\${\\'$self->{reassign_ops}{$method}'}"
. $self->deparse_args(@{$call->{arg}}) . "}->()";

next;
}

Expand Down Expand Up @@ -1374,11 +1375,12 @@ HEADER

# Exclamation mark (!) at the end of a method
if (substr($method, -1) eq '!') {
$code = '('
. "$old_code=$code->"

$code =
"CORE::sub : lvalue {my \$ref=\\$code; \$\$ref=\$\$ref\->"
. substr($method, 0, -1)
. (exists($call->{arg}) ? $self->deparse_args(@{$call->{arg}}) : '')
. ", $old_code" . ')[1]';
. (exists($call->{arg}) ? $self->deparse_args(@{$call->{arg}}) : '') . "}->()";

next;
}

Expand Down

0 comments on commit 2dd3ec7

Please sign in to comment.