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

chore(stdlib)!: Switch to using records for getInternalStats #1898

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions compiler/test/__snapshots__/provides.82c10ab4.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ provides › provide12
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$malloc\" (global $GRAIN$EXPORT$malloc_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$decRef\" (global $GRAIN$EXPORT$decRef_0 (mut i32)))
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$print\" (global $print_1199 (mut i32)))
(import \"GRAIN$MODULE$providedType\" \"GRAIN$EXPORT$apply\" (global $apply_1197 (mut i32)))
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$print\" (global $print_1200 (mut i32)))
(import \"GRAIN$MODULE$providedType\" \"GRAIN$EXPORT$apply\" (global $apply_1198 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"malloc\" (func $malloc_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$pervasives\" \"print\" (func $print_1199 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$providedType\" \"apply\" (func $apply_1197 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$pervasives\" \"print\" (func $print_1200 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$providedType\" \"apply\" (func $apply_1198 (param i32 i32) (result i32)))
(global $GRAIN$TABLE_SIZE i32 (i32.const 1))
(memory $0 0)
(elem $elem (global.get $relocBase_0) $lam_lambda_1198)
(elem $elem (global.get $relocBase_0) $lam_lambda_1199)
(export \"memory\" (memory $0))
(export \"_gmain\" (func $_gmain))
(export \"_start\" (func $_start))
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
(func $lam_lambda_1198 (param $0 i32) (param $1 i32) (result i32)
(func $lam_lambda_1199 (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
Expand Down Expand Up @@ -76,10 +76,10 @@ provides › provide12
)
)
)
(return_call $print_1199
(return_call $print_1200
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $print_1199)
(global.get $print_1200)
)
(local.get $8)
)
Expand Down Expand Up @@ -131,10 +131,10 @@ provides › provide12
)
)
)
(return_call $apply_1197
(return_call $apply_1198
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $apply_1197)
(global.get $apply_1198)
)
(local.get $6)
)
Expand Down
13 changes: 4 additions & 9 deletions compiler/test/stdlib/map.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -269,30 +269,25 @@ assert Map.contains(Brick, fa)
assert !Map.contains(Wood, fa)

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

let resize = Map.makeSized(1)

// (nodeCount, bucketLength)
assert Map.getInternalStats(resize) == (0, 1)
assert Map.getInternalStats(resize) == { currentSize: 0, bucketCount: 1 }

Map.set("🌾", 1, resize)
Map.set("🐑", 1, resize)

// (nodeCount, bucketLength)
assert Map.getInternalStats(resize) == (2, 1)
assert Map.getInternalStats(resize) == { currentSize: 2, bucketCount: 1 }

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

// (nodeCount, bucketLength)
assert Map.getInternalStats(resize) == (3, 2)
assert Map.getInternalStats(resize) == { currentSize: 3, bucketCount: 2 }

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

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

// (nodeCount, bucketLength)
assert Map.getInternalStats(largeMap) == (128, 64)
assert Map.getInternalStats(largeMap) == { currentSize: 128, bucketCount: 64 }

// Map.filter()

Expand Down
13 changes: 4 additions & 9 deletions compiler/test/stdlib/set.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -235,30 +235,25 @@ assert Array.contains(3, r)
assert Array.contains(4, r)

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

let resize = Set.makeSized(1)

// (nodeCount, bucketLength)
assert Set.getInternalStats(resize) == (0, 1)
assert Set.getInternalStats(resize) == { currentSize: 0, bucketCount: 1 }

Set.add("🌾", resize)
Set.add("🐑", resize)

// (nodeCount, bucketLength)
assert Set.getInternalStats(resize) == (2, 1)
assert Set.getInternalStats(resize) == { currentSize: 2, bucketCount: 1 }

Set.add("🧱", resize)

// (nodeCount, bucketLength)
assert Set.getInternalStats(resize) == (3, 2)
assert Set.getInternalStats(resize) == { currentSize: 3, bucketCount: 2 }

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

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

// (nodeCount, bucketLength)
assert Set.getInternalStats(largeSet) == (128, 64)
assert Set.getInternalStats(largeSet) == { currentSize: 128, bucketCount: 64 }

module Immutable {
from Set use { module Immutable as Set }
Expand Down
14 changes: 11 additions & 3 deletions stdlib/map.gr
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ abstract record Map<k, v> {
mut buckets: Array<Option<Bucket<k, v>>>,
}

/**
* Represents the internal state of a map.
*/
provide record InternalMapStats {
currentSize: Number,
bucketCount: Number,
}

/**
* 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.
*
Expand Down Expand Up @@ -497,22 +505,22 @@ provide let reject = (predicate, map) => {
filter((key, value) => !predicate(key, value), map)
}

// TODO(#190): Should return a Record type instead of a Tuple
/**
* Provides data representing the internal state state of the map.
*
* @param map: The map to inspect
* @returns The internal state of the map
*
* @since v0.2.0
* @history v0.6.0: switch to returning `InternalMapStats` record
spotandjake marked this conversation as resolved.
Show resolved Hide resolved
*/
provide let getInternalStats = map => {
(map.size, Array.length(map.buckets))
{ currentSize: map.size, bucketCount: Array.length(map.buckets) }
}

/**
* An immutable map implementation.
*
*
* @since v0.6.0
* @history v0.5.4: Originally in `"immutablemap"` module
*/
Expand Down
28 changes: 23 additions & 5 deletions stdlib/map.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ Type declarations included in the Map module.
type Map<k, v>
```

### Map.**InternalMapStats**

```grain
record InternalMapStats {
currentSize: Number,
bucketCount: Number,
}
```

Represents the internal state of a map.

## Values

Functions and constants included in the Map module.
Expand Down Expand Up @@ -502,13 +513,20 @@ Parameters:

### Map.**getInternalStats**

<details disabled>
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
No other changes yet.
<details>
<summary>Added in <code>0.2.0</code></summary>
<table>
<thead>
<tr><th>version</th><th>changes</th></tr>
</thead>
<tbody>
<tr><td><code>next</code></td><td>switch to returning `InternalMapStats` record</td></tr>
</tbody>
</table>
</details>

```grain
getInternalStats : (map: Map<a, b>) => (Number, Number)
getInternalStats : (map: Map<a, b>) => InternalMapStats
```

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

|type|description|
|----|-----------|
|`(Number, Number)`|The internal state of the map|
|`InternalMapStats`|The internal state of the map|

## Map.Immutable

Expand Down
Loading
Loading