Skip to content

Commit

Permalink
adds basic locking around static caches of stripers and assemblers
Browse files Browse the repository at this point in the history
  • Loading branch information
scottfavre authored and aloneguid committed Nov 14, 2023
1 parent f3631c5 commit 288fa54
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/Parquet/Serialization/ParquetSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Parquet.Serialization {
/// </summary>
public static class ParquetSerializer {

private static readonly object _striperLock = new();
private static readonly object _assemblerLock = new();
private static readonly Dictionary<Type, object> _typeToStriper = new();
private static readonly Dictionary<Type, object> _typeToAssembler = new();

Expand Down Expand Up @@ -54,11 +56,13 @@ public static async Task<ParquetSchema> SerializeAsync<T>(IEnumerable<T> objectI

Striper<T> striper;

if(_typeToStriper.TryGetValue(typeof(T), out object? boxedStriper)) {
striper = (Striper<T>)boxedStriper;
} else {
striper = new Striper<T>(typeof(T).GetParquetSchema(false));
_typeToStriper[typeof(T)] = striper;
lock(_striperLock) {
if(_typeToStriper.TryGetValue(typeof(T), out object? boxedStriper)) {
striper = (Striper<T>)boxedStriper;
} else {
striper = new Striper<T>(typeof(T).GetParquetSchema(false));
_typeToStriper[typeof(T)] = striper;
}
}

bool append = options != null && options.Append;
Expand Down Expand Up @@ -159,11 +163,13 @@ public static async Task<IList<T>> DeserializeAsync<T>(Stream source,

Assembler<T> asm;

if(_typeToAssembler.TryGetValue(typeof(T), out object? boxedAssembler)) {
asm = (Assembler<T>)boxedAssembler;
} else {
asm = new Assembler<T>(typeof(T).GetParquetSchema(true));
_typeToAssembler[typeof(T)] = asm;
lock(_assemblerLock) {
if(_typeToAssembler.TryGetValue(typeof(T), out object? boxedAssembler)) {
asm = (Assembler<T>)boxedAssembler;
} else {
asm = new Assembler<T>(typeof(T).GetParquetSchema(true));
_typeToAssembler[typeof(T)] = asm;
}
}

return asm;
Expand Down

0 comments on commit 288fa54

Please sign in to comment.