AVRO-4249: [java] provide a cache of schema to avoid building by mkeskells · Pull Request #3746 · apache/avro · GitHub
Skip to content

AVRO-4249: [java] provide a cache of schema to avoid building#3746

Open
mkeskells wants to merge 9 commits into
apache:mainfrom
mkeskells:AVRO-4249-schema-cache
Open

AVRO-4249: [java] provide a cache of schema to avoid building#3746
mkeskells wants to merge 9 commits into
apache:mainfrom
mkeskells:AVRO-4249-schema-cache

Conversation

@mkeskells

@mkeskells mkeskells commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

What is the purpose of the change

To improve the performance of parsing files.
In an environment where we parse 10k to 100K (generally very small) files that are small, and use the same schema, or a handful of schemas we see many Tb of garbage generation with duplicate schemas being parsed

this PR is a simple fix to enable a cache to be inserted in the reader, so that the cache lookup can replace the parse where there is an exact match already

The shape of the API changes - I leave this to the reviewers to comment, and I am happy to work to their steer, and will generate tests when I have agreement of the approach to this

Verifying this change

(Please pick one of the following options)

I will add tests once the chnages to the API have been agreed. There are many ways that this chnage could be implemented so I dont want to spend th time until the shape can be agreed

Documentation

  • Does this pull request introduce a new feature? yes - i presume there would be some pluggable system setting, and an API change. I ahve proposed a system property
  • If yes, how is the feature documented? not documented as yet

@github-actions github-actions Bot added the Java Pull Requests for Java binding label Apr 29, 2026
@mkeskells mkeskells force-pushed the AVRO-4249-schema-cache branch from 65f4513 to 34e6910 Compare April 29, 2026 22:30
@mkeskells

Copy link
Copy Markdown
Contributor Author

@mkeskells

Copy link
Copy Markdown
Contributor Author

Could a maintainer please add the performance label to this PR?

@mkeskells

mkeskells commented May 3, 2026

Copy link
Copy Markdown
Contributor Author

I have fixed the licence issues reported in the build (missing licence header).
Is there any way that I can re-run the builds? Or is that only for the maintainers?

@mkeskells

Copy link
Copy Markdown
Contributor Author

I finally got o the bottom of the memmory leak I was chasing when I observed the problem that this fixes- its https://issues.apache.org/jira/browse/AVRO-4253, a memory leak, which in my environment was holding only 200Gb of Schemas due to the leak. Mostly fixed by this PR

@RyanSkraba

Copy link
Copy Markdown
Contributor

Hey, pardon me! I appreciate the work, and I'll take a closer look soon -- we're going to do a 1.13.0 release just after the next one 1.12.2, and this should be in it!

@mkeskells

mkeskells commented May 7, 2026

Copy link
Copy Markdown
Contributor Author

Hey, pardon me! I appreciate the work, and I'll take a closer look soon -- we're going to do a 1.13.0 release just after the next one 1.12.2, and this should be in it!

Hi @RyanSkraba I have fixed the checkstyle error I hope (unfortunately checkstyle doesnt run on this project fo me so I couldn't check before)

A bit off topic, but Iwould have thought that https://issues.apache.org/jira/browse/AVRO-4253 would need to be fixed for any future release. Happy to assist (or get more involved in avro), but I dont have the context to propose anything other than the trivial/expensive fix in the comments

@mkeskells

Copy link
Copy Markdown
Contributor Author

@RyanSkraba I have added a soft cache as well.

QQ - what should the default cache behaviour be?
I can't see much downside in having a weak cache as the default, but it is a change in behaviour
Would it be a good idea to set it to weak now, or wait to 1.13

I don't know the avro policy on changes like this so looking for steer

@mkeskells

mkeskells commented May 10, 2026

Copy link
Copy Markdown
Contributor Author

I do think that there is an oppertunity to use the same logic on a broader level, a soft cache of a String + parse options -> schema, but don't know the innards of the parsing expectations to know if a new Schema is guaranteed

handling duplicate schemas seems to have secondary costs, like duplicate data in FastReaderBuilder, which can be avoided

Anyway - its outside of the scope of this PR, and outside my knowledge, but maybe a manttainer can have an opinion

do {
for (long i = 0; i < l; i++) {
String key = vin.readString(null).toString();
String key = vin.readString();

@mkeskells mkeskells May 12, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to the schema cache, but removes the allocation of the ByteBuffer, which is disguarded immediately. This was done after the benchmark results were published and will reduce allocations by the size of the schema for each file parsed, for all scenarios

for (long i = 0; i < l; i++) {
String key = vin.readString(null).toString();
String key = vin.readString();
ByteBuffer value = vin.readBytes(null);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could reuse the byte buffer, but an impovement in #3776 removes it anyway

@mkeskells

Copy link
Copy Markdown
Contributor Author

@mkeskells mkeskells force-pushed the AVRO-4249-schema-cache branch from a66abf0 to 6e14feb Compare May 16, 2026 15:56
mkeskells added 2 commits May 30, 2026 22:57
# Conflicts:
#	lang/java/avro/src/main/java/org/apache/avro/file/DataFileStream.java
@mkeskells mkeskells force-pushed the AVRO-4249-schema-cache branch from 0dab957 to b26ec41 Compare May 30, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java Pull Requests for Java binding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants