Skip to content

Commit 94113d4

Browse files
committed
Add performance optimization "unsafeDelete"
1 parent 49adfcd commit 94113d4

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/Data/Codec/Argonaut/Sum.purs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import Foreign.Object.ST as FOST
3939
import Prim.Row as Row
4040
import Record as Record
4141
import Type.Proxy (Proxy(..))
42+
import Unsafe.Coerce (unsafeCoerce)
4243

4344
-- | A helper for defining JSON codecs for "enum" sum types, where every
4445
-- | constructor is nullary, and the type will be encoded as a string.
@@ -217,7 +218,7 @@ instance gCasesSum ∷
217218
let
218219
codecs1 = Record.get (Proxy @name) r codecs1
219220
r1 = Record.insert (Proxy @name) codecs1 {} Record r1
220-
r2 = Record.delete (Proxy @name) r Record r2
221+
r2 = unsafeDelete (Proxy @name) r Record r2
221222
in
222223
case _ of
223224
Inl lhs → gCasesEncode encoding r1 lhs
@@ -391,3 +392,8 @@ encodeSumCase encoding tag jsons =
391392
in
392393
encode jobject $ Obj.fromFoldable $ catMaybes
393394
[ tagEntry, valEntry ]
395+
396+
-- | Same as `Record.delete` deleting only happens at the type level
397+
-- | and the value is left untouched.
398+
unsafeDelete r1 r2 l a. IsSymbol l Row.Lacks l r1 Row.Cons l a r1 r2 Proxy l Record r2 Record r1
399+
unsafeDelete _ r = unsafeCoerce r

0 commit comments

Comments
 (0)