Skip to content
This repository was archived by the owner on Feb 3, 2021. It is now read-only.

Commit 94b693e

Browse files
committed
Give native attrs a firey baptism by switching $!pos and $!from attributes in Cursor to be native int attributes. This saves a bunch of boxing/unboxing and as a result should reduce GC churn and also be a memory improvement for big parses (since Match objects reference Cursor objects). Note that Match objects still need to store these boxed for now, but that'll be fixed in the future and be a further memory usage and GC churn reduction.
1 parent 155b951 commit 94b693e

File tree

4 files changed

+59
-42
lines changed

4 files changed

+59
-42
lines changed

src/HLL/Grammar.pm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,8 @@ An operator precedence parser.
553553
$P0 = find_lex '$preclim'
554554
preclim = $P0
555555
556-
.local pmc here, pos, debug
556+
.local pmc here, debug
557+
.local int pos
557558
(here, pos) = self.'!cursor_start'()
558559
debug = getattribute here, cur_class, '$!debug'
559560
if null debug goto debug_1
@@ -699,7 +700,7 @@ An operator precedence parser.
699700
term = pop termstack
700701
pos = here.'pos'()
701702
here = self.'!cursor_start'()
702-
setattribute here, cur_class, '$!pos', pos
703+
repr_bind_attr_int here, cur_class, '$!pos', pos
703704
setattribute here, cur_class, '$!match', term
704705
here.'!reduce'('EXPR')
705706
if null debug goto done

src/PAST/Compiler-Regex.pir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,9 +1187,9 @@ Code for initial regex scan.
11871187
ops.'push_pirop'('ne', '$I10', CURSOR_FAIL, donelabel)
11881188
ops.'push_pirop'('goto', scanlabel)
11891189
ops.'push'(looplabel)
1190-
self.'!cursorop'(ops, 'from', 1, '$P10')
1191-
ops.'push_pirop'('inc', '$P10')
1192-
ops.'push_pirop'('set', pos, '$P10')
1190+
self.'!cursorop'(ops, 'from', 1, pos)
1191+
ops.'push_pirop'('inc', pos)
1192+
self.'!cursorop'(ops, '!cursor_from', 0, pos)
11931193
ops.'push_pirop'('ge', pos, eos, donelabel)
11941194
ops.'push'(scanlabel)
11951195
ops.'push_pirop'('set_addr', '$I10', looplabel)

src/Regex/Cursor-protoregex-peek.pir

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ Perform a match for protoregex C<name>.
4444
.local pmc target
4545
.local int pos
4646
target = getattribute self, cur_class, '$!target'
47-
$P1 = getattribute self, cur_class, '$!pos'
48-
pos = $P1
47+
pos = repr_get_attr_int self, cur_class, '$!pos'
4948

5049
# Use the character at the current match position to determine
5150
# the longest possible token we could encounter at this point.

src/Regex/Cursor.pir

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ grammars.
9191
how.'add_method'(type_obj, '!INTERPOLATE', $P33)
9292
.const 'Sub' $P34 = 'Regex_Cursor_meth_!INTERPOLATE_REGEX'
9393
how.'add_method'(type_obj, '!INTERPOLATE_REGEX', $P34)
94+
.const 'Sub' $P35 = 'Regex_Cursor_meth_!cursor_from'
95+
how.'add_method'(type_obj, '!cursor_from', $P35)
9496
.const 'Sub' $P10 = 'Regex_Cursor_meth_before'
9597
how.'add_method'(type_obj, 'before', $P10)
9698
.const 'Sub' $P11 = 'Regex_Cursor_meth_ident'
@@ -147,13 +149,14 @@ grammars.
147149
how.'add_method'(type_obj, 'Bool', $P17)
148150

149151
# Add attributes.
150-
.local pmc NQPAttribute, attr
152+
.local pmc NQPAttribute, int_type, attr
151153
NQPAttribute = get_hll_global "NQPAttribute"
154+
int_type = get_hll_global "int"
152155
attr = NQPAttribute."new"("$!target" :named("name"))
153156
how."add_attribute"(type_obj, attr)
154-
attr = NQPAttribute."new"("$!from" :named("name"))
157+
attr = NQPAttribute."new"("$!from" :named("name"), int_type :named('type'))
155158
how."add_attribute"(type_obj, attr)
156-
attr = NQPAttribute."new"("$!pos" :named("name"))
159+
attr = NQPAttribute."new"("$!pos" :named("name"), int_type :named('type'))
157160
how."add_attribute"(type_obj, attr)
158161
attr = NQPAttribute."new"("$!match" :named("name"))
159162
how."add_attribute"(type_obj, attr)
@@ -231,9 +234,11 @@ for the Cursor if one hasn't been created yet.
231234
.local pmc target, from, to
232235
target = getattribute self, cur_class, '$!target'
233236
setattribute match, '$!target', target
234-
from = getattribute self, cur_class, '$!from'
237+
$I0 = repr_get_attr_int self, cur_class, '$!from'
238+
from = box $I0
235239
setattribute match, '$!from', from
236-
to = getattribute self, cur_class, '$!pos'
240+
$I0 = repr_get_attr_int self, cur_class, '$!pos'
241+
to = box $I0
237242
setattribute match, '$!to', to
238243

239244
# Create any arrayed subcaptures.
@@ -378,8 +383,8 @@ Return the cursor's current position.
378383
.sub 'pos' :method :subid('Regex_Cursor_meth_pos')
379384
.local pmc cur_class
380385
cur_class = get_global '$?CLASS'
381-
$P0 = getattribute self, cur_class, '$!pos'
382-
.return ($P0)
386+
$I0 = repr_get_attr_int self, cur_class, '$!pos'
387+
.return ($I0)
383388
.end
384389
385390
@@ -392,8 +397,8 @@ Return the cursor's from position.
392397
.sub 'from' :method :subid('Regex_Cursor_meth_from')
393398
.local pmc cur_class
394399
cur_class = get_global '$?CLASS'
395-
$P0 = getattribute self, cur_class, '$!from'
396-
.return ($P0)
400+
$I0 = repr_get_attr_int self, cur_class, '$!from'
401+
.return ($I0)
397402
.end
398403

399404
=back
@@ -423,16 +428,12 @@ Create a new cursor for matching C<target>.
423428
setattribute cur, cur_class, '$!target', $P0
424429

425430
if has_cont goto cursor_cont
426-
$P0 = box pos
427-
setattribute cur, cur_class, '$!from', $P0
428-
$P0 = box pos
429-
setattribute cur, cur_class, '$!pos', $P0
431+
repr_bind_attr_int cur, cur_class, '$!from', pos
432+
repr_bind_attr_int cur, cur_class, '$!pos', pos
430433
goto cursor_done
431434
cursor_cont:
432-
$P0 = box CURSOR_FAIL
433-
setattribute cur, cur_class, '$!from', $P0
434-
$P0 = box cont
435-
setattribute cur, cur_class, '$!pos', $P0
435+
repr_bind_attr_int cur, cur_class, '$!from', CURSOR_FAIL
436+
repr_bind_attr_int cur, cur_class, '$!pos', cont
436437
cursor_done:
437438

438439
.return (cur)
@@ -461,11 +462,12 @@ provided, then the new cursor has the same type as lang.
461462
regex = getattribute self, cur_class, '&!regex'
462463
unless null regex goto cursor_restart
463464

464-
.local pmc from, target, debug
465+
.local pmc target, debug
466+
.local int from
465467

466-
from = getattribute self, cur_class, '$!pos'
467-
setattribute cur, cur_class, '$!from', from
468-
setattribute cur, cur_class, '$!pos', from
468+
from = repr_get_attr_int self, cur_class, '$!pos'
469+
repr_bind_attr_int cur, cur_class, '$!from', from
470+
repr_bind_attr_int cur, cur_class, '$!pos', from
469471

470472
target = getattribute self, cur_class, '$!target'
471473
setattribute cur, cur_class, '$!target', target
@@ -475,16 +477,17 @@ provided, then the new cursor has the same type as lang.
475477
.return (cur, from, target, 0)
476478

477479
cursor_restart:
478-
.local pmc pos, cstack, bstack
479-
from = getattribute self, cur_class, '$!from'
480+
.local pmc cstack, bstack
481+
.local int pos
482+
from = repr_get_attr_int self, cur_class, '$!from'
480483
target = getattribute self, cur_class, '$!target'
481484
debug = getattribute self, cur_class, '$!debug'
482485
cstack = getattribute self, cur_class, '@!cstack'
483486
bstack = getattribute self, cur_class, '@!bstack'
484-
pos = box CURSOR_FAIL
487+
pos = CURSOR_FAIL
485488

486-
setattribute cur, cur_class, '$!from', from
487-
setattribute cur, cur_class, '$!pos', pos
489+
repr_bind_attr_int cur, cur_class, '$!from', from
490+
repr_bind_attr_int cur, cur_class, '$!pos', pos
488491
setattribute cur, cur_class, '$!target', target
489492
setattribute cur, cur_class, '$!debug', debug
490493
if null cstack goto cstack_done
@@ -506,10 +509,9 @@ Permanently fail this cursor.
506509
=cut
507510

508511
.sub '!cursor_fail' :method :subid('Regex_Cursor_meth_!cursor_fail')
509-
.local pmc cur_class, pos
512+
.local pmc cur_class
510513
cur_class = get_global '$?CLASS'
511-
pos = box CURSOR_FAIL_RULE
512-
setattribute self, cur_class, '$!pos', pos
514+
repr_bind_attr_int self, cur_class, '$!pos', CURSOR_FAIL_RULE
513515
null $P0
514516
setattribute self, cur_class, '$!match', $P0
515517
setattribute self, cur_class, '@!bstack', $P0
@@ -528,13 +530,13 @@ with a "real" Match object when requested.
528530
=cut
529531

530532
.sub '!cursor_pass' :method :subid('Regex_Cursor_meth_!cursor_pass')
531-
.param pmc pos
533+
.param int pos
532534
.param string name
533535

534536
.local pmc cur_class
535537
cur_class = get_global '$?CLASS'
536538

537-
setattribute self, cur_class, '$!pos', pos
539+
repr_bind_attr_int self, cur_class, '$!pos', pos
538540
.local pmc match
539541
match = get_global '$!TRUE'
540542
setattribute self, cur_class, '$!match', match
@@ -615,10 +617,24 @@ Set the cursor's position to C<pos>.
615617
=cut
616618

617619
.sub '!cursor_pos' :method :subid('Regex_Cursor_meth_!cursor_pos')
618-
.param pmc pos
620+
.param int pos
621+
.local pmc cur_class
622+
cur_class = get_global '$?CLASS'
623+
repr_bind_attr_int self, cur_class, '$!pos', pos
624+
.end
625+
626+
627+
=item !cursor_from(pos)
628+
629+
Set the cursor's from to C<from>.
630+
631+
=cut
632+
633+
.sub '!cursor_from' :method :subid('Regex_Cursor_meth_!cursor_from')
634+
.param int from
619635
.local pmc cur_class
620636
cur_class = get_global '$?CLASS'
621-
setattribute self, cur_class, '$!pos', pos
637+
repr_bind_attr_int self, cur_class, '$!from', from
622638
.end
623639
624640
@@ -636,9 +652,10 @@ Log a debug message.
636652
$P0 = getattribute self, cur_class, '$!debug'
637653
if null $P0 goto done
638654
unless $P0 goto done
639-
.local pmc fmt, from, pos, orig, line
655+
.local pmc fmt, orig, line
656+
.local int from
640657
fmt = new ['ResizablePMCArray']
641-
from = getattribute self, cur_class, '$!from'
658+
from = repr_get_attr_int self, cur_class, '$!from'
642659
orig = getattribute self, cur_class, '$!target'
643660
$P0 = get_hll_global ['HLL'], 'Compiler'
644661
line = $P0.'lineof'(orig, from, 'cache'=>1)

0 commit comments

Comments
 (0)