Skip to content

Commit c13f2b1

Browse files
committed
added string cache todo
1 parent cacae5d commit c13f2b1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

kscr-core/Util/StringCache.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5+
using System.Linq;
6+
using KScr.Core.Bytecode;
7+
using KScr.Core.Std;
58

69
namespace KScr.Core.Util;
710

@@ -10,6 +13,20 @@ public sealed class StringCache
1013
public const string FileName = "strings" + RuntimeBase.BinaryFileExt;
1114

1215
public static readonly byte[] NewLineBytes = RuntimeBase.Encoding.GetBytes("\n");
16+
17+
public static IEnumerable<string> CommonStrings => new[] { Method.ConstructorName, Method.StaticInitializerName }
18+
.Concat(GetAllClasses(Class.LibRootPackage)
19+
.SelectMany(cls => new[] { cls.CanonicalName, cls.FullDetailedName }));
20+
21+
private static IEnumerable<Class> GetAllClasses(IPackageMember mem)
22+
{
23+
if (mem is Package pkg)
24+
return pkg.PackageMembers.Values.SelectMany(GetAllClasses);
25+
if (mem is Class cls)
26+
return new[]{cls};
27+
throw new System.Exception("invalid state");
28+
}
29+
1330
private readonly IList<string> _strings = new List<string>();
1431
private int _index = -1;
1532

@@ -19,6 +36,8 @@ public int this[string str]
1936
{
2037
if (str == null)
2138
throw new NullReferenceException();
39+
if (CommonStrings.Contains(str))
40+
; // todo Do not include common strings in every string cache
2241
if (_strings.IndexOf(str) is var i && i != -1)
2342
return i;
2443
var id = ++_index;

0 commit comments

Comments
 (0)