Skip to content

Commit a597796

Browse files
authored
Fix NgramExtractingTransformer GetSlotNames to not allocate a new delegate on every invoke. (#4247)
This was showing up as allocating a huge number of delegates for no reason.
1 parent edfd10f commit a597796

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

src/Microsoft.ML.Transforms/Text/NgramTransform.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,17 +544,14 @@ private void GetSlotNames(int iinfo, int size, ref VBuffer<ReadOnlyMemory<char>>
544544
Host.Assert(n >= 0);
545545

546546
// Get the unigrams composing the current n-gram.
547-
ComposeNgramString(ngram, n, sb, keyCount,
548-
unigramNames.GetItemOrDefault);
547+
ComposeNgramString(ngram, n, sb, keyCount, in unigramNames);
549548
dstEditor.Values[slot] = sb.ToString().AsMemory();
550549
}
551550

552551
dst = dstEditor.Commit();
553552
}
554553

555-
private delegate void TermGetter(int index, ref ReadOnlyMemory<char> term);
556-
557-
private void ComposeNgramString(uint[] ngram, int count, StringBuilder sb, int keyCount, TermGetter termGetter)
554+
private void ComposeNgramString(uint[] ngram, int count, StringBuilder sb, int keyCount, in VBuffer<ReadOnlyMemory<char>> terms)
558555
{
559556
Host.AssertValue(sb);
560557
Host.AssertValue(ngram);
@@ -572,7 +569,7 @@ private void ComposeNgramString(uint[] ngram, int count, StringBuilder sb, int k
572569
sb.Append("*");
573570
else
574571
{
575-
termGetter((int)unigram - 1, ref term);
572+
term = terms.GetItemOrDefault((int)unigram - 1);
576573
sb.AppendMemory(term);
577574
}
578575
}

0 commit comments

Comments
 (0)