Skip to content

Commit 410c3d6

Browse files
author
Killian Perlin
committed
Make 'units()' primitive lazy
1 parent 75a3ecc commit 410c3d6

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

lkql_checker/share/lkql/kp/kp_20229.lkql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fun kp_20229(node) =
5050
if output then
5151
stdlib.any([
5252
from r.ref through parent select first ReturnStmt
53-
for r in output.p_find_all_references(units())
53+
for r in output.p_find_all_references(units().to_list)
5454
])
5555
else false
5656
}

lkql_jit/language/src/main/java/com/adacore/lkql_jit/built_ins/BuiltInFunctions.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import com.adacore.lkql_jit.runtime.values.interfaces.Indexable;
1818
import com.adacore.lkql_jit.runtime.values.interfaces.Iterable;
1919
import com.adacore.lkql_jit.runtime.values.interfaces.Iterator;
20+
import com.adacore.lkql_jit.runtime.values.lists.BaseLKQLList;
21+
import com.adacore.lkql_jit.runtime.values.lists.LKQLLazyListStreamWrapper;
2022
import com.adacore.lkql_jit.runtime.values.lists.LKQLList;
2123
import com.adacore.lkql_jit.utils.Constants;
2224
import com.adacore.lkql_jit.utils.LKQLTypesHelper;
@@ -556,12 +558,8 @@ abstract static class UnitsExpr extends BuiltInBody {
556558

557559
@Specialization
558560
@CompilerDirectives.TruffleBoundary
559-
protected LKQLList alwaysTrue() {
560-
return new LKQLList(
561-
LKQLLanguage.getContext(this)
562-
.getAllUnits()
563-
.toArray(LangkitSupport.AnalysisUnit[]::new)
564-
);
561+
protected BaseLKQLList alwaysTrue() {
562+
return new LKQLLazyListStreamWrapper(LKQLLanguage.getContext(this).getAllUnits());
565563
}
566564
}
567565

lkql_jit/language/src/main/java/com/adacore/lkql_jit/runtime/ListStorage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public ListStorage(int startSize) {
2424

2525
public void append(T item) {
2626
if (current_size >= capacity) {
27+
capacity = Math.max(capacity, 1);
2728
capacity *= 2;
2829
var new_storage = new Object[capacity];
2930
for (int i = 0; i < current_size; i++) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// Copyright (C) 2005-2025, AdaCore
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
//
5+
6+
package com.adacore.lkql_jit.runtime.values.lists;
7+
8+
import com.adacore.lkql_jit.runtime.ListStorage;
9+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
10+
import java.util.Iterator;
11+
import java.util.stream.Stream;
12+
13+
/**
14+
* This class is a wrapper from a Java Stream to an LKQLLazyList.
15+
* It is currently only used to wrap the units() builtin.
16+
*/
17+
public class LKQLLazyListStreamWrapper extends BaseLKQLLazyList {
18+
19+
// ----- Attributes -----
20+
21+
private Iterator<? extends Object> iterator;
22+
23+
// ----- Constructors -----
24+
25+
public LKQLLazyListStreamWrapper(Stream<? extends Object> source) {
26+
super(new ListStorage<>(0));
27+
iterator = source.iterator();
28+
}
29+
30+
// ----- Instance methods -----
31+
32+
@TruffleBoundary
33+
protected void initCacheTo(long n) {
34+
while ((n < 0 || cache.size() <= n) && iterator.hasNext()) {
35+
cache.append(iterator.next());
36+
}
37+
}
38+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
@unit_check
2-
fun display_units(unit) = [{message: "UNITS: " & units().img,
2+
fun display_units(unit) = [{message: "UNITS: " & units().to_list.img,
33
loc: unit.root}]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
print(units())
1+
print(units().to_list)

0 commit comments

Comments
 (0)