Skip to content

Commit bdb119a

Browse files
spotandjakephated
andauthored
chore(stdlib)!: Switch to using records for getInternalStats (#1898)
Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
1 parent 51d53f7 commit bdb119a

File tree

7 files changed

+103
-61
lines changed

7 files changed

+103
-61
lines changed

compiler/test/__snapshots__/provides.82c10ab4.0.snapshot

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ provides › provide12
1212
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$malloc\" (global $GRAIN$EXPORT$malloc_0 (mut i32)))
1313
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32)))
1414
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$decRef\" (global $GRAIN$EXPORT$decRef_0 (mut i32)))
15-
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$print\" (global $print_1199 (mut i32)))
16-
(import \"GRAIN$MODULE$providedType\" \"GRAIN$EXPORT$apply\" (global $apply_1197 (mut i32)))
15+
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$print\" (global $print_1200 (mut i32)))
16+
(import \"GRAIN$MODULE$providedType\" \"GRAIN$EXPORT$apply\" (global $apply_1198 (mut i32)))
1717
(import \"GRAIN$MODULE$runtime/gc\" \"malloc\" (func $malloc_0 (param i32 i32) (result i32)))
1818
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
1919
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
20-
(import \"GRAIN$MODULE$pervasives\" \"print\" (func $print_1199 (param i32 i32) (result i32)))
21-
(import \"GRAIN$MODULE$providedType\" \"apply\" (func $apply_1197 (param i32 i32) (result i32)))
20+
(import \"GRAIN$MODULE$pervasives\" \"print\" (func $print_1200 (param i32 i32) (result i32)))
21+
(import \"GRAIN$MODULE$providedType\" \"apply\" (func $apply_1198 (param i32 i32) (result i32)))
2222
(global $GRAIN$TABLE_SIZE i32 (i32.const 1))
2323
(memory $0 0)
24-
(elem $elem (global.get $relocBase_0) $lam_lambda_1198)
24+
(elem $elem (global.get $relocBase_0) $lam_lambda_1199)
2525
(export \"memory\" (memory $0))
2626
(export \"_gmain\" (func $_gmain))
2727
(export \"_start\" (func $_start))
2828
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
29-
(func $lam_lambda_1198 (param $0 i32) (param $1 i32) (result i32)
29+
(func $lam_lambda_1199 (param $0 i32) (param $1 i32) (result i32)
3030
(local $2 i32)
3131
(local $3 i32)
3232
(local $4 i32)
@@ -76,10 +76,10 @@ provides › provide12
7676
)
7777
)
7878
)
79-
(return_call $print_1199
79+
(return_call $print_1200
8080
(call $incRef_0
8181
(global.get $GRAIN$EXPORT$incRef_0)
82-
(global.get $print_1199)
82+
(global.get $print_1200)
8383
)
8484
(local.get $8)
8585
)
@@ -131,10 +131,10 @@ provides › provide12
131131
)
132132
)
133133
)
134-
(return_call $apply_1197
134+
(return_call $apply_1198
135135
(call $incRef_0
136136
(global.get $GRAIN$EXPORT$incRef_0)
137-
(global.get $apply_1197)
137+
(global.get $apply_1198)
138138
)
139139
(local.get $6)
140140
)

compiler/test/stdlib/map.test.gr

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,30 +269,25 @@ assert Map.contains(Brick, fa)
269269
assert !Map.contains(Wood, fa)
270270

271271
// Resizes the map when it grows
272-
// TODO(#190): Don't use these internals, as they need to change after 190 is fixed
273272

274273
let resize = Map.makeSized(1)
275274

276-
// (nodeCount, bucketLength)
277-
assert Map.getInternalStats(resize) == (0, 1)
275+
assert Map.getInternalStats(resize) == { currentSize: 0, bucketCount: 1 }
278276

279277
Map.set("🌾", 1, resize)
280278
Map.set("🐑", 1, resize)
281279

282-
// (nodeCount, bucketLength)
283-
assert Map.getInternalStats(resize) == (2, 1)
280+
assert Map.getInternalStats(resize) == { currentSize: 2, bucketCount: 1 }
284281

285282
Map.set("🧱", 1, resize)
286283

287-
// (nodeCount, bucketLength)
288-
assert Map.getInternalStats(resize) == (3, 2)
284+
assert Map.getInternalStats(resize) == { currentSize: 3, bucketCount: 2 }
289285

290286
// Regression tests for https://github.com/grain-lang/grain/issues/497
291287

292288
let largeMap = Map.fromArray(Array.init(128, i => (i, i)))
293289

294-
// (nodeCount, bucketLength)
295-
assert Map.getInternalStats(largeMap) == (128, 64)
290+
assert Map.getInternalStats(largeMap) == { currentSize: 128, bucketCount: 64 }
296291

297292
// Map.filter()
298293

compiler/test/stdlib/set.test.gr

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,30 +235,25 @@ assert Array.contains(3, r)
235235
assert Array.contains(4, r)
236236

237237
// Resizes the map when it grows
238-
// TODO(#190): Don't use these internals, as they need to change after 190 is fixed
239238

240239
let resize = Set.makeSized(1)
241240

242-
// (nodeCount, bucketLength)
243-
assert Set.getInternalStats(resize) == (0, 1)
241+
assert Set.getInternalStats(resize) == { currentSize: 0, bucketCount: 1 }
244242

245243
Set.add("🌾", resize)
246244
Set.add("🐑", resize)
247245

248-
// (nodeCount, bucketLength)
249-
assert Set.getInternalStats(resize) == (2, 1)
246+
assert Set.getInternalStats(resize) == { currentSize: 2, bucketCount: 1 }
250247

251248
Set.add("🧱", resize)
252249

253-
// (nodeCount, bucketLength)
254-
assert Set.getInternalStats(resize) == (3, 2)
250+
assert Set.getInternalStats(resize) == { currentSize: 3, bucketCount: 2 }
255251

256252
// Regression tests for https://github.com/grain-lang/grain/issues/497
257253

258254
let largeSet = Set.fromArray(Array.init(128, i => i))
259255

260-
// (nodeCount, bucketLength)
261-
assert Set.getInternalStats(largeSet) == (128, 64)
256+
assert Set.getInternalStats(largeSet) == { currentSize: 128, bucketCount: 64 }
262257

263258
module Immutable {
264259
from Set use { module Immutable as Set }

stdlib/map.gr

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ abstract record Map<k, v> {
3131
mut buckets: Array<Option<Bucket<k, v>>>,
3232
}
3333

34+
/**
35+
* Represents the internal state of a map.
36+
*/
37+
provide record InternalMapStats {
38+
currentSize: Number,
39+
bucketCount: Number,
40+
}
41+
3442
/**
3543
* Creates a new empty map with an initial storage of the given size. As values are added or removed, the internal storage may grow or shrink. Generally, you won't need to care about the storage size of your map and can use `Map.make()` instead.
3644
*
@@ -497,22 +505,22 @@ provide let reject = (predicate, map) => {
497505
filter((key, value) => !predicate(key, value), map)
498506
}
499507

500-
// TODO(#190): Should return a Record type instead of a Tuple
501508
/**
502509
* Provides data representing the internal state state of the map.
503510
*
504511
* @param map: The map to inspect
505512
* @returns The internal state of the map
506513
*
507514
* @since v0.2.0
515+
* @history v0.6.0: Return `InternalMapStats` record instead of a tuple
508516
*/
509517
provide let getInternalStats = map => {
510-
(map.size, Array.length(map.buckets))
518+
{ currentSize: map.size, bucketCount: Array.length(map.buckets) }
511519
}
512520

513521
/**
514522
* An immutable map implementation.
515-
*
523+
*
516524
* @since v0.6.0
517525
* @history v0.5.4: Originally in `"immutablemap"` module
518526
*/

stdlib/map.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ Type declarations included in the Map module.
2525
type Map<k, v>
2626
```
2727

28+
### Map.**InternalMapStats**
29+
30+
```grain
31+
record InternalMapStats {
32+
currentSize: Number,
33+
bucketCount: Number,
34+
}
35+
```
36+
37+
Represents the internal state of a map.
38+
2839
## Values
2940

3041
Functions and constants included in the Map module.
@@ -502,13 +513,20 @@ Parameters:
502513

503514
### Map.**getInternalStats**
504515

505-
<details disabled>
506-
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
507-
No other changes yet.
516+
<details>
517+
<summary>Added in <code>0.2.0</code></summary>
518+
<table>
519+
<thead>
520+
<tr><th>version</th><th>changes</th></tr>
521+
</thead>
522+
<tbody>
523+
<tr><td><code>next</code></td><td>Return `InternalMapStats` record instead of a tuple</td></tr>
524+
</tbody>
525+
</table>
508526
</details>
509527

510528
```grain
511-
getInternalStats : (map: Map<a, b>) => (Number, Number)
529+
getInternalStats : (map: Map<a, b>) => InternalMapStats
512530
```
513531

514532
Provides data representing the internal state state of the map.
@@ -523,7 +541,7 @@ Returns:
523541

524542
|type|description|
525543
|----|-----------|
526-
|`(Number, Number)`|The internal state of the map|
544+
|`InternalMapStats`|The internal state of the map|
527545

528546
## Map.Immutable
529547

0 commit comments

Comments
 (0)